6 Replies Latest reply on Aug 23, 2007 2:38 AM by jfheintz

    compounded primary key with ejb 3.0

    jfheintz Newbie

      Hello,

      it is now three week I tried to migrate from ejb 2.0 to ejb 3.0, I finally make it work, but when I have a look to the DB,
      hibernate as change my table which have a compounded primary key!!!!

      Here is an example:

      --- TABLE: SI_SITE ---
      CREATE TABLE SI_SITE (
      siteId BIGSERIAL,
      name VARCHAR(128)
      );


      --- TABLE: SI_TIMEUNIT ---
      CREATE TABLE SI_TIMEUNIT (
      timeUnitId BIGSERIAL,
      fromDate TIMESTAMP,
      toDate TIMESTAMP
      );

      --- TABLE: SI_BROWSERDESC ---
      CREATE TABLE SI_BROWSERDESC (
      browserDescId VARCHAR(128),
      name VARCHAR(128)
      );

      --- TABLE: SI_BROWSERSTAT ---
      CREATE TABLE SI_BROWSERSTAT (
      timeUnit_timeUnitId BIGINT,
      site_siteId BIGINT,
      browserDesc_browserDescId VARCHAR(128),
      view INTEGER,
      );

      ALTER TABLE SI_BROWSERSTAT ADD PRIMARY KEY (timeUnit_timeUnitId, site_siteId, browserDesc_browserDescId);
      ALTER TABLE SI_BROWSERSTAT ADD CONSTRAINT SI_BROWSERSTAT_timeUnit_HAS_SI_TIMEUNIT FOREIGN KEY ( timeUnit_timeUnitId ) REFERENCES SI_TIMEUNIT( timeUnitId );
      ALTER TABLE SI_BROWSERSTAT ADD CONSTRAINT SI_BROWSERSTAT_site_HAS_SI_SITE FOREIGN KEY ( site_siteId ) REFERENCES SI_SITE( siteId );
      ALTER TABLE SI_BROWSERSTAT ADD CONSTRAINT SI_BROWSERSTAT_browserDesc_HAS_SI_BROWSERDESC FOREIGN KEY ( browserDesc_browserDescId ) REFERENCES SI_BROWSERDESC( browserDescId );


      ---------------- BrowserStatEJB3 ---------------------------
      @Entity
      @IdClass(BrowserStatPK.class)
      @Table(name="SI_BROWSERSTAT")
      public class BrowserStatEJB3 {

      @EmbeddedId
      private BrowserStatPK pk;

      @Column(name="view")
      private int view;

      @javax.persistence.Column(name="timeUnit_timeUnitId", nullable=false, updatable=false, insertable=false)
      private java.lang.Long timeUnitTimeUnitId;

      @javax.persistence.ManyToOne(fetch = javax.persistence.FetchType.LAZY)
      @javax.persistence.JoinColumn(name="timeUnit_timeUnitId")
      private data.timeUnit.TimeUnitEJB3 timeUnit=new com.statinfo.stat.data.timeUnit.TimeUnitEJB3();

      @javax.persistence.Column(name="site_siteId", nullable=false, updatable=false, insertable=false)
      private java.lang.Long siteSiteId;

      @javax.persistence.ManyToOne(fetch = javax.persistence.FetchType.LAZY)
      @javax.persistence.JoinColumn(name="site_siteId")
      private data.site.SiteEJB3 site=new com.statinfo.stat.data.site.SiteEJB3();

      @javax.persistence.Column(name="browserDesc_browserDescId", nullable=false, updatable=false, insertable=false)
      private java.lang.String browserDescBrowserDescId;

      @javax.persistence.ManyToOne(fetch = javax.persistence.FetchType.LAZY)
      @javax.persistence.JoinColumn(name="browserDesc_browserDescId")
      private data.parameter.browserDesc.BrowserDescEJB3 browserDesc=new com.statinfo.stat.data.parameter.browserDesc.BrowserDescEJB3();

      public BrowserStatEJB3() {
      }


      public BrowserStatPK getPk() {
      return this.pk;
      }

      public void setPk(BrowserStatPK pk) {
      this.pk=pk;
      }

      ... other getter and setter ...


      ---------------- BrowserStatPK ---------------------------
      public class BrowserStatPK implements java.io.Serializable {

      final static long serialVersionUID = 1L;
      private transient int _hashCode = 0;

      public java.lang.Long timeUnitTimeUnitId;
      public java.lang.Long siteSiteId;
      public java.lang.String browserDescBrowserDescId;

      public BrowserStatPK() {
      }

      public BrowserStatPK( java.lang.Long timeUnitTimeUnitId, java.lang.Long siteSiteId, java.lang.String browserDescBrowserDescId) {
      this.setTimeUnitTimeUnitId(timeUnitTimeUnitId);
      this.setSiteSiteId(siteSiteId);
      this.setBrowserDescBrowserDescId(browserDescBrowserDescId);
      }

      ... other getter and setter ...


      Now Jboss has added, the following column to my SI_BROWSERSTAT table:

      timeUnitTimeUnitId BIGINT,
      siteSiteId BIGINT,
      browserDescBrowserDescId VARCHAR(128),
      pk BYTEARRAY

      Why does it not use the good pk column?????

      Regards,

      JF

        • 1. Re: compounded primary key with ejb 3.0
          wayne baylor Apprentice

          you should use either @IdClass or @EmbeddedId, but not both.

          • 2. Re: compounded primary key with ejb 3.0
            jfheintz Newbie

            OK, you are right, I remove the @EmbeddedId on pk and @Id on each attribute which are part of the primary key.

            And the result is still the same....

            • 3. Re: compounded primary key with ejb 3.0
              wayne baylor Apprentice

              can you post your entity code after this change?

              • 4. Re: compounded primary key with ejb 3.0
                jfheintz Newbie

                Here it is, and many thanks for your help

                /////////////////// BrowserStatEJB3 /////////////////////
                @javax.persistence.Entity
                @javax.persistence.IdClass(BrowserStatPK.class)
                @javax.persistence.Table(name="SI_BROWSERSTAT")
                public class BrowserStatEJB3 {

                private BrowserStatPK pk;

                @javax.persistence.Column(name="view")
                private int view;

                @javax.persistence.Id
                @javax.persistence.Column(name="timeUnit_timeUnitId", nullable=false, updatable=false, insertable=false)
                private java.lang.Long timeUnitTimeUnitId;

                @javax.persistence.ManyToOne(fetch = javax.persistence.FetchType.LAZY)
                @javax.persistence.JoinColumn(name="timeUnit_timeUnitId")
                private com.statinfo.stat.data.timeUnit.TimeUnitEJB3 timeUnit;


                @javax.persistence.Id
                @javax.persistence.Column(name="site_siteId", nullable=false, updatable=false, insertable=false)
                private java.lang.Long siteSiteId;

                @javax.persistence.ManyToOne(fetch = javax.persistence.FetchType.LAZY)
                @javax.persistence.JoinColumn(name="site_siteId")
                private com.statinfo.stat.data.site.SiteEJB3 site;


                @javax.persistence.Id
                @javax.persistence.Column(name="browserDesc_browserDescId", nullable=false, updatable=false, insertable=false)
                private java.lang.String browserDescBrowserDescId;

                @javax.persistence.ManyToOne(fetch = javax.persistence.FetchType.LAZY)
                @javax.persistence.JoinColumn(name="browserDesc_browserDescId")
                private com.statinfo.stat.data.parameter.browserDesc.BrowserDescEJB3 browserDesc;

                public BrowserStatEJB3() {
                }


                public BrowserStatPK getPk() {
                return this.pk;
                }

                public void setPk(BrowserStatPK pk) {
                this.pk=pk;
                }

                public int getView() {
                return this.view;
                }

                public void setView(int value) {
                this.view=value;
                }

                }


                /////////////////// BrowserStatPK /////////////////////
                public class BrowserStatPK implements java.io.Serializable {

                final static long serialVersionUID = 1L;
                private transient int _hashCode = 0;

                public java.lang.Long timeUnitTimeUnitId;
                public java.lang.Long siteSiteId;
                public java.lang.String browserDescBrowserDescId;

                public BrowserStatPK() {
                }

                public BrowserStatPK( java.lang.Long timeUnitTimeUnitId, java.lang.Long siteSiteId, java.lang.String browserDescBrowserDescId) {
                this.setTimeUnitTimeUnitId(timeUnitTimeUnitId);
                this.setSiteSiteId(siteSiteId);
                this.setBrowserDescBrowserDescId(browserDescBrowserDescId);
                }

                .. getter + setter ...

                • 5. Re: compounded primary key with ejb 3.0
                  wayne baylor Apprentice

                  try removing the field:

                  private BrowserStatPK pk;

                  from BrowserStatEJB3.

                  you've already got the id fields, so you shouldn't need this.

                  • 6. Re: compounded primary key with ejb 3.0
                    jfheintz Newbie

                    I already try, the pk field is removed from the DB (after I recreate the DB)
                    but I still have the 3 other fields.

                    I imagine Hibernate find the three real column of th PK and decide put another name as the 3 field already exist? Why is it not using my column?

                    This is really strange behaviour!