在单个方法中执行时,Hibernate插入和选择操作失败

时间:2018-01-24 05:55:22

标签: java hibernate transactions hibernate-session

我正在使用hibernate与mySQL DB服务器进行CRUD操作。

我遇到了一个奇怪的问题,当我进行插入操作时,操作成功。我甚至可以使用DB编辑器检查数据库中的值。 现在,如果我尝试在同一记录上发出一个选择呼叫,我就不会得到任何结果。

我的HibernateUtil类如下所示:

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;
    }
}

我的实现类如下所示:

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();
    }

}

0 个答案:

没有答案