Hibernate/JPA - NullPointerException when accessing SingularAttribute parameter

。_饼干妹妹 提交于 2019-12-01 03:38:49

Calling HibernateUtil.getEntityManager() in main before method call somehow works. It even works when i call literally a empty block of a method init(). It could be related with initialization of classes. Here's the code snippet.

public class HibernateUtil {
    private static EntityManagerFactory emFactory;
    private static EntityManager em;
    private static final Logger log = LoggerFactory.getLogger(HibernateUtil.class);
    private static final String PERSISTENCE_UNIT = "pt";
    static{
        log.info("Creating entity manager factory");
        emFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
    }
    //calling this from main before PersonDao's method calls somehow works...
    public void init(){} //does nothing
    public static EntityManager getEntityManager(){
        if(em != null && em.isOpen()){
            closeEntityManager();
        }
        log.debug("Creating new entity manager");
        em = emFactory.createEntityManager();
        return em;
    }
    public static void close() throws Exception {
        if(em != null && em.isOpen()){
            closeEntityManager();
        }
        log.info("Closing entity manager factory");
        emFactory.close();
    }
    private static void closeEntityManager(){
        log.info("Closing last entity manager");
        em.close();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!