I am using hibernate to do CRUD operations with mySQL DB server.
I am running into a weird problem where when I do an insert operation, the operation is successful. I even can check the values in the DB using a DB editor. Now if I try to fire a select call on the same record, I donot get any result back.
My HibernateUtil Class looks like following:
public class HibernateUtil {
//Annotation based configuration
private static SessionFactory sessionAnnotationFactory;
private static SessionFactory buildSessionAnnotationFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
System.out.println("Hibernate Annotation Configuration loaded");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
System.out.println("Hibernate Annotation serviceRegistry created");
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionAnnotationFactory() {
if (sessionAnnotationFactory == null) sessionAnnotationFactory = buildSessionAnnotationFactory();
return sessionAnnotationFactory;
}
}
My Implementation class looks like following:
public void insertCustomer (String emailId,String mobileNumber,
String password) throws MyException {
Session session = HibernateUtil.getSessionAnnotationFactory().openSession();
Customers tempCustomer = new Customers(emailId,null,mobileNumber,password,"N",null,"NC",null,"N","N","CUS","N","N");
session.beginTransaction();
session.save(tempCustomer);
session.getTransaction().commit();
QueryCustomer queryCustomer = new QueryCustomer();
int customerId = queryCustomer.getCustomerId(emailId);
//Here the customer id is not returned to me... which is wrong
}
public int getCustomerId(String emailId) throws MoneyBuddyException {
logger.debug("QueryCustomer class : getCustomerId method : start");
Session hibernateSession = HibernateUtil.getSessionAnnotationFactory().openSession();
//hibernateSession.flush();
Object result;
int customerId = 0;
try {
hibernateSession.beginTransaction();
System.out.println("HI there 1 + emailID : "+emailId);
result = hibernateSession.createQuery("select customerId from Customers where emailId = '"+emailId+"'").uniqueResult();
if (result != null) {
System.out.println("HI there 1"+result.toString());
customerId = Integer.parseInt(result.toString());
}
//Here I get the result as null.. which is wrong
logger.debug("QueryCustomer class : getCustomerId method : end");
return customerId;
} catch (HibernateException e) {
logger.debug("QueryCustomer class : getCustomerId method : Caught Exception");
e.printStackTrace();
throw new MyException(e.getMessage(),e);
} catch (Exception e) {
logger.debug("QueryCustomer class : getCustomerId method : Caught Exception");
e.printStackTrace();
throw new MyException(e.getMessage(),e);
} finally {
hibernateSession.clear();
hibernateSession.close();
}
}