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

时间:2016-01-16 04:43:28

标签: java hibernate maven jpa metamodel

我正在尝试使用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_的元模型类Personorg.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();的界面。

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

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

同时在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

这是它应该做的吗?如何将P1 from vendor name TEST1 P2 and P3 from vendor name TEST2 P4 from vendor name TEST3 P5 from vendor name TEST4 对象作为参数传递给方法?

1 个答案:

答案 0 :(得分:0)

在方法调用之前以某种方式在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();
    }
}