如何从基于JPA的条目中获取实际实体类型而不获取所有对象

时间:2017-04-07 10:45:39

标签: hibernate jpa

我需要从基于JPA的条目中获取实际的实体类型,而不需要获取所有对象。例如,有两个实体--SuperUser扩展用户。

我希望得到

id  type
1   com.company.domain.SuperUser   
2   com.company.domain.User 

情况:

用户

@Table(name = "users")
@DiscriminatorValue(value = "User")
@Inheritance(strategy= InheritanceType.SINGLE_TABLE)
@Entity
public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;

    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
}

超级用户

@DiscriminatorValue(value = "SuperUser")
@Entity
public class SuperUser extends User implements Serializable {

    private static final long serialVersionUID = 2L;
}

当前条目数据

INSERT INTO users(DTYPE, id) VALUES('SuperUser',1);
INSERT INTO users(DTYPE, id) VALUES('User',2);

我试图获取实体类和列ID但我得到NullPointerException。我做错了什么?

EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("persistence");
EntityManager entityManager = entityManagerFactory.createEntityManager();

Query query;

//Works
query = entityManager.createQuery("select type(us), us.id From User us where type(us) = 'SuperUser'");
query.getResultList();
//Works
query = entityManager.createQuery("select type(us), us.id From SuperUser us");
query.getResultList();

//Throws NPE
query = entityManager.createQuery("select type(us), us.id From User us where type(us) = 'User'");
query.getResultList();
//Throws NPE
query = entityManager.createQuery("select type(us), us.id From User us");
query.getResultList();

更新 添加stackTrace:

Exception in thread "main" java.lang.NullPointerException
    at org.hibernate.tuple.entity.PojoEntityTuplizer.determineConcreteSubclassEntityName(PojoEntityTuplizer.java:333)
    at org.hibernate.persister.entity.AbstractEntityPersister.getSubclassEntityPersister(AbstractEntityPersister.java:4581)
    at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1456)
    at org.hibernate.persister.entity.DiscriminatorType.nullSafeGet(DiscriminatorType.java:72)
    at org.hibernate.persister.entity.DiscriminatorType.nullSafeGet(DiscriminatorType.java:59)
    at org.hibernate.loader.hql.QueryLoader.getResultRow(QueryLoader.java:452)
    at org.hibernate.loader.hql.QueryLoader.getResultColumnOrRow(QueryLoader.java:435)
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:756)
    at org.hibernate.loader.Loader.processResultSet(Loader.java:972)
    at org.hibernate.loader.Loader.doQuery(Loader.java:930)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:336)
    at org.hibernate.loader.Loader.doList(Loader.java:2611)
    at org.hibernate.loader.Loader.doList(Loader.java:2594)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2423)
    at org.hibernate.loader.Loader.list(Loader.java:2418)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:501)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:371)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:220)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1268)
    at org.hibernate.internal.QueryImpl.list(QueryImpl.java:87)
    at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:567)
    at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:436)
    at com.geowarin.hibernate.jpa.standalone.App.tryFetch(App.java:40)
    at com.geowarin.hibernate.jpa.standalone.App.main(App.java:19)

0 个答案:

没有答案