I'm new to JPA and I've spent the last 4 days trying to get to figure out why the *&^!@ my queries keep blowing up. After much ado it seems the problem is somehow related to the name of the variable I'm using. See my entities and test class below. My apologies for the enormopost but I've tried to simplify it as much as possible.
Code:
@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;
}
}
Code:
@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;
}
}
Code:
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);
}
}
Code:
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 FROM MyEntity M WHERE M.name=:name", MyEntity.class);
q.setParameter("name", name);
System.out.println(q.getSingleResult());
}
}
Code:
<?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 I run the previous main class, it behaves as I expect and get the following output:
aEntity -- null
bEntity -- aEntity -- null
However, if I change the MyEntity.ecConfigs variable name to MyEntity.secConfigs and change the getters/setters to:
Code:
public Map<String, ContainedEntity> getSecConfigs() {
return secConfigs;
}
public void setSecConfigs(Map<String, ContainedEntity> secConfigs) {
this.secConfigs = secConfigs;
}
It blows up on the findEntity("bEntity"); call. My output is:
aEntity -- null
followed by the exception:
Quote:
Exception in thread "main" org.hibernate.AssertionFailure: null identifier
at org.hibernate.engine.spi.EntityKey.<init>(EntityKey.java:69)
at org.hibernate.internal.AbstractSessionImpl.generateEntityKey(AbstractSessionImpl.java:240)
at org.hibernate.loader.Loader.extractKeysFromResultSet(Loader.java:722)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:635)
at org.hibernate.loader.Loader.doQuery(Loader.java:856)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
at org.hibernate.loader.Loader.loadEntity(Loader.java:2058)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:82)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:72)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3697)
at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:439)
at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:420)
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:204)
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:251)
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:148)
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:954)
at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:903)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:610)
at org.hibernate.type.EntityType.resolve(EntityType.java:438)
at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:150)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1006)
at org.hibernate.loader.Loader.doQuery(Loader.java:883)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289)
at org.hibernate.loader.Loader.doList(Loader.java:2463)
at org.hibernate.loader.Loader.doList(Loader.java:2449)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2279)
at org.hibernate.loader.Loader.list(Loader.java:2274)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:470)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:355)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1115)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:280)
at test.nocommit.Tester.findEntity(Tester.java:38)
at test.nocommit.Tester.main(Tester.java:17)
At first I was blaming this on Spring but I've stripped everything Spring right out and still encounter the problem.
My hibernate version calls itself 4.0.1.Final
I posted this on another forum and someone suggested I try EclipseLink. So I did that hoping to at least get a more meaningful error message...which I did:
Quote:
Exception Description: The derived composite primary key attribute [myEnt] of type [test.nocommit.MyEntity] from [test.nocommit.ContainedEntityPK] should be of the same type as its parent id field from [test.nocommit.MyEntity]. That is, it should be of type [java.lang.Long].
So I changed the ContainedEntityPK.myEnt to type Long and that worked for EclipseLink. I changed back to Hibernate and while it doesn't fail on the query anymore, if I try to actually add a ContainedEntity to a MyEntity and persist it, it throws an error about the Long type for the composite key.
Am I missing something here? Is there some variable name requirements? Am I stepping on some reserved words? Am I not specifying something correctly? From what I can tell, there may be some relationship to the self-referencing entity as well as the composite primary key. Is this a Hibernate bug?