Now I use the following code
package com.marketecho.mecap;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.*;
public class WritingRecord {
public static void main(String[] args) {
Session session = null;
try {
// This step will read hibernate.cfg.xml and prepare hibernate for use
SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
session = sessionFactory.openSession();
org.hibernate.Transaction tx = session.beginTransaction();
//Create new instance of Contact and set values in it by reading them from form object
System.out.println("Inserting Record");
Customer cust = new Customer();
cust.setCustomerId(3457);
cust.setCustomerFName("Deepak");
cust.setCustomerLName("Kumar");
cust.setEmail("
[email protected]");
session.save(cust);
tx.commit();
System.out.println("Done");
}
catch(Exception e) {
System.out.println(e.getMessage());
}
finally {
// Actual contact insertion will happen at this step
session.flush();
session.close();
}
}
}
I get the following error:
Dec 21, 2006 1:34:33 PM org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.0.5
Dec 21, 2006 1:34:33 PM org.hibernate.cfg.Environment <clinit>
INFO: loaded properties from resource hibernate.properties: {hibernate.connection.username=root, hibernate.connection.password=****, hibernate.cglib.use_reflection_optimizer=true, hibernate.dialect=org.hibernate.dialect.MySQLDialect, hibernate.connection.url=jdbc:mysql://localhost/mecap, hibernate.connection.driver_class=org.gjt.mm.mysql.Driver}
Dec 21, 2006 1:34:33 PM org.hibernate.cfg.Environment <clinit>
INFO: using CGLIB reflection optimizer
Dec 21, 2006 1:34:33 PM org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
Dec 21, 2006 1:34:33 PM org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
Dec 21, 2006 1:34:33 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
Dec 21, 2006 1:34:34 PM org.hibernate.cfg.Configuration doConfigure
INFO: Configured SessionFactory: null
Exception in thread "main" java.lang.NullPointerException
at com.marketecho.mecap.WritingRecord.main(WritingRecord.java:39)
-----------
Still INFO: Configured SessionFactory: null