6 Replies Latest reply on Apr 11, 2005 3:11 PM by dsouza

    Problem with TABLE_PER_CLASS inheritance in Preview 5

    dsouza

      Hi,

      I just installed Preview 5 and now I'm getting an exception I didn't get before:

      13:25:28,984 WARN [ServiceController] Problem starting service jboss.j2ee:service=EJB3,module=storeEntity.ejb3
      org.hibernate.AnnotationException: TABLE_PER_CLASS only allows hierarchy leaf mapping
       at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:301)
       at org.hibernate.cfg.AnnotationConfiguration.addAnnotatedClass(AnnotationConfiguration.java:94)
       at org.hibernate.cfg.AnnotationConfiguration.addAnnotatedClasses(AnnotationConfiguration.java:62)
       at org.jboss.ejb3.entity.HibernateSessionFactory.createSessionFactory(HibernateSessionFactory.java:196)
       at org.jboss.ejb3.Ejb3Module.createHibernateSessionFactory(Ejb3Module.java:256)
      ...
      


      I checked the new tutorial and documentation and everything seems fine. My implementation looks much like the Pet/Dog/Cat example.

      Any ideas on what could be causing this?

      DS

        • 1. Re: Problem with TABLE_PER_CLASS inheritance in Preview 5
          dsouza

          I got it to work by removing all class-level annotations from the superclass and annotating the subclass as a regular entity. I get exactly the same effect I would expect from table_per_class inheritance without explicitly declaring it as such.

          I'm glad I found a way to make it work but I still would like to understando what happened.

          DS

          • 2. Re: Problem with TABLE_PER_CLASS inheritance in Preview 5
            epbernard

            This is the way Hibernate annotations currently implements TABLE_PER_CLASS inheritance. I need to improve that (this is queued)

            • 3. Re: Problem with TABLE_PER_CLASS inheritance in Preview 5
              bill.burke

              you can only put the @Inheritance at the top of the hierarchy. Take a look at Pet, vs. Dog/Cat in the tableperinheritance tutorial

              • 4. Re: Problem with TABLE_PER_CLASS inheritance in Preview 5
                dsouza

                Yes, I had tried that and upon deployment hibernate ended up creating a table mapping for the superclass instead of the child classes. I was running it with hibernate.hbm2ddl.auto=update.

                Now that you mentioned it, I tried it again, but this time I dropped all tables and started jboss with hibernate.hbm2ddl.auto=create. It worked. Don't know what I did wrong the first time, though... I guess it should have worked then.

                Anyway, thanks for the answer. I wouldn't have tried it again otherwise.
                DS

                • 5. Re: Problem with TABLE_PER_CLASS inheritance in Preview 5
                  dsouza

                  Hi, sorry to insist on this, but I started jboss with hibernate.hbm2ddl.auto=update again and the problem came back. If I put the @Inheritance in my superclass, it creates a table in the database with the superclass' name and maps any relationships to it instead of the respective subclasses.
                  eg. If I have entity A with @Inheritance and entity B extends A, it creates table A in the database. Any relationships from a table C->B are mapped as C->A.

                  If I remove @Inheritance and leave it as I mentioned before it works fine. If I chose to leave @Inheritance it only works if I start jboss with hibernate.hbm2ddl.auto=create.

                  It's strange. I wouldn't think changing the automatic table creation option could affect the relationships like that.

                  DS

                  • 6. Re: Problem with TABLE_PER_CLASS inheritance in Preview 5
                    dsouza

                    In hope that it will help to understand and find the problem:

                    I also was able to make it work in any case once I removed the @Entity from the top of the hierarchy.

                    As for the @Entity I don't know which exactly is correct, but the tutorial shows that @Entity should be used on both the superclass and the subclass.

                    In regard to using @Inheritance, the Preview 5 tutorial says that:

                    The subclass annotations look like this.
                    
                    @Entity
                    @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
                    public class Dog extends Pet
                    {
                    


                    The actual Dog class in the tutorial doesn't contain the @Inheritance. I guess it was a typo inherited from the Preview 4 tutorial, but please correct this as it may lead to confusion.

                    Thanks,
                    DS