gpt4 book ai didi

java - Hibernate/JPA - 访问 SingularAttribute 参数时出现 NullPointerException

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:37:15 25 4
gpt4 key购买 nike

我尝试在 Hibernate 5.0.7.Final 中使用 JPA2 类型安全条件查询。

...
criteria.where( builder.equal( root.get(SingularAttribute.attr), value ));
//where parameters are
//criteria.where( builder.equal( root.get(Person_.name), "Can" ));
...

root.get 总是抛出 NullPointerExceptionPerson 的元模型类 Person_org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor 生成。

JPA/Hibernate Static Metamodel Attributes not Populated -- NullPointerException 中提出了类似的问题但这次两个类都在同一个包中。

堆栈跟踪:

java.lang.NullPointerException
at org.hibernate.jpa.criteria.path.AbstractPathImpl.get(AbstractPathImpl.java:123)

我的代码:

我用来确保它们具有 getId(); 的接口(interface)。

package it.unibz.db.hibernate.model;

public interface ModelInterface<PK extends Serializable> extends Serializable {
PK getId();
}

模型类

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_类

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 类

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

  • hibernate-entitymanager 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

同时这会在 root.get(column) 处抛出 InvocationTargetExceptionNullPointerException 引起。

//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() 的一个空 block 时也有效。它可能与类的初始化有关。这是代码片段。

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

关于java - Hibernate/JPA - 访问 SingularAttribute 参数时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34823602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com