java - JPA/Hibernate Query failure related to entity variable name -
i'm new jpa , i've spent last 4 days trying figure out why *&^!@ queries keep blowing up. after ado seems problem somehow related name of variable i'm using. see entities , test class below. apologies enormopost i've tried simplify as possible.
@entity public class myentity { @id @generatedvalue(strategy = generationtype.sequence) private long id; @column(length = 50) private string name; @manytoone private myentity myentity; @onetomany(cascade = cascadetype.all, mappedby = "myent", fetch = fetchtype.eager) @mapkey(name = "typename") private map<string, containedentity> ecconfigs; public myentity() { } public myentity(string name, myentity myentity) { this.name = name; this.myentity = myentity; } public myentity(string name) { this.name = name; } public string getname() { return name; } public void setname(string name) { this.name = name; } public long getid() { return id; } public void setid(long id) { this.id = id; } public map<string, containedentity> getecconfigs() { return ecconfigs; } public void setecconfigs(map<string, containedentity> ecconfigs) { this.ecconfigs = ecconfigs; } public myentity getmyentity() { return myentity; } public void setmyentity(myentity myentity) { this.myentity = myentity; } public string tostring() { return name + " -- " + myentity; } }
@idclass(containedentitypk.class) @entity public class containedentity { @id private string typename; @id private myentity myent; @column(length = 255) private string cename; public containedentity() { } public containedentity(string typename, string cename) { super(); this.typename = typename; this.cename = cename; } public string gettypename() { return typename; } public void settypename(string typename) { this.typename = typename; } public myentity getmyent() { return myent; } public void setmyent(myentity myent) { this.myent = myent; } public string getcename() { return cename; } public void setcename(string cename) { this.cename = cename; } }
public class containedentitypk implements serializable{ private static final long serialversionuid = -1714218588564578557l; @column(length = 255) private string typename; @manytoone private myentity myent; public containedentitypk() { } public containedentitypk(string typename, myentity myent) { super(); this.typename = typename; this.myent = myent; } public string gettypename() { return typename; } public void settypename(string name) { this.typename = name; } public myentity getmyent() { return myent; } public void setmyent(myentity myent) { this.myent = myent; } public int hashcode() { return (int) (typename.hashcode() + myent.hashcode()); } public boolean equals(object obj) { if (obj == null) return false; if (obj == this) return true; if (!(obj instanceof containedentitypk)) return false; containedentitypk pk = (containedentitypk) obj; return pk.getmyent().equals(myent) && pk.typename.equals(typename); } }
public class tester { static entitymanagerfactory emf = persistence.createentitymanagerfactory("testpersistenceunit"); /** * @param args */ public static void main(string[] args) { addentities(); findentity("aentity"); findentity("bentity"); } public static void addentities() { entitymanager em = emf.createentitymanager(); myentity aentity = new myentity("aentity"); myentity bentity = new myentity("bentity", aentity); em.gettransaction().begin(); em.persist(aentity); em.persist(bentity); em.gettransaction().commit(); em.close(); } public static void findentity(string name) { entitymanager em = emf.createentitymanager(); typedquery<myentity> q = em.createquery("select m myentity m m.name=:name", myentity.class); q.setparameter("name", name); system.out.println(q.getsingleresult()); } }
<?xml version="1.0" encoding="utf-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0"> <persistence-unit name="testpersistenceunit" transaction-type="resource_local"> <provider>org.hibernate.ejb.hibernatepersistence</provider> <class>test.nocommit.myentity</class> <class>test.nocommit.containedentity</class> <properties> <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.driver.oracledriver"/> <property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@server:1521:instance"/> <property name="javax.persistence.jdbc.user" value="user"/> <property name="javax.persistence.jdbc.password" value="pass"/> <property name="hibernate.dialect" value="org.hibernate.dialect.oracle10gdialect"/> <property name="hibernate.hbm2ddl.auto" value="create"/> </properties> </persistence-unit> </persistence>
when run previous main class, behaves expect , following output:
aentity -- null
bentity -- aentity -- null
however, if change myentity.ecconfigs variable name myentity.secconfigs , change getters/setters to:
public map<string, containedentity> getsecconfigs() { return secconfigs; } public void setsecconfigs(map<string, containedentity> secconfigs) { this.secconfigs = secconfigs; }
it blows on findentity("bentity"); call. output is:
aentity -- null
followed exception:
exception in thread "main" org.hibernate.assertionfailure: null identifier @ org.hibernate.engine.spi.entitykey.<init>(entitykey.java:69) @ org.hibernate.internal.abstractsessionimpl.generateentitykey(abstractsessionimpl.java:240) @ org.hibernate.loader.loader.extractkeysfromresultset(loader.java:722) @ org.hibernate.loader.loader.getrowfromresultset(loader.java:635) @ org.hibernate.loader.loader.doquery(loader.java:856) @ org.hibernate.loader.loader.doqueryandinitializenonlazycollections(loader.java:289) @ org.hibernate.loader.loader.doqueryandinitializenonlazycollections(loader.java:259) @ org.hibernate.loader.loader.loadentity(loader.java:2058) @ org.hibernate.loader.entity.abstractentityloader.load(abstractentityloader.java:82) @ org.hibernate.loader.entity.abstractentityloader.load(abstractentityloader.java:72) @ org.hibernate.persister.entity.abstractentitypersister.load(abstractentitypersister.java:3697) @ org.hibernate.event.internal.defaultloadeventlistener.loadfromdatasource(defaultloadeventlistener.java:439) @ org.hibernate.event.internal.defaultloadeventlistener.doload(defaultloadeventlistener.java:420) @ org.hibernate.event.internal.defaultloadeventlistener.load(defaultloadeventlistener.java:204) @ org.hibernate.event.internal.defaultloadeventlistener.proxyorload(defaultloadeventlistener.java:251) @ org.hibernate.event.internal.defaultloadeventlistener.onload(defaultloadeventlistener.java:148) @ org.hibernate.internal.sessionimpl.fireload(sessionimpl.java:954) @ org.hibernate.internal.sessionimpl.internalload(sessionimpl.java:903) @ org.hibernate.type.entitytype.resolveidentifier(entitytype.java:610) @ org.hibernate.type.entitytype.resolve(entitytype.java:438) @ org.hibernate.engine.internal.twophaseload.initializeentity(twophaseload.java:150) @ org.hibernate.loader.loader.initializeentitiesandcollections(loader.java:1006) @ org.hibernate.loader.loader.doquery(loader.java:883) @ org.hibernate.loader.loader.doqueryandinitializenonlazycollections(loader.java:289) @ org.hibernate.loader.loader.dolist(loader.java:2463) @ org.hibernate.loader.loader.dolist(loader.java:2449) @ org.hibernate.loader.loader.listignorequerycache(loader.java:2279) @ org.hibernate.loader.loader.list(loader.java:2274) @ org.hibernate.loader.hql.queryloader.list(queryloader.java:470) @ org.hibernate.hql.internal.ast.querytranslatorimpl.list(querytranslatorimpl.java:355) @ org.hibernate.engine.query.spi.hqlqueryplan.performlist(hqlqueryplan.java:196) @ org.hibernate.internal.sessionimpl.list(sessionimpl.java:1115) @ org.hibernate.internal.queryimpl.list(queryimpl.java:101) @ org.hibernate.ejb.queryimpl.getsingleresult(queryimpl.java:280) @ test.nocommit.tester.findentity(tester.java:38) @ test.nocommit.tester.main(tester.java:17)
at first blaming on spring i've stripped spring right out , still encounter problem.
my hibernate version calls 4.0.1.final
am missing here? there variable name requirements? stepping on reserved words? not specifying correctly? can tell, there may relationship self-referencing entity composite primary key......i'm baffled , crying softly in cubicle.
the beauty of jpa it's specification, not implementation. code looks it's written against jpa spec, not hibernate.
i've found eclipselink has much better description in it's error messages when messed up. i'd try running code using eclipselink , see if better error message.
i'll keep looking @ code until see if can spot what's wrong...
Comments
Post a Comment