Getting nullpointer exception while executing query in JPA using Hibernate 5.1.2 as JPA vendor -
i'm trying upgrade application openjpa implementation 1.0.1 using jpa 1.0 hibernate 5.1.2
i'm getting nullpointer exception when trying to call geresultlist on entity class.
my sample classes below: (i've changed class names can not share original source code. mapping same.)
1) class charger (representing sort of mobile charger has 2 pin)
@entity @table(name = "charger") class chargerentityext5 extends chargerentity1abstract implements serializable { @transient private static final long serialversionuid = -1l; @onetomany(mappedby="chargerentityext5",targetentity=portentityext5.class, fetch=fetchtype.eager) @fetch(value = fetchmode.subselect) public list<pinentityext5> pinentityextlist; public list<pinentityext5> getpinentityextlist() { return pinentityextlist; } public void setpinentityextlist(list<pinentityext5> pinentityextlist) { this.pinentityextlist = pinentityextlist; } }
2)
@mappedsuperclass class chargerentity1abstract implements serializable { private static final long serialversionuid = 1l; @id @column(name="id") public long id; @column(name="charger_id") public string chargerid; // *** constructor **** public chargerentity1abstract() { } @override public string tostring() { return "chargerentity [cpeid=" + id!=null? id.tostring():"" + ", chargerid=" + chargerid!=null? chargerid.tostring():"" + "]"; } public long getid() { return id; } public void setid(long id) { id = id; } public string getchargerid() { return chargerid; } public void setchargerid(string chargerid) { this.chargerid = chargerid; } }
3) every charger has 2 pins i've declared bi-directional 1 many mapping between charger , pin.
@entity @table(name = "pin") class pinentityext5 extends pinentity1abstract implements serializable{ @transient private static final long serialversionuid = -1l; @manytoone(optional=false) @notfound(action = notfoundaction.ignore) @joincolumn(name="charger_id", referencedcolumnname="charger_id", insertable= false, updatable = false) private chargerentityext5 chargerentityext5; public chargerentityext5 getchargerentityext5() { return chargerentityext5; } public void setchargerentityext5(chargerentityext5 chargerentityext5) { this.chargerentityext5 = chargerentityext5; } }
4) pin entity has 2 properties. charger_id , pin_number. 2 field defines 1 record in pin table uniquely. i've declared idclass (chargerpin) representing composite key
@mappedsuperclass @idclass(chargerpin.class) class pinentity1abstract implements serializable{ @transient private static final long serialversionuid = 1l; @id @column(name="charger_id", nullable = true) public string chargerid; @id @column(name="pin_number", nullable = true) public long pin; public pinentity1abstract() { } @override public string tostring() { return "pinentity [chargerid=" + chargerid + ", port=" + pin!=null? pin.tostring():"" + "]"; } public string getchargerid() { return chargerid; } public void setchargerid(string chargerid) { this.chargerid = chargerid; } public long getpin() { return pin; } public void setpin(long pin) { this.pin = pin; } } class chargerpin implements java.io.serializable{ @transient private static final long serialversionuid = 1l; private string chargerid; private long pin; // **** constructor ***** public chargerpin() { } public chargerpin(string kapid, long port) { this.chargerid = kapid; this.pin = port; } @override public string tostring() { return "kapid [chargerid=" + chargerid!=null? chargerid.tostring():"" + ", port=" + pin!=null? pin.tostring():"" + "]"; } @override public int hashcode() { final int prime = 31; int result = 1; int kap = chargerid != null ?(integer.parseint(chargerid)):0; int kap_port = pin!=null?(pin).intvalue():0; result = prime * result + kap; result = prime * result + kap_port; return result; } @override public boolean equals(object obj) { //return ((o instanceof cpeport) && (kapid.equalsignorecase(((cpeport) o).getkapid())) && (port == (((cpeport) o).getport()))); if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof cpeport1)) return false; chargerpin other = (chargerpin) obj; if (chargerid == null) { if (other.chargerid != null) return false; } else if ((other.chargerid != null) && chargerid.equals(other.chargerid)) { return false; } if (pin == null) { if (other.pin != null) return false; } else if ((other.pin != null) && (pin != other.pin)) return false; return true; } public string getchargerid() { return chargerid; } public void setchargerid(string chargerid) { this.chargerid = chargerid; } public long getpin() { return pin; } public void setpin(long pin) { this.pin = pin; } }
5) while i'm invoking below query i'm getting nullpointer exception empty satck trace.
public class hibernatetestclass { /** * @param args */ public static entitymanagerfactory entitymanagerfactory; public static void main(string[] args) { // todo auto-generated method stub entitymanager em = null; list<chargerentityext5> chargerlist = null; query query = null; string querystring = "select ce chargerentityext5 ce ce.chargerid=?1"; try { try { em = createentitymanager(); } catch (exception e) { system.out.println(e); } query = em.createquery(querystring); query.setparameter(1, "110284026"); chargerlist = query.getresultlist(); system.out.println(chargerlist.size()); } catch (illegalstateexception e) { system.out.println(e); } catch (illegalargumentexception e) { system.out.println(e); } catch (exception e) { system.out.println(e); } finally{ em.close(); } } public static entitymanager createentitymanager() { entitymanagerfactory = persistence.createentitymanagerfactory("polcaincapu"); return entitymanagerfactory.createentitymanager(); } }
can please bug. earlier trying hibernate 5.1.1. version , code throwing error "error accessing field reflection persistent property". changed code base use hibernate version 5.1.2 , without change of source code exception got changed nullpointer exception.
down vote
this full stack trace of exception. know why coming @ all:
java.lang.nullpointerexception @ chargerentity1abstract.tostring(embeddedidtester.java:462) @ org.hibernate.pretty.messagehelper.addidtocollectioninfostring(messagehelper.java:362) @ org.hibernate.pretty.messagehelper.collectioninfostring(messagehelper.java:333) @ org.hibernate.loader.plan.exec.process.internal.collectionreferenceinitializerimpl.finishuprow(collectionreferenceinitializerimpl.java:62) @ org.hibernate.loader.plan.exec.process.internal.abstractrowreader.readrow(abstractrowreader.java:121) @ org.hibernate.loader.plan.exec.process.internal.resultsetprocessorimpl.extractresults(resultsetprocessorimpl.java:122) @ org.hibernate.loader.plan.exec.internal.abstractloadplanbasedloader.executeload(abstractloadplanbasedloader.java:122) @ org.hibernate.loader.plan.exec.internal.abstractloadplanbasedloader.executeload(abstractloadplanbasedloader.java:86) @ org.hibernate.loader.collection.plan.abstractloadplanbasedcollectioninitializer.initialize(abstractloadplanbasedcollectioninitializer.java:88) @ org.hibernate.persister.collection.abstractcollectionpersister.initialize(abstractcollectionpersister.java:688) @ org.hibernate.event.internal.defaultinitializecollectioneventlistener.oninitializecollection(defaultinitializecollectioneventlistener.java:75) @ org.hibernate.internal.sessionimpl.initializecollection(sessionimpl.java:2004) @ org.hibernate.collection.internal.abstractpersistentcollection$4.dowork(abstractpersistentcollection.java:567) @ org.hibernate.collection.internal.abstractpersistentcollection.withtemporarysessionifneeded(abstractpersistentcollection.java:249) @ org.hibernate.collection.internal.abstractpersistentcollection.initialize(abstractpersistentcollection.java:563) @ org.hibernate.collection.internal.abstractpersistentcollection.forceinitialization(abstractpersistentcollection.java:731) @ org.hibernate.engine.internal.statefulpersistencecontext.initializenonlazycollections(statefulpersistencecontext.java:918) @ org.hibernate.loader.plan.exec.internal.abstractloadplanbasedloader.executeload(abstractloadplanbasedloader.java:149) @ org.hibernate.loader.plan.exec.internal.abstractloadplanbasedloader.executeload(abstractloadplanbasedloader.java:86) @ org.hibernate.loader.entity.plan.abstractloadplanbasedentityloader.load(abstractloadplanbasedentityloader.java:167) @ org.hibernate.persister.entity.abstractentitypersister.load(abstractentitypersister.java:4002) @ org.hibernate.event.internal.defaultloadeventlistener.loadfromdatasource(defaultloadeventlistener.java:508) @ org.hibernate.event.internal.defaultloadeventlistener.doload(defaultloadeventlistener.java:478) @ org.hibernate.event.internal.defaultloadeventlistener.load(defaultloadeventlistener.java:219) @ org.hibernate.event.internal.defaultloadeventlistener.proxyorload(defaultloadeventlistener.java:278) @ org.hibernate.event.internal.defaultloadeventlistener.doonload(defaultloadeventlistener.java:121) @ org.hibernate.event.internal.defaultloadeventlistener.onload(defaultloadeventlistener.java:89) @ org.hibernate.internal.sessionimpl.fireload(sessionimpl.java:1142) @ org.hibernate.internal.sessionimpl.access$2600(sessionimpl.java:167) @ org.hibernate.internal.sessionimpl$identifierloadaccessimpl.doload(sessionimpl.java:2762) @ org.hibernate.internal.sessionimpl$identifierloadaccessimpl.load(sessionimpl.java:2741) @ org.hibernate.internal.sessionimpl.get(sessionimpl.java:978) @ org.hibernate.jpa.spi.abstractentitymanagerimpl.find(abstractentitymanagerimpl.java:1075) @ org.hibernate.jpa.spi.abstractentitymanagerimpl.find(abstractentitymanagerimpl.java:1033) @ dk.tdc.visitation.polcalink.resource.dbresource.polcadb.inca.servingfp.embeddedidtester.main(embeddedidtester.java:47)
Comments
Post a Comment