4 Replies Latest reply on Mar 4, 2010 4:08 AM by galder.zamarreno

    Inconsistent behaviour when fetching cached subclass entities?

      Hi all,

       

      The use of jboss-cache as hibernate second-level cache is leading to inconsistent behaviour when fetching cached subclass entities as opposed to fetching non-cached subclass entities.

       

      Scenario

      Consider a simple scenario having a parent entity:

      @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:

      @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() + "}]";
          }
      }

       

      @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:

      em.persist(new Subclass1("identifier1"));

       

      Trying to fetch an instance of Subclass2 using:

      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().

       

      Environment

      jboss-cache 1.4.1.SP13

      hibernate 3.2.5

      jboss 4.2.2