Hibernate/JPA - 访问 SingularAttribute 参数时出现 NullPointerException
Hibernate/JPA - NullPointerException when accessing SingularAttribute parameter
我正在尝试将 JPA2 类型安全条件查询与 Hibernate 5.0 一起使用。7.Final。
...
criteria.where( builder.equal( root.get(SingularAttribute.attr), value ));
//where parameters are
//criteria.where( builder.equal( root.get(Person_.name), "Can" ));
...
root.get总是抛出NullPointerException
。
Person
的元模型 class Person_
由 org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor
生成。
在 JPA/Hibernate Static Metamodel Attributes not Populated -- NullPointerException 中提出了类似的问题,但这次两个 class 都在同一个包中。
堆栈跟踪:
java.lang.NullPointerException
at org.hibernate.jpa.criteria.path.AbstractPathImpl.get(AbstractPathImpl.java:123)
我的代码:
我用来确保他们有 getId();
.
的界面
package it.unibz.db.hibernate.model;
public interface ModelInterface<PK extends Serializable> extends Serializable {
PK getId();
}
型号class
package it.unibz.db.hibernate.model;
@Entity
@Table(name ="person")
public class Person implements ModelInterface<Integer> {
@Id
private Integer id;
private String name;
public Integer getId() {
return id;
}
//other getter and setters
}
生成的元模型 Person_ class
package it.unibz.db.hibernate.model;
@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(Person.class)
public abstract class Person_ {
public static volatile SingularAttribute<Person, String> name;
public static volatile SingularAttribute<Person, Integer> id;
}
我用 PersonDao
继承的通用 DAO class
public class GenericDao<E extends ModelInterface<PK>, PK extends Serializable> implements DaoInterface<E, PK> {
private Class<E> type;
public GenericDao(Class<E> type) {
this.type = type;
//called as super(ClassName.class); eg. super(Person.class);
}
public List<E> readBy(SingularAttribute column, String value) throws Exception {
EntityManager em = HibernateUtil.getEntityManager();
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<E> criteria = builder.createQuery(type);
Root<E> root = criteria.from(type);
criteria.select(root);
criteria.where( builder.equal( root.get(column), value ));
List<E> entityList = em.createQuery(criteria).getResultList();
em.close();
return entityList;
}
}
我的一些依赖项
hibernate-c3p0 5.0.7.Final
休眠实体管理器 5.0.7.Final
postgresql 9.4.1207.jre7
hibernate-jpamodelgen 5.0.7.Final
编辑:
运行 main 方法中的这段代码有效
EntityManager em = HibernateUtil.getEntityManager();
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Person> criteria = builder.createQuery(Person.class);
Root<Person> root = criteria.from(Person.class);
criteria.select(root);
criteria.where( builder.equal( root.get(Person_.name), "Can" ));
List<Person> entityList = em.createQuery(criteria).getResultList();
//do stuff with entityList
但是方法中包含的相同代码抛出 NullPointerException
。
public List<Person> readBy(SingularAttribute column, String value) throws Exception {
log.debug("Reading entity by");
EntityManager em = HibernateUtil.getEntityManager();
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Person> criteria = builder.createQuery(Person.class);
Root<Person> root = criteria.from(Person.class);
criteria.select(root);
criteria.where( builder.equal( root.get(column), value ));
List<Person> entityList = em.createQuery(criteria).getResultList();
em.close();
return entityList;
}
所以问题似乎是将 SingularAttribute
参数传递给方法 readBy(SingularAttribute column, String value)
。
我在 main 中测试了这段代码并打印了 false
EntityManager em = HibernateUtil.getEntityManager();
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Person> criteria = builder.createQuery(Person.class);
Root<Person> root = criteria.from(Person.class);
System.out.println(root.get(Person_.name) == null); //false
同时这抛出 InvocationTargetException
由 NullPointerException
在 root.get(column)
引起。
//invoked as personDao.test(Person_.name) from main
public void test(SingularAttribute column) {
EntityManager em = HibernateUtil.getEntityManager();
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Person> criteria = builder.createQuery(Person.class);
Root<Person> root = criteria.from(Person.class);
System.out.println(root.get(column) == null); //InvocationTargetException
}
这是它应该做的吗?如何将 SingularAttribute
对象作为参数传递给方法?
在方法调用之前在 main 中调用 HibernateUtil.getEntityManager()
以某种方式起作用。 它甚至在我真正调用一个方法的空块时也有效 init()
。
它可能与 类 的初始化有关。这是代码片段。
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();
}
}
我正在尝试将 JPA2 类型安全条件查询与 Hibernate 5.0 一起使用。7.Final。
...
criteria.where( builder.equal( root.get(SingularAttribute.attr), value ));
//where parameters are
//criteria.where( builder.equal( root.get(Person_.name), "Can" ));
...
root.get总是抛出NullPointerException
。
Person
的元模型 class Person_
由 org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor
生成。
在 JPA/Hibernate Static Metamodel Attributes not Populated -- NullPointerException 中提出了类似的问题,但这次两个 class 都在同一个包中。
堆栈跟踪:
java.lang.NullPointerException
at org.hibernate.jpa.criteria.path.AbstractPathImpl.get(AbstractPathImpl.java:123)
我的代码:
我用来确保他们有 getId();
.
package it.unibz.db.hibernate.model;
public interface ModelInterface<PK extends Serializable> extends Serializable {
PK getId();
}
型号class
package it.unibz.db.hibernate.model;
@Entity
@Table(name ="person")
public class Person implements ModelInterface<Integer> {
@Id
private Integer id;
private String name;
public Integer getId() {
return id;
}
//other getter and setters
}
生成的元模型 Person_ class
package it.unibz.db.hibernate.model;
@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(Person.class)
public abstract class Person_ {
public static volatile SingularAttribute<Person, String> name;
public static volatile SingularAttribute<Person, Integer> id;
}
我用 PersonDao
继承的通用 DAO classpublic class GenericDao<E extends ModelInterface<PK>, PK extends Serializable> implements DaoInterface<E, PK> {
private Class<E> type;
public GenericDao(Class<E> type) {
this.type = type;
//called as super(ClassName.class); eg. super(Person.class);
}
public List<E> readBy(SingularAttribute column, String value) throws Exception {
EntityManager em = HibernateUtil.getEntityManager();
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<E> criteria = builder.createQuery(type);
Root<E> root = criteria.from(type);
criteria.select(root);
criteria.where( builder.equal( root.get(column), value ));
List<E> entityList = em.createQuery(criteria).getResultList();
em.close();
return entityList;
}
}
我的一些依赖项
hibernate-c3p0 5.0.7.Final
休眠实体管理器 5.0.7.Final
postgresql 9.4.1207.jre7
hibernate-jpamodelgen 5.0.7.Final
编辑:
运行 main 方法中的这段代码有效
EntityManager em = HibernateUtil.getEntityManager();
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Person> criteria = builder.createQuery(Person.class);
Root<Person> root = criteria.from(Person.class);
criteria.select(root);
criteria.where( builder.equal( root.get(Person_.name), "Can" ));
List<Person> entityList = em.createQuery(criteria).getResultList();
//do stuff with entityList
但是方法中包含的相同代码抛出 NullPointerException
。
public List<Person> readBy(SingularAttribute column, String value) throws Exception {
log.debug("Reading entity by");
EntityManager em = HibernateUtil.getEntityManager();
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Person> criteria = builder.createQuery(Person.class);
Root<Person> root = criteria.from(Person.class);
criteria.select(root);
criteria.where( builder.equal( root.get(column), value ));
List<Person> entityList = em.createQuery(criteria).getResultList();
em.close();
return entityList;
}
所以问题似乎是将 SingularAttribute
参数传递给方法 readBy(SingularAttribute column, String value)
。
我在 main 中测试了这段代码并打印了 false
EntityManager em = HibernateUtil.getEntityManager();
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Person> criteria = builder.createQuery(Person.class);
Root<Person> root = criteria.from(Person.class);
System.out.println(root.get(Person_.name) == null); //false
同时这抛出 InvocationTargetException
由 NullPointerException
在 root.get(column)
引起。
//invoked as personDao.test(Person_.name) from main
public void test(SingularAttribute column) {
EntityManager em = HibernateUtil.getEntityManager();
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Person> criteria = builder.createQuery(Person.class);
Root<Person> root = criteria.from(Person.class);
System.out.println(root.get(column) == null); //InvocationTargetException
}
这是它应该做的吗?如何将 SingularAttribute
对象作为参数传递给方法?
在方法调用之前在 main 中调用 HibernateUtil.getEntityManager()
以某种方式起作用。 它甚至在我真正调用一个方法的空块时也有效 init()
。
它可能与 类 的初始化有关。这是代码片段。
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();
}
}