10 Replies Latest reply on Sep 17, 2005 6:45 AM by infodavid

    InheritanceType.TABLE_PER_CLASS don't work ?

    infodavid

      Hi,

      I have tried this code :

      @EmbeddableSuperclass(access = AccessType.PROPERTY)
      public abstract class AbstractSimpleEntity implements Serializable, ISimpleObject { ... }

      @javax.persistence.Entity(access = AccessType.PROPERTY)
      @Table(name = "ENTITIES")
      @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
      public class Entity extends AbstractSimpleEntity implements IEntity { ... }

      Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
      @Entity(access = AccessType.PROPERTY)
      @Table(name = "PERSONS")
      @Remote(IPerson.class)
      public class Person extends org.infodavid.par.Entity implements IPerson { ... }

      but two tables are created with all the fields.
      I would like to create two tables 'ENTITIES' and 'PERSONS' :

      CREATE TABLE ENTITIES (
      entity_id INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 0, INCREMENT BY 1) PRIMARY KEY,
      name VARCHAR(32) NOT NULL,
      ...
      )

      CREATE TABLE PERSONS (
      person_id INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 0, INCREMENT BY 1) PRIMARY KEY,
      ...
      entity_id INTEGER NOT NULL,
      FOREIGN KEY (entity_id) REFERENCES ENTITIES(entity_id) ON DELETE CASCADE,
      )

        • 1. Re: InheritanceType.TABLE_PER_CLASS don't work ?
          epbernard

          so it's a JOINED inheritance strategy

          • 2. Re: InheritanceType.TABLE_PER_CLASS don't work ?
            infodavid

            Can you give me an example because I have tried this strategy and something did not work in my code.

            Thanks for all ;)

            • 3. Re: InheritanceType.TABLE_PER_CLASS don't work ?
              infodavid

              here is the trouble, eclipse can't found annotation :
              @InheritanceJoinColumn

              I use Jboss 4.0.1RC1 with the ejb3 deployer.

              • 4. Re: InheritanceType.TABLE_PER_CLASS don't work ?
                infodavid

                Now I have :

                @javax.persistence.Entity(access = AccessType.PROPERTY)
                @Table(name = "ENTITIES")
                @Inheritance(strategy = InheritanceType.JOINED)
                public class Entity extends AbstractSimpleEntity implements IEntity {
                @Id(generate = GeneratorType.TABLE)
                @Column(name = "entity_id", nullable = false, unique = true)
                public long getPrimaryKey() { return id; }
                ...
                }

                and

                @Entity(access = AccessType.PROPERTY)
                @Table(name = "PERSONS")
                @Inheritance(strategy = InheritanceType.JOINED)
                @PrimaryKeyJoinColumns( { @PrimaryKeyJoinColumn( name = "entity_id", referencedColumnName = "entity_id") } )
                @Remote(IPerson.class)
                public class Person extends org.infodavid.par.Entity implements IPerson { ... }

                the first table is ok,
                the second table seems not ok because the only key I have is 'entity_id' and no second key like 'person_id'. See :

                CREATE TABLE PERSONS (
                person_id INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 0, INCREMENT BY 1) PRIMARY KEY,
                ...
                entity_id INTEGER NOT NULL,
                FOREIGN KEY (entity_id) REFERENCES ENTITIES(entity_id) ON DELETE CASCADE,
                )

                How can it work if a person has the same id as the entity ? I would like to create an other subclass of 'Entity' (like Company or something like this) and I don't understand how it can work.

                • 5. Re: InheritanceType.TABLE_PER_CLASS don't work ?
                  epbernard

                  I don't see why it fails, what is the full code of the annotated classes?
                  Hibernate shouldn't generate 'person_id'

                  • 6. Re: InheritanceType.TABLE_PER_CLASS don't work ?
                    infodavid

                    First, Thanks for all.

                    Here is the full code :

                    For Entity :

                    @javax.persistence.Entity(access = AccessType.PROPERTY)
                    @Table(name = "ENTITIES")
                    @Inheritance(strategy = InheritanceType.JOINED)
                    public class Entity extends AbstractSimpleEntity implements IEntity {
                     private String address = null;
                     private City city = null;
                    
                     public Entity() { }
                    
                     public Entity(Entity o) { super(o); }
                    
                     @Id(generate = GeneratorType.TABLE)
                     @Column(name = "entity_id", nullable = false, unique = true)
                     public long getPrimaryKey() { return id; }
                    
                     @Column(name = "address", nullable = true, unique = false, length = 255)
                     @Length(min = 0, max = 255)
                     public String getAddress() throws RemoteException { return address; }
                    
                     public void setAddress(String s) throws RemoteException { address = s; }
                    
                     @ManyToOne(fetch = FetchType.LAZY)
                     @JoinColumn(name = "city_id")
                     public City getCity() throws RemoteException { return city; }
                    
                     public void setCity(Object o) throws RemoteException { city = (City)o; }
                    }


                    for Person :

                    @Entity(access = AccessType.PROPERTY)
                    @Table(name = "PERSONS")
                    @Inheritance(strategy = InheritanceType.JOINED)
                    @PrimaryKeyJoinColumns( { @PrimaryKeyJoinColumn( name = "entity_id", referencedColumnName = "entity_id") } )
                    @Remote(IPerson.class)
                    public class Person extends org.infodavid.par.Entity implements IPerson {
                     private String firstname = null;
                     private PersonTitle title = null;
                    
                     public Person() { }
                    
                     public Person(Person o) { super(o); }
                    
                     @ManyToOne(fetch = FetchType.LAZY)
                     @JoinColumn(name = "title_id")
                     public PersonTitle getTitle() throws RemoteException { return title; }
                    
                     public void setTitle(Object o) throws RemoteException { title = (PersonTitle)o; }
                    
                     @Column(name = "firstname", nullable = true, unique = false, length = 48)
                     @Length(min = 0, max = 48)
                     public String getFirstname() throws RemoteException { return firstname; }
                    
                     public void setFirstname(String s) throws RemoteException { firstname = s; }
                    }


                    and the generated tables :

                    For Entity :

                    CREATE MEMORY TABLE ENTITY(
                    ENTITY_ID BIGINT NOT NULL PRIMARY KEY,
                    ADDRESS VARCHAR(255),
                    CITY_ID BIGINT,CONSTRAINT SYS_CT_12 UNIQUE(ENTITY_ID),
                    CONSTRAINT FK7C02D003F09E40D9 FOREIGN KEY(CITY_ID) REFERENCES CITY(CITY_ID))


                    for Person :

                    CREATE MEMORY TABLE PERSONS(
                    ENTITY_ID BIGINT NOT NULL PRIMARY KEY,
                    FIRSTNAME VARCHAR(48),(15),
                    TITLE_ID BIGINT,
                    CONSTRAINT FK25B5B9E5B91CA19 FOREIGN KEY(ENTITY_ID) REFERENCES ENTITY(ENTITY_ID))


                    ALTER TABLE PERSONS ADD CONSTRAINT FK25B5B9E6DE21C86 FOREIGN KEY(TITLE_ID) REFERENCES PERSONTITLE(TITLE_ID)



                    So when I add a 'Person', what is done ? In my other version (simple jdbc), I add an 'Entity' record for each 'Person' using the 'Entity' generated key in the column 'entity_id' of the new 'Person'.

                    Many thanks for your help.

                    • 7. Re: InheritanceType.TABLE_PER_CLASS don't work ?
                      infodavid

                      You can see my other post for more informations if needed

                      http://www.jboss.org/index.html?module=bb&op=viewtopic&t=69327

                      • 8. Re: InheritanceType.TABLE_PER_CLASS don't work ?
                        epbernard

                        OK so Hibernate is working as I'm expecting.

                        You ask it to create a person table with a PK/FK to Entities names entity_id

                        @PrimaryKeyJoinColumns( { @PrimaryKeyJoinColumn( name = "person_id", referencedColumnName = "entity_id") } )

                        would have created a PK/FK named person_id

                        To load a person, Hibernate do an inner join between Entities and Persons using this FK.

                        • 9. Re: InheritanceType.TABLE_PER_CLASS don't work ?
                          infodavid

                          Thanks it works for 'Person', now I'll try using another subclass of 'Entity'.

                          Thanks for all Emmanuel,
                          Merci beaucoup.

                          • 10. Re: InheritanceType.TABLE_PER_CLASS don't work ?
                            infodavid

                            Tout va marche bien, merci encore.