0

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();
    }
}
alexrnov
  • 2,346
  • 3
  • 18
  • 34
Vicky
  • 104
  • 1
  • 9
  • Can you show the exception? – codeLover Jan 24 '18 at 06:06
  • there is no exception which i see in the logs, I find the result object as null which to me seems like hibernate saying no object found for the mentioned criteria / condition. – Vicky Jan 24 '18 at 06:23
  • What you get when you execute hibernateSession.createQuery() method? your result type is basically an object so you better define list or something like that and sniff the result. – ibrahimgunes Jan 24 '18 at 06:24
  • Can you remove quote(') surrounding the emailId and try – codeLover Jan 24 '18 at 06:28
  • Depending on your implementation of the `Customer` class `session.save(tempCustomer);` should already return the ID associated with the newly inserted customer entity. There is no need to fetch the entity from the database again, just to get the ID. – dpr Jan 24 '18 at 09:28

0 Answers0