2 Replies Latest reply on Mar 7, 2006 11:16 AM by Milen Angelov

    InheritanceType.TABLE_PER_CLASS question

    Milen Angelov Newbie

      Because the EJB3.0 FD spec doesn't say to much about that kind of inheritance strategy, I looked up at the following examples:
      [url]http://docs.jboss.org/ejb3/app-server/tutorial/tableperinheritance/table.html[/url]

      I played with them but an exception occured:
      javax.ejb.EJBException: org.hibernate.exception.SQLGrammarException: could not execute query
      at org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptionInOurTx(Ejb3TxPolicy.java:69)
      at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:83)
      *****
      Caused by: org.hibernate.exception.SQLGrammarException: could not execute query
      at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:65)
      at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
      at org.hibernate.loader.Loader.doList(Loader.java:2148)
      at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
      at org.hibernate.loader.Loader.list(Loader.java:2024)
      at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:375)
      at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:308)
      at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:153)
      at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1129)
      at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
      at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:39)
      ****
      Caused by: java.sql.SQLException: ORA-00942: table or view does not exist


      I have the following class definition:

      @Entity
      @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
      public class Pet implements java.io.Serializable {
      private int id;

      private String name;

      private int weight;
      *****

      -----------------------------------------

      @Entity
      @Table(name="CAT")
      public class Cat extends Pet
      {
      int lives;

      public int getLives()
      {
      return lives;
      }

      public void setLives(int lives)
      {
      this.lives = lives;
      }
      }

      ---------------------------------------------

      @Entity
      @Table(name="DOG")
      public class Dog extends Pet
      {
      private int numBones;

      public int getNumBones()
      {
      return numBones;
      }

      public void setNumBones(int numBones)
      {
      this.numBones = numBones;
      }
      }


      and a stateless session bean:
      public List getAllPets( ){

      return em.createQuery("from Pet p").getResultList();

      }


      ************************

      I have only two table - CAT and DOG exactly as provided in the example above.

      Can anyone see where is the problem - should I have table PET?!?

        • 1. Re: InheritanceType.TABLE_PER_CLASS question
          milan wölke Newbie

          maybe its because u dont have any properties defined for pet. Or didnt u just post them? Do cat and dog tables contain pet attributes?

          Regards, Milan Wölke

          • 2. Re: InheritanceType.TABLE_PER_CLASS question
            Milen Angelov Newbie

            Here is the whole Pet class:

            import javax.persistence.Entity;
            import javax.persistence.GeneratedValue;
            import javax.persistence.GenerationType;
            import javax.persistence.Id;
            import javax.persistence.Inheritance;
            import javax.persistence.InheritanceType;

            @Entity
            @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
            public abstract class Pet implements java.io.Serializable {
            private int id;

            private String name;

            private int weight;

            @Id
            @GeneratedValue(strategy = GenerationType.TABLE)
            public int getId() {
            return id;
            }

            public void setId(int id) {
            this.id = id;
            }

            public String getName() {
            return name;
            }

            public void setName(String name) {
            this.name = name;
            }

            public int getWeight() {
            return weight;
            }

            public void setWeight(int weight) {
            this.weight = weight;
            }
            }


            Also here are the two tables:

            create table CAT (
            ID NUMBER primary key,
            LIVES NUMBER,
            NAME varchar2(20),
            WEIGHT NUMBER
            );

            create table DOG (
            ID NUMBER primary key,
            NUMBONES NUMBER,
            NAME varchar2(20),
            WEIGHT NUMBER
            );