Hi all,
Enabling the hibernate second-level cache is leading to inconsistent behaviour when fetching cached subclass entities as opposed to fetching non-cached subclass entities.
Consider a simple scenario having a parent entity:
Code:
@Entity
@Table(name = "tt_test")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class ParentEntity implements Serializable {
private static final long serialVersionUID = 5751958972675892020L;
@Id
private String id;
protected ParentEntity() {
super();
}
public ParentEntity(String id) {
this.id = id;
}
public String getId() {
return id;
}
}
and two subclass entities extending the above entity:
Code:
@Entity
public final class Subclass1 extends ParentEntity {
private static final long serialVersionUID = -4329753920470094896L;
protected Subclass1() {
super();
}
public Subclass1(String id) {
super(id);
}
@Override
public String toString() {
return "Subclass1 [ID={" + getId() + "}, Class={" + getClass() + "}]";
}
}
Code:
@Entity
public final class Subclass2 extends ParentEntity {
private static final long serialVersionUID = -6102575763486357670L;
/**
* Default constructor (required for hibernation reasons)
*/
protected Subclass2() {
super();
}
public Subclass2(String id) {
super(id);
}
@Override
public String toString() {
return "Subclass2 [ID={" + getId() + "}, Class={" + getClass() + "}]";
}
}
After persisting an instance of Subclass1 using:
Code:
em.persist(new Subclass1("identifier1"));
Trying to fetch an instance of Subclass2 using:
Code:
em.find(Subclass2.class, "identifier1");
will result in a
null return if the entity is not cached. This is the expected, correct behaviour.
However, when the entity with id
identifier1 is cached, this will match and return an instance (of Subclass1) if the entity is cached. This appears to be both inconsistent behaviour, and a wrong result, as the entity with identifier
identifier1 is of type Subclass1 not SubClass2, as requested in the call to em.find().
This behaviour was reproduced in the following environements:
jboss-cache 1.4.1.SP13
hibernate 3.2.5
jboss 4.2.2
EHCache - terracotta-3.2.0
hibernate 3.2.5
jboss 4.2.2
jboss-cache 3.1.0
hibernate 3.3.1.GA
jboss 5.1.0.GA
As this was reproducible on both jboss-cache and EHCache it seems to be a hibernate second level caching problem.
Any help on this matter will be very much appreciated.