Hibernate/JPA - NullPointerException when accessing SingularAttribute parameter

前端 未结 1 1140
醉梦人生
醉梦人生 2021-01-12 03:59

I\'m trying to use JPA2 type-safe criteria queries with Hibernate 5.0.7.Final.

...
criteria.where( builder.equal( root.get(SingularAttribute.attr), value ));         


        
相关标签:
1条回答
  • 2021-01-12 04:51

    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();
        }
    }
    
    0 讨论(0)
提交回复
热议问题