2

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.

@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 FROM MyEntity M WHERE 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 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:

    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:

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

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......I'm currently baffled and crying softly in my cubicle.

2
  • Works fine on DataNucleus JPA (with ecConfigs or secConfigs). I see no reserved words anywhere, and besides if something is a reserved word in an RDBMS then it can easily be quoted by the JPA implementation. Note that you haven't specified a sequence name in the GeneratedValue, so need to add that (maybe it was simply missed off the post yet you really have it?) Commented Mar 10, 2012 at 9:24
  • OP's bug report: hibernate.atlassian.net/browse/HHH-7178 Commented Jan 2, 2015 at 15:43

2 Answers 2

1

The beauty of JPA is that it's a specification, not an implementation. Your code looks like it's written against the JPA spec, not hibernate.

I've found that EclipseLink has much better description in it's error messages for when you messed up. I'd try running your code using EclipseLink and see if you get a better error message.

I'll keep looking at your code until then see if I can spot what's wrong...

Sign up to request clarification or add additional context in comments.

3 Comments

Interesting...I took your advice, and EcliplseLink seems to have a beef with it...but a different beef altogether.
'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].' Much better error message. It's obviously mad about the PK so I changed the myEnt type to long and it works in EclipseLink. Is that how a complex type in a PK is supposed to be defined? Also, I changed back to hibernate with this pk and it works until I try to persist a MyEntity containing a ContainedEntity.
'Caused by: java.lang.IllegalArgumentException: Can not set long field test.nocommit.ContainedEntityPK.myEnt to test.nocommit.MyEntity' So, Hibernate kinda likes it that way until I use it, then it explicitly doesn't like it...argh. Side note...it seems I'm married to Hibernate as declared by my overlords to maintain consistency with the rest of the product set.
0

Well, This has not an easy solution, I've faced today the same issue with two Pojos ( A and B), both have a java.utl.Long field as @Id. The A pojo has a field which is a collection to another entity.

My load process similar to this: A -> B -> B.c

The load process was crashing because of c, loading c was throwing an Missing param IN/OUT, but this was hidden behind the reported exception.

In my case i was able to fix the problem because detaching c from B Using a fetchmode 'SELECT' instead of 'SUBSELECT'. But i loosed 8 hour searching for info. My hibernate version is 3.6.10-Final

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.