2 Replies Latest reply on Dec 4, 2007 12:07 PM by p.chevillon

    hibernate and inheritance

    p.chevillon

      Hello,

      I've a problem with requests on sublasses of a class tree (Entity beans). I got a org.hibernate.WrongClassException when trying to access data with a oneToMany relation.

      My architecture is:

      A <---- B <---- C

      @Entity
      @Table(name = "A")
      @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
      @DiscriminatorColumn(name = "DTYPE", discriminatorType = DiscriminatorType.STRING)
      @DiscriminatorValue("A_TYPE")
      public class A implements java.io.Serializable {
      
       private Long id;
      ...
      }
      
      @Entity
      @DiscriminatorValue("B_TYPE")
      public class B extends A {
      
       // Associations
       private Collection<Obj> objSet = new ArrayList<Obj>();
      
       @OneToMany(mappedBy = "A")
       public Collection<Review> getObjSet() {
       return objSet;
       }
      
       public void setObjSet(Collection<Obj> objSet) {
       this.objSet = objSet;
       }
      }
      
      @Entity
      @DiscriminatorValue("C_TYPE")
      public class C extends B {
       ...
      }
      
      


      And Obj is defined like:

      @Entity
      @Table(name = "OBJ")
      public class Obj {
       private B value;
      
       @ManyToOne
       @JoinColumn(name="A_ID")
       public AppPOI getValue() {
       return value;
       }
      
       public void setValue(B value) {
       this.value = value;
       }
      
      }
      


      When I create a new Obj and associate it to a B Entity it works. (correctly added to the DB, good ID pointing from OBJ to a "A like" entry)

      Now when I try to access using this request:
      SELECT obj FROM Obj obj WHERE obj.value.id=44549450

      It gives me that error message:
      org.hibernate.WrongClassException: Object with id: 44549450 was not of the specified subclass: C (Discriminator: B_TYPE)
      


      I'm using using jboss 4.0.5 with seam and hibernate updated to version 3.2.5ga

      Thanks for your help, I couldn't find an answer to my problem with the search function of this forum, I've passed so many times reading it at this point...

      Paulin



        • 1. Re: hibernate and inheritance
          p.chevillon

          My DB is PostgreSQL. Any Idea ?

          • 2. Re: hibernate and inheritance
            p.chevillon

            I found a possible explanation to this problem. It seem's the discriminant is'n't used when requesting by a one-to-many in an subclass. So a way to make hibernate consider the hierarchy is to force control on the Discriminator column.

            With annotations, @Where should be used.
            @Where(clause="POI_TYPE='B'")
            and
            @Where(clause="POI_TYPE='C'")

            But in my example, I really don't understand where I have to place this clauses. Any help ?

            Thanks

            Paulin