title: JPA/Hibernate Query failure related to entity variable name
body: <p>I'm new to JPA and I've spent the last 4 days trying to get to figure out why the *&amp;^!@ 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. </p>
  
  <pre class="lang-java prettyprint-override"><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&lt;String, ContainedEntity&gt; 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&lt;String, ContainedEntity&gt; getEcConfigs() {
      return ecConfigs;
  }
  
  public void setEcConfigs(Map&lt;String, ContainedEntity&gt; ecConfigs) {
      this.ecConfigs = ecConfigs;
  }
  
  public MyEntity getMyEntity() {
      return myEntity;
  }
  
  public void setMyEntity(MyEntity myEntity) {
      this.myEntity = myEntity;
  }
  
  public String toString() {
      return name + " -- " + myEntity;
  }
  }
  </code></pre>
  
  <pre class="lang-java prettyprint-override"><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></pre>
  
  <pre class="lang-java prettyprint-override"><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) &amp;&amp; pk.typeName.equals(typeName);
  }
  }
  </code></pre>
  
  <pre class="lang-java prettyprint-override"><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&lt;MyEntity&gt; q = em.createQuery("SELECT M FROM MyEntity M WHERE M.name=:name", MyEntity.class);
      q.setParameter("name", name);
      System.out.println(q.getSingleResult());
  }
  }
  </code></pre>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0"&gt;
  &lt;persistence-unit name="testPersistenceUnit" transaction-type="RESOURCE_LOCAL"&gt;
  &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
  &lt;class&gt;test.nocommit.MyEntity&lt;/class&gt;
  &lt;class&gt;test.nocommit.ContainedEntity&lt;/class&gt;
  &lt;properties&gt;
    &lt;property name="javax.persistence.jdbc.driver" value="oracle.jdbc.driver.OracleDriver"/&gt;
    &lt;property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@server:1521:instance"/&gt;
    &lt;property name="javax.persistence.jdbc.user" value="user"/&gt;
    &lt;property name="javax.persistence.jdbc.password" value="pass"/&gt;
    &lt;property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/&gt;
    &lt;property name="hibernate.hbm2ddl.auto" value="create"/&gt; 
  &lt;/properties&gt;
  &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>When I run the previous main class, it behaves as I expect and get the following output:<br>
  aEntity -- null<br>
  bEntity -- aEntity -- null  </p>
  
  <p>However, if I change the MyEntity.ecConfigs variable name to MyEntity.secConfigs and change the getters/setters to:</p>
  
  <pre class="lang-java prettyprint-override"><code>    public Map&lt;String, ContainedEntity&gt; getSecConfigs() {
          return secConfigs;
      }
  
      public void setSecConfigs(Map&lt;String, ContainedEntity&gt; secConfigs) {
          this.secConfigs = secConfigs;
      }
  </code></pre>
  
  <p>It blows up on the findEntity("bEntity"); call.  My output is:<br>
  aEntity -- null<br>
  followed by the exception:</p>
  
  <pre><code>Exception in thread "main" org.hibernate.AssertionFailure: null identifier
      at org.hibernate.engine.spi.EntityKey.&lt;init&gt;(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)
  </code></pre>
  
  <p>At first I was blaming this on Spring but I've stripped everything Spring right out and still encounter the problem.  </p>
  
  <p>My hibernate version calls itself 4.0.1.Final  </p>
  
  <p>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.</p>