Amruta Mistry

Greenhorn
+ Follow
since Jan 25, 2012
Amruta likes ...
Linux
Merit badge: grant badges
Biography
Amruta Mistry
Bechlors in Computer Enngineering
For More
Amhedabad
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
2
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Amruta Mistry

I think i emphasis for same thing in my very first reply to the post

11 years ago
The method which you are talking is static while the varibale you are tying to use within the same is non static.
Have you tried declaring the variable as static??

11 years ago
Hey Ankit
Thanks for the reply.
I was able to fix the issue.There was 1 jar missing added that jar as well some code changes provided with my main class.

Initially i was using:



which i have changed to



as well i have added the correct port number for my database in hibernate.cfg.xml file which has fixed my issue.

Thanks for your reply ankit.

Cheers,
Amruta
PreparedStatement is an interface while Statement is a class
If you refer JDBC API you will get more idea about same.
11 years ago
Hey Campbell Ritchie

Thanks
I saw good books links
Will try to grab whenever time permits
11 years ago
You can go for "The complete referece: Java"
This will help you out
As well you can go for book by "khalid mughal"
11 years ago
Hello Everyone

I am new to Hibernate.
This is my first hibernate code where I am getting exception.
And I am unable to figure out what's going wrong.

Here is my code:

this is my Employee.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="Employee" table="EMPLOYEE">
<id name="id" type="int" column="id">
<generator class="native"/>
</id>

<property name="firstname" column="first_name" type="String"/>
<property name="lastname" column="last_name" type="String"/>
<property name="salary" column="salary" type="int"/>
</class>
</hibernate-mapping>


hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">


<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>

<property name="hibernate.connection.url">
jdbc:mysql://localhost/hibernatetest
</property>
<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password">
root
</property>

<!-- List of XML mapping files -->
<mapping resource="Employee.hbm.xml"/>

</session-factory>
</hibernate-configuration>

My POJO
package com.practice.HBtest;

public class Employee {

private int id;
private String firstName;
private String lastName;
private Integer salary;


public Employee(){}
public Employee(String fname,String lname , int salary){
this.firstName=fname;
this.lastName=lname;
this.salary=salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id=id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String first_name) {
this.firstName = first_name;
}
public String getLastName() {
return lastName;
}
public void setLastName(String last_name) {
this.lastName = last_name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}


my main class:
package com.practice.HBtest;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class EmployeeRecordManager {

private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
private Session session;

public static void main(String[] args) {

EmployeeRecordManager erm = new EmployeeRecordManager();

try {
Configuration cfg = new Configuration()
.addResource("Employee.hbm.xml");
SessionFactory sessionFactory = cfg.configure()
.buildSessionFactory(serviceRegistry);

} catch (Exception e) {
System.out.println(e);
}
Integer empID1 = erm.addEmployee("Amruta", "Ali", 1000);
Integer empID2 = erm.addEmployee("Ruchi", "Das", 5000);
Integer empID3 = erm.addEmployee("Mitul", "Paul", 10000);

System.out.println(erm);

}

public Integer addEmployee(String fname, String lname, int salary) {
Session session = sessionFactory.openSession();
Transaction tx = null;
Integer empID = null;
try {
tx = session.beginTransaction();
Employee employee = new Employee(fname, lname, salary);
empID = (Integer) session.save(employee);
tx.commit();
} catch (HibernateException e) {
if (tx != null)
tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
return empID;
}

}

When i run it it throws me below exception:
Nov 7, 2012 2:55:05 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
Nov 7, 2012 2:55:05 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.8.Final}
Nov 7, 2012 2:55:05 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Nov 7, 2012 2:55:05 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Nov 7, 2012 2:55:05 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Employee.hbm.xml
Nov 7, 2012 2:55:05 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Nov 7, 2012 2:55:05 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Nov 7, 2012 2:55:05 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Employee.hbm.xml
Nov 7, 2012 2:55:05 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Nov 7, 2012 2:55:05 PM org.hibernate.cfg.Configuration$MappingsImpl addImport
INFO: HHH000071: Duplicate import: Employee -> Employee
org.hibernate.InvalidMappingException: Could not parse mapping document from resource Employee.hbm.xml
Exception in thread "main" java.lang.NullPointerException
at com.practice.HBtest.EmployeeRecordManager.addEmployee(EmployeeRecordManager.java:38)
at com.practice.HBtest.EmployeeRecordManager.main(EmployeeRecordManager.java:29)

Campbell Ritchie wrote:You have one object which is created from the subclass . . . but part of it is created from the superclass too. Find a copy of Head First Java (Sierra and Bates) and find the diagrams with concentric circles showing objects being made up of different parts from different classes.
Anybody know which page that would be?

page: 236 chapter :9
11 years ago