2 Replies Latest reply on Jun 6, 2005 3:51 PM by edstorm

    EJB3 Table Creation

    edstorm Newbie

      Hi,

      I am currently working with the ejb3 implementation in jboss-head and have run into some trouble with automatic table creation for entities that inherit a one to many relationship via TABLE_PER_CLASS Inheritance. Specifically, If EntityB inherits a One-To-Many realtionship to EntityC from EntityA then the generated Table Create statement for EntityB will contain two fk columns ( with the same name ).

      I should mention that if I create the tables manually everything works as I expect. Below are code snippets that can be used to reproduce the behavior. I am not 100% complete with my reading of the ejb3 draft spec so if this use case is not allowed by the spec I apologize for the wasted space.

      Using the code below, the TABLE Create statement that is generated for EasyActivityEvent is:

      create table TEST_EA (
       id int4 not null,
       easyDay_fk int4,
       easyDay_fk int4,
       primary key (id)
      )
      
      
      


      EntityA
      @Entity
      @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
      @Table( name = "TEST_EE" )
      public abstract class EasyEvent implements java.io.Serializable
      {
      
       private int mId;
       private EasyDay mEasyDay;
      
       @Id(generate = GeneratorType.AUTO)
       public int getId()
       {
       return mId;
       }
      
       public void setId( int pId ) {
       mId = pId;
       }
      
       @ManyToOne
       @JoinColumn( name = "easyDay_fk" )
       public EasyDay getEasyDay() {
       return mEasyDay;
       }
      
      
       public void setEasyDay( EasyDay pEasyDay ) {
       this.mEasyDay = pEasyDay;
       }
      
      }
      


      EntityB
      @Entity
      @Table(name = "TEST_EA")
       public class EasyActivityEvent extends EasyEvent {
       }
      
      


      EntityC
      @Entity
      @Table(name = "TEST_ED")
      @NamedQuery( name = "easyDay.findAll", queryString = "Select Distinct Object(p) FROM EasyDay p")
       public class EasyDay implements java.io.Serializable {
      
       private int mId;
       Set<EasyActivityEvent> mEasyActivityEvents;
      
       @Id(generate = GeneratorType.AUTO)
       public int getId()
       {
       return mId;
       }
      
       public void setId( int pId ) {
       mId = pId;
       }
      
       @OneToMany(mappedBy="easyDay", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
       public Set<EasyActivityEvent> getEasyActivityEvents( ) {
       return mEasyActivityEvents;
       }
      
       public void setEasyActivityEvents( Set<EasyActivityEvent> pEasyActivityEvents ) {
       this.mEasyActivityEvents = pEasyActivityEvents;
       }
      
      
      
      
      
       }
      




        • 1. Re: EJB3 Table Creation
          Emmanuel Bernard Master

          What you've done is inconsistent.

          @ManyToOne
          @JoinColumn( name = "easyDay_fk" )
          public EasyDay getEasyDay() {

          There is a many to one between EasyEvent and EasyDay

          @OneToMany(mappedBy="easyDay", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
          public Set getEasyActivityEvents( ) {
          return mEasyActivityEvents;
          }

          there is a many to one (actually one to many inverse) between EasyActivityEvent and EasyDay with the same fk column

          they are considered as 2 different associations.

          • 2. Re: EJB3 Table Creation
            edstorm Newbie

            Thanks so much for your help, I appreciate it.

            -Ed