6 Replies Latest reply on Sep 17, 2002 3:09 PM by Dain Sundstrom

    How to map composite primary key?

    Tung Dang Novice

      One table looks like this:

      create table t(
      id int not null,
      int fid not null,
      int something,
      primary key(id, fid))

      As you see, the table has a composite key.

      I have the primary key class (MyKeyPK) for a bean MyBean. The MyBean looks like this:

      public abstract class AbstractBaseBean implements
      EntityBean {

      public abstract void setMyKeyPK(MyKeyPK pk);
      public abstract MyKeyPK getMyKeyPK();

      public abstract int getSomething();
      public abstract void setSomething(int something);

      public MyKeyPK ejbCreate(MyKeyPK pk) throws CreateException, EJBException {
      setMyKeyPK(pk);

      return null;
      }

      public void ejbPostCreate(MyKeyPK pk) throws CreateException, EJBException { }

      public void setEntityContext(EntityContext context) throws EJBException { }
      public void unsetEntityContext() throws EJBException { }
      public void ejbActivate() throws EJBException { }
      public void ejbPassivate() throws EJBException { }
      public void ejbRemove() throws EJBException { }
      public void ejbStore() throws EJBException { }
      public void ejbLoad() throws EJBException { }
      }

      How can I map a composite primary key in ejb-jar.xml and jbosscmp-jdbc.xml?

      Regards.

        • 1. Re: How to map composite primary key?
          Dain Sundstrom Master

          You need a custom primary key class. I suggest you take a look at one of the EJB books; look for primary key class in the index.

          • 2. Re: How to map composite primary key?
            David Li Newbie

            Hi Dain,
            I did use the primary key class I created, and I declared in the ejb-jar.xml file under that particular
            entity as:

            .
            .
            <prim-key-class>table1Key</prim-key-class>
            .
            .

            but I am still getting an error when I started the server.

            javax.ejb.ObjectNotFoundException: Object with primary key table1Key@4df1cf5b not found in storage

            Do you know what I might do wrong?
            David Li.

            ps. thx for the comments on my other post, I am trying to read the docments on JBoss3.0.1 now. and in the progress of testing xdoclet to generate my ejb deployment scripts.

            • 3. Re: How to map composite primary key?
              David Li Newbie

              Hi Dain,
              I did use the primary key class I created, and I declared in the ejb-jar.xml file under that particular
              entity as:

              .
              .
              <prim-key-class>table1Key</prim-key-class>
              .
              .

              but I am still getting an error when I started the server.

              javax.ejb.ObjectNotFoundException: Object with primary key table1Key@4df1cf5b not found in storage

              Do you know what I might do wrong?
              David Li.

              ps. thx for the comments on my other post, I am trying to read the docments on JBoss3.0.1 now. and in the progress of testing xdoclet to generate my ejb deployment scripts.

              • 4. Re: How to map composite primary key?
                Dain Sundstrom Master

                You could be doing lots of things wrong. It is very hard to get the primary key class correct (a lot of generators like JBuilder get it very wrong).

                The two most important methods are equals and hashCode. Don't try to implement any inheritance in your pk classes; it is way more trouble then it is worth.

                Here's some sample code. This may not be perfect; I writing it from memory: [pre]
                public boolean equals(Object o) {
                if(!(o instanceof MyPkType)) {
                return false;
                }
                // will never be null
                MyPkType myPk = (MyPkType)o;
                return myPk.blah1.equals(this.blah1) &&
                myPk.blah2.equals(this.blah2) &&
                myPk.blah3.equals(this.blah3) &&
                myPk.blah4.equals(this.blah4);
                }

                public int hashCode() {
                int result = 17;
                result = 37*result + (blah1==null ? 0 : blah1.hashCode());
                result = 37*result + (blah2==null ? 0 : blah2.hashCode());
                result = 37*result + (blah3==null ? 0 : blah3.hashCode());
                result = 37*result + (blah4==null ? 0 : blah4.hashCode());
                return result;
                } [/pre]

                • 5. Re: How to map composite primary key?
                  Chris Bailey Newbie

                  Might I suggest that it would be enormously helpful if the JBossCMP book simply changed one of the tables, such as gangster, from primitive key (integer gangsterId), to a composite primary key (GangsterKey{firstName,lastName}).

                  It seems there are many of us who have been beating our heads for many hours (or at least I have mine) on the vast multitude of ripple affects from such a seemingly "trivial" change for such a basic design pattern.

                  Stump point #1: primkey
                  primkey-field, primkey-class mismatch
                  (primkey omission)
                  Stump point #2: CMR
                  how to map CMR column name to multiple fields
                  Stump point #3: create
                  table generation failure
                  (turn off primary key constraint)
                  Stump point #4: exists
                  JBoss omits where: SELECT COUNT(*) FROM TBL WHERE
                  (still working on this one)
                  Stump point #5: DVC
                  etc. etc.

                  Cheers,

                  Chris

                  • 6. Re: How to map composite primary key?
                    Dain Sundstrom Master

                    I'm actually planning on writing a short article on the correct way to write a compoung primary key.