Contact.java
public class Contact
{
private String firstName;
private String lastName;
private String email;
private long id;
public void setFirstName(String firstName)
{
this.firstName=firstName;
}
public String getFirstName()
{
return firstName;
}
public void setLastName(String lastName)
{
this.lastName=lastName;
}
public String getLastName()
{
return lastName;
}
public void setEmail(String email)
{
this.email=email;
}
public String getEmail()
{
return email;
}
public void setId(long id)
{
this.id=id;
}
public long getId()
{
return id;
}
}
hibernat-cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:ORCL</property>
<property name="hibernate.connection.username">scott</property>
<property name="hibernate.connection.password">tiger</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="contact.hbm.xml"/>
</session-factory>
</hibernate-configuration>
contact.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Contact" table="CONTACT">
<id name="id" type="long" column="ID">
<generator class="assigned"/>
</id>
<property name="firstName">
<column name="FIRSTNAME"/>
</property>
<property name="lastName">
<column name="LASTNAME"/>
</property>
<property name="email">
<column name="EMAIL"/>
</property>
</class>
</hibernate-mapping>
FirstExample.java
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class FirstExample
{
public static void main(String[] args)
{
Session session=null;
try
{
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
session=sessionFactory.openSession();
System.out.println("Inserting Record");
Contact contact = new Contact();
contact.setId(3);
contact.setFirstName("Deepak");
contact.setLastName("Kumar");
contact.setEmail("
[email protected]");
session.save(contact);
System.out.println("Done");
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
finally
{
session.flush();
session.close();
}
}
}
this is my program..
my jdk is 1.5
jdbc is oracle 10g
in class path i include these
set CLASSPATH=C:\hibernate-3.0alpha\hibernate3.jar;C:\hibernate-3.0alpha\lib\dom4j-1.4.jar;C:\hibernate-3.0alpha\lib\cglib-full-2.0.2.jar;C:\hibernate-3.0alpha\lib\hibernate-tools.jar;C:\hibernate-3.0alpha\lib\commons-logging-1.0.4.jar;C:\hibernate-3.0alpha\lib\commons-collections-2.1.1.jar;%CLASSPATH%
and in env variable i included classes12.jar
it is compiling fine
all these four files i put it in one folder and i compiled like this
first set the class path
after that i compiled
javac *.java
after that i run the program
java FirstExample
it is showing error like this
Sep 27, 2008 11:05:29 AM org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.0 alpha
Sep 27, 2008 11:05:29 AM org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
Sep 27, 2008 11:05:29 AM org.hibernate.cfg.Environment <clinit>
INFO: using CGLIB reflection optimizer
Sep 27, 2008 11:05:29 AM org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
Sep 27, 2008 11:05:29 AM org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
Sep 27, 2008 11:05:29 AM org.hibernate.cfg.Configuration getConfigurationInput
ream
INFO: Configuration resource: /hibernate.cfg.xml
Sep 27, 2008 11:05:29 AM org.hibernate.cfg.Configuration getConfigurationInput
ream
WARNING: /hibernate.cfg.xml not found
/hibernate.cfg.xml not found
Exception in thread "main" java.lang.NullPointerException
at FirstExample.main(FirstExample.java:34)
is it the correct way to compile the program
or please suggest me.. iam new to hibernate...