[hibernate-issues] [Hibernate-JIRA] Created: (HHH-6555) IdClass and mappedBy problem

Piotr Gliźniewicz (JIRA) noreply at atlassian.com
Mon Aug 8 03:08:03 EDT 2011


IdClass and mappedBy problem
----------------------------

                 Key: HHH-6555
                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-6555
             Project: Hibernate Core
          Issue Type: Bug
    Affects Versions: 4.0.0.Beta4, 3.6.6, 4.0.0.Beta2
         Environment: Derby 10.8.1.2
            Reporter: Piotr Gliźniewicz


I have the following classes:
{code}@Entity
public class SimpleEntity {
    @Id
    private int id;
    @OneToOne(cascade = CascadeType.ALL, mappedBy = "entityA")
    private EntityConnection connection;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "entityB")
    private List<EntityConnection> otherConnections;
    // accessors
}{code}
{code}@Entity
@IdClass(EntityConnectionPK.class)
public class EntityConnection {
    @Id
    @JoinColumn(name = "entity_a_id")
    @OneToOne
    private SimpleEntity entityA;
    @Id
    @JoinColumn(name = "entity_b_id")
    @ManyToOne
    private SimpleEntity entityB;
    // accessors
}{code}
{code}public class EntityConnectionPK implements Serializable {
    private int entityA;
    private int entityB;
    // accessors, equals, hashcode
}{code}

Executing the following query:
{code}
TypedQuery<SimpleEntity> q = em.createQuery("select e from SimpleEntity e where e.id = 1", SimpleEntity.class);
SimpleEntity foundEntity = q.getResultList().get(0);
{code}
results in:
{code}Exception in thread "main" java.lang.NullPointerException
	at org.hibernate.persister.entity.AbstractEntityPersister.loadByUniqueKey(AbstractEntityPersister.java:2200)
	at org.hibernate.type.EntityType.loadByUniqueKey(EntityType.java:661)
	at org.hibernate.type.EntityType.resolve(EntityType.java:441)
	at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:150)
	at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1012)
	at org.hibernate.loader.Loader.doQuery(Loader.java:889)
	at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289)
	at org.hibernate.loader.Loader.doList(Loader.java:2449)
	at org.hibernate.loader.Loader.doList(Loader.java:2435)
	at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276)
	at org.hibernate.loader.Loader.list(Loader.java:2271)
	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:1105)
	at org.hibernate.internal.QueryImpl.list(QueryImpl.java:100)
	at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:252){code}

After some debugging I found that Hibernate ignores the mappedBy values and uses names with prepended "_identifierMapper". After changing the annotations to:
{code}@Entity
public class SimpleEntity {
    @Id
    private int id;
    @OneToOne(cascade = CascadeType.ALL, mappedBy = "_identifierMapper.entityA")
    private EntityConnection connection;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "_identifierMapper.entityB")
    private List<EntityConnection> otherConnections;
    // accessors
}{/code}
the code works.

Expected: the first example of SimpleEntity should work.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

       



More information about the hibernate-issues mailing list