Hi all,
I think I might've encountered a Bug in Hibernate 4.1.7. Here's a test case I built:
Code:
public class HibernateBugTest {
@Test
public void test() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("pu");
EntityManager em = emf.createEntityManager();
SubClass1 sc1 = em.find(SubClass1.class, "1");
SubClass2 sc2 = em.find(SubClass2.class, "1");
assertTrue(sc1.getKeyField().equals(sc2.getKeyField()));
}
@Entity
@Table(name = "BUGTEST")
@Inheritance
@DiscriminatorColumn(name = "otherField")
public static class AbstractSuperClass {
@Id
private String keyField;
public String getKeyField() {
return keyField;
}
public void setKeyField(String keyField) {
this.keyField = keyField;
}
}
@Entity
@DiscriminatorValue("SC1")
public static class SubClass1 extends AbstractSuperClass {
}
@Entity
@DiscriminatorValue("SC2")
public static class SubClass2 extends AbstractSuperClass {
}
}
Running this case results in a null pointer exception at assertTrue(), because sc2 is null. I've debugged the case to the point where I found that an error occurs during the second call to em.find().
The method
org.hibernate.engine.internal.StatefulPersistenceContext#getEntity(EntityKey key) returns the SubClass1 object fetched by the first call to em.find().
I think this might be due to the case that the EntityKey's hashCode() method does not evaluate it's entityName property, but only the rootEntityName (which is 'AbstractSuperClass' for SubClass1
and SubClass2).
Is this really a bug, or did I miss something?
Edit: The test case would work, if I inserted an em.clear() inbetween the two find()s, of course - but this isn't really the point, is it? ;-)