hibernate - How to get actual entity type from JPA based entries without fetching all object -
i need actual entity type jpa based entries without fetching object. example there 2 entities - superuser extends user.
and want
id type 1 com.company.domain.superuser 2 com.company.domain.user
situation:
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; } }
super user
@discriminatorvalue(value = "superuser") @entity public class superuser extends user implements serializable { private static final long serialversionuid = 2l; }
current entries data
insert users(dtype, id) values('superuser',1); insert users(dtype, id) values('user',2);
i'm trying entity class , column id nullpointerexception. i'm doing wrong?
entitymanagerfactory entitymanagerfactory = persistence.createentitymanagerfactory("persistence"); entitymanager entitymanager = entitymanagerfactory.createentitymanager(); query query; //works query = entitymanager.createquery("select type(us), us.id user type(us) = 'superuser'"); query.getresultlist(); //works query = entitymanager.createquery("select type(us), us.id superuser us"); query.getresultlist(); //throws npe query = entitymanager.createquery("select type(us), us.id user type(us) = 'user'"); query.getresultlist(); //throws npe query = entitymanager.createquery("select type(us), us.id user us"); query.getresultlist();
update: adding stacktrace:
exception in thread "main" java.lang.nullpointerexception @ org.hibernate.tuple.entity.pojoentitytuplizer.determineconcretesubclassentityname(pojoentitytuplizer.java:333) @ org.hibernate.persister.entity.abstractentitypersister.getsubclassentitypersister(abstractentitypersister.java:4581) @ org.hibernate.internal.sessionimpl.getentitypersister(sessionimpl.java:1456) @ org.hibernate.persister.entity.discriminatortype.nullsafeget(discriminatortype.java:72) @ org.hibernate.persister.entity.discriminatortype.nullsafeget(discriminatortype.java:59) @ org.hibernate.loader.hql.queryloader.getresultrow(queryloader.java:452) @ org.hibernate.loader.hql.queryloader.getresultcolumnorrow(queryloader.java:435) @ org.hibernate.loader.loader.getrowfromresultset(loader.java:756) @ org.hibernate.loader.loader.processresultset(loader.java:972) @ org.hibernate.loader.loader.doquery(loader.java:930) @ org.hibernate.loader.loader.doqueryandinitializenonlazycollections(loader.java:336) @ org.hibernate.loader.loader.dolist(loader.java:2611) @ org.hibernate.loader.loader.dolist(loader.java:2594) @ org.hibernate.loader.loader.listignorequerycache(loader.java:2423) @ org.hibernate.loader.loader.list(loader.java:2418) @ org.hibernate.loader.hql.queryloader.list(queryloader.java:501) @ org.hibernate.hql.internal.ast.querytranslatorimpl.list(querytranslatorimpl.java:371) @ org.hibernate.engine.query.spi.hqlqueryplan.performlist(hqlqueryplan.java:220) @ org.hibernate.internal.sessionimpl.list(sessionimpl.java:1268) @ org.hibernate.internal.queryimpl.list(queryimpl.java:87) @ org.hibernate.jpa.internal.queryimpl.list(queryimpl.java:567) @ org.hibernate.jpa.internal.queryimpl.getresultlist(queryimpl.java:436) @ com.geowarin.hibernate.jpa.standalone.app.tryfetch(app.java:40) @ com.geowarin.hibernate.jpa.standalone.app.main(app.java:19)
Comments
Post a Comment