Featured post
jpa 2.0 - JPA 2 Criteria API - Exception converting JPQL to Criteria API query with Eclipselink -
i'm having trouble converting jpa query use criteria api
i have following query attempts find serviceuser matching passed address parameter. serviceuser abstract entity concrete subclasses child , adult. i'm using joined inheritance strategy on serviceuser class.
everything works fine when call method.
public set<t> findbyaddress(address address) { query query = manager.createquery("select distinct s serviceuser s join s.addresshistory h h = :address"); query.setparameter("address", address); hashset<t> result = new hashset<t>(); (object s : query.getresultlist()) { // tclass runtime type of generic session bean. if (this.tclass.isinstance(s)) { result.add((t) s); } } return result; }
but, if try replace function following uses generated metamodel , typedquery getting exception
public set<t> findbyaddress(address address) { criteriabuilder cb = manager.getcriteriabuilder(); criteriaquery<serviceuser> cq = cb.createquery(serviceuser.class); root<serviceuser> su = cq.from(serviceuser.class); join<serviceuser, address> addresshistory = su.join(serviceuser_.addresshistory); cq.select(su).distinct(true).where(cb.equal(addresshistory, cb.parameter(address.class, "address"))); typedquery<serviceuser> q = manager.createquery(cq); q.setparameter("address", address); hashset<t> result = new hashset<t>(); (serviceuser s : q.getresultlist()) { // tclass runtime type of generic session bean. if (this.tclass.isinstance(s)) { result.add((t) s); } } return result; }
here stacktrace eclipslink logging. there seems nulpointerexception being thrown i'm not sure why?
/** test : shouldbeabletofindchildbyaddress() uk.gov.sunderland.ccms.model.bean.childservice.findbyaddress(address[id=1,lineone=7,linetwo=camberwell way,linethree=moorside,linefour=sunderland,postcode=sr7 3xn]) [el finer]: 2010-12-20 12:39:42.89--serversession(7055953)--thread(thread[pool-52-thread-1,5,grizzly-kernel])--client acquired [el finer]: 2010-12-20 12:39:42.89--unitofwork(22763294)--thread(thread[pool-52-thread-1,5,grizzly-kernel])--tx binding tx mgr, status=status_active [el finest]: 2010-12-20 12:39:42.89--unitofwork(22763294)--thread(thread[pool-52-thread-1,5,grizzly-kernel])--execute query readobjectquery(referenceclass=address sql="select id, line_two, line_three, line_one, line_four, postcode address (id = ?)") [el finest]: 2010-12-20 12:39:42.906--unitofwork(22763294)--thread(thread[pool-52-thread-1,5,grizzly-kernel])--execute query readallquery(referenceclass=serviceuser ) [el finest]: 2010-12-20 12:39:42.906--serversession(7055953)--thread(thread[pool-52-thread-1,5,grizzly-kernel])--reconnecting external connection pool [el fine]: 2010-12-20 12:39:42.906--serversession(7055953)--connection(32184426)--thread(thread[pool-52-thread-1,5,grizzly-kernel])--select distinct t2.service_user_type address t0, service_user t2, service_user_address t1 ((? = t0.id) , ((t1.service_user_id = t2.id) , (t0.id = t1.address_id))) bind => [1] [el finer]: 2010-12-20 12:39:42.906--unitofwork(22763294)--thread(thread[pool-52-thread-1,5,grizzly-kernel])--class uk.gov.sunderland.ccms.model.entity.adult [el finest]: 2010-12-20 12:39:42.906--serversession(7055953)--thread(thread[pool-52-thread-1,5,grizzly-kernel])--reconnecting external connection pool [el fine]: 2010-12-20 12:39:42.906--serversession(7055953)--connection(29495316)--thread(thread[pool-52-thread-1,5,grizzly-kernel])--select distinct t0.id, t0.service_user_type, t0.title, t0.dob, t0.gender, t0.disability, t0.ethnicity, t1.id, t1.employment, t1.info_pack_required, t1.home_phone, t1.other_language, t1.referrer, t1.mobile_phone, t1.lone_parent, t1.gp_practice, t1.contactable, t1.work_phone, t1.baby_due_date, t1.english_level service_user_address t3, address t2, adult t1, service_user t0 (((? = t2.id) , ((t1.id = t0.id) , (t0.service_user_type = ?))) , ((t3.service_user_id = t0.id) , (t2.id = t3.address_id))) bind => [1, 1] [el warning]: 2010-12-20 12:39:42.906--unitofwork(22763294)--thread(thread[pool-52-thread-1,5,grizzly-kernel])--java.lang.nullpointerexception @ org.eclipse.persistence.descriptors.inheritancepolicy.selectallrowusingdefaultmultipletablesubclassread(inheritancepolicy.java:1355) @ org.eclipse.persistence.descriptors.inheritancepolicy.selectallrowusingmultipletablesubclassread(inheritancepolicy.java:1396) @ org.eclipse.persistence.internal.queries.expressionquerymechanism.selectallrows(expressionquerymechanism.java:2493) @ org.eclipse.persistence.queries.readallquery.executeobjectlevelreadquery(readallquery.java:407) @ org.eclipse.persistence.queries.objectlevelreadquery.executedatabasequery(objectlevelreadquery.java:1074) @ org.eclipse.persistence.queries.databasequery.execute(databasequery.java:736) @ org.eclipse.persistence.queries.objectlevelreadquery.execute(objectlevelreadquery.java:1034) @ org.eclipse.persistence.queries.readallquery.execute(readallquery.java:380) @ org.eclipse.persistence.queries.objectlevelreadquery.executeinunitofwork(objectlevelreadquery.java:1112) @ org.eclipse.persistence.internal.sessions.unitofworkimpl.internalexecutequery(unitofworkimpl.java:2909) @ org.eclipse.persistence.internal.sessions.abstractsession.executequery(abstractsession.java:1291) @ org.eclipse.persistence.internal.sessions.abstractsession.executequery(abstractsession.java:1273) @ org.eclipse.persistence.internal.sessions.abstractsession.executequery(abstractsession.java:1247) @ org.eclipse.persistence.internal.jpa.ejbqueryimpl.executereadquery(ejbqueryimpl.java:479) @ org.eclipse.persistence.internal.jpa.ejbqueryimpl.getresultlist(ejbqueryimpl.java:714) */
looking @ sql gets issued failing criteria api query , comparing sql gets issued when perform query jpa string query.
criteria query
select distinct t0.id, t0.service_user_type, t0.title, t0.dob, t0.gender, t0.disability, t0.ethnicity, t1.id, t1.employment, t1.info_pack_required, t1.home_phone, t1.other_language, t1.referrer, t1.mobile_phone, t1.lone_parent, t1.gp_practice, t1.contactable, t1.work_phone, t1.baby_due_date, t1.english_level service_user_address t3, address t2, adult t1, service_user t0 (((? = t2.id) , ((t1.id = t0.id) , (t0.service_user_type = ?))) , ((t3.service_user_id = t0.id) , (t2.id = t3.address_id)))
jpa string query
select distinct t0.id, t0.service_user_type, t0.title, t0.dob, t0.gender, t0.disability, t0.ethnicity, t1.id, t1.employment, t1.info_pack_required, t1.home_phone, t1.other_language, t1.referrer, t1.mobile_phone, t1.lone_parent, t1.gp_practice, t1.contactable, t1.work_phone, t1.baby_due_date, t1.english_level service_user_address t3, address t2, adult t1, service_user t0 (((? = t2.id) , ((t1.id = t0.id) , (t0.service_user_type = ?))) , ((t3.service_user_id = t0.id) , (t2.id = t3.address_id)))
and
select distinct t0.id, t0.service_user_type, t0.title, t0.dob, t0.gender, t0.disability, t0.ethnicity, t1.id service_user_address t3, address t2, child t1, service_user t0 (((? = t2.id) , ((t1.id = t0.id) , (t0.service_user_type = ?))) , ((t3.service_user_id = t0.id) , (t2.id = t3.address_id)))
i can see first part deals adult subclass of serviceuser identical in both cases when comes child subclass criteria query throws
[el warning]: 2010-12-20 )--java.lang.nullpointerexception @ org.eclipse.persistence.descriptors.inheritancepolicy.selectallrowusingdefaultmultipletablesubclassread(inheritancepolicy.java:1355)
could bug eclipselink?
yes indeed appears bug eclipselink
<dependency> <groupid>org.eclipse.persistence</groupid> <artifactid>eclipselink</artifactid> <version>2.1.1</version> <scope>provided</scope> </dependency>
changing inheritance strategy
@inheritance(strategy = inheritancetype.joined)
to
@inheritance(strategy = inheritancetype.single_table)
on abstract base class service_user provides workaround in case ok.
- Get link
- X
- Other Apps
Comments
Post a Comment