3 Replies Latest reply on Apr 12, 2005 7:00 AM by Piero Sartini

    Troubles with table-per-class inheritance

    Mark Auburn Newbie

      I'm trying to implement Hibernate's table-per-concrete class strategy using EJB3.0's TABLE_PER_CLASS inheritance and the latest CVS source code.

      This used to work in preview 4, but with the latest source code I don't seem to have the ability to overwrite the @Table attribute on the subclass, and it appears to automagically create a "type" attribute that shouldn't exist.

      My base class looks like:

      @Entity(access = AccessType.FIELD)
      @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
      public abstract class BaseEntity3<EntityPKType extends PK3> implements java.io.Serializable, Comparable
      {
      @Id(generate = GeneratorType.NONE)
      @Column(name="ID")
      protected long id;

      @Version
      @Column(name="VERSION")
      protected long version;
      ...
      }

      My concrete subclass is:

      @Entity(access = AccessType.FIELD)
      @Table(name = "Groups")
      public class Group3 extends BaseEntity3<GroupPK3>
      {
      @Column(name="NAME", nullable=false, length=50)
      protected String name;

      @Column(name="DESCRIPTION", length=50)
      protected String description;

      @ManyToOne(fetch = FetchType.EAGER)
      @JoinColumn(name="PREFERENCE_PARENT_ID")
      protected Group3 preferenceParent;
      ...
      }

      When I do a find on Group3, the SQL looks like:
      select group3x0_.ID as ID, ... from BaseEntity3 group3x0_ where group3x0_.TYPE='com.portblue.model.admin.group.Group3' ...

      I've tried putting a @Inheritance tag on the subclass as well as on the superclass, and I get the Hibernate error: org.hibernate.AnnotationException: TABLE_PER_CLASS only allows hierarchy leaf mapping

      I also tried putting a union-subclass hibernate mapping in a Group3.hbm.xml file, but then I got a complaint about "duplicate mappings".

      Am I missing something? Thanks...

      - Mark

        • 1. Re: Troubles with table-per-class inheritance
          Bill Burke Master

          I'll try to reproduce and fix. I don't like regressing between releases.

          Pretty cool/interesting use case though. So interesting in fact, i've forwarded it to the EG for contemplation....

          • 2. Re: Troubles with table-per-class inheritance
            Mark Auburn Newbie

            To give a little more detail: in preview 4, I defined the Group3 class as follows:

            @Entity(access = AccessType.FIELD)
            @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
            @Table(name = "Groups")
            public class Group3 extends BaseEntity3<GroupPK3>
            ...

            Once I moved to the CVS source code version, I had to remove the @Inheritance tag from Group3 because otherwise I got a Hibernate error on deployment:

            2005-03-29 10:31:22,950 ERROR [org.hibernate.cfg.AnnotationConfiguration] Could not compile the mapping annotations
            org.hibernate.AnnotationException: TABLE_PER_CLASS only allows hierarchy leaf mapping

            The Hibernate error seems rather counterintuitive, given that getting rid of the error requires removing the @INHERITANCE annotation from the leaf class!

            I believe that this class structure is pretty generic and useful - probably as more people get used to using Java 5 generics (we just transitioned recently ourselves), more people will want to do things like this...

            - Mark

            • 3. Re: Troubles with table-per-class inheritance
              Piero Sartini Newbie

              seems to be a similar problem i ran into - worked fine until i used the cvs version:

              @Entity
              @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
              public class DBObject implements java.io.Serializable {
              private long id;
              private Date lastChanged;
              private Date created;

              i used this base class to define fields which must be present in every class/table:

              @Entity
              @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
              public class Customer extends DBObject {

              with the jboss-head from cvs it doesn't work. the error is:
              TABLE_PER_CLASS only allows hierarchy leaf mapping

              is there any way to get around this for now?