5 Replies Latest reply on Aug 21, 2006 4:52 AM by Bronks Private

    Composite Primary Key in an EntityBean (CMP) - XDoclet

    Bronks Private Newbie

      Hi!

      I am new to JBossIDE and XDoclet. I have coded and deployed successfully an entity bean with a primary key with a single field.

      Now I want to code a new entity bean with a Primary Key with 2 fields:

      Fieldname - Type - Primary
      ordernumber - integer - PK
      itemnumber - integer - PK
      article - varchar
      amount - float


      I do not know how to code that. I have used the search, google ... etc ...

      Please show me a code for an entity bean with a composite key.

      Thanks

      Bronks

        • 1. Re: Composite Primary Key in an EntityBean (CMP) - XDoclet
          Frank Langelage Master

          Add
          * @ejb.pk-field
          to the getters comment of the PK-fields.

          • 2. Re: Composite Primary Key in an EntityBean (CMP) - XDoclet
            Bronks Private Newbie

             

            "lafr" wrote:
            Add
            * @ejb.pk-field
            to the getters comment of the PK-fields.

            Thanks. I have done this but i am getting the following Deploymenterror:

            [verifier] EJB spec violation:
            Bean : OrderItem
            Method : public OrderItemBean$PrimaryKey ejbCreate(Integer, Integer, Integer) throws CreateException
            Section: 10.6.4
            Warning: The return type of an ejbCreate(...) method must be the entity bean's primary key type.


            What should i write at "primkey-field=" ???

            I can not find the mistake. Please take a look at my code:

            /**
             * <!-- begin-xdoclet-definition -->
             * @ejb.bean name="OrderItem"
             * jndi-name="OrderItem"
             * type="CMP"
             * primkey-field="ordernumber" ?????????????????????????????????
             * schema="OrderItemSCHEMA"
             * cmp-version="2.x"
             *
             * @ejb.persistence
             * table-name="public.orderitems"
             *
             * @ejb.finder
             * query="SELECT OBJECT(a) FROM OrderItemSCHEMA as a"
             * signature="java.util.Collection findAll()"
             *
             * @ejb.pk class="OrderItemBean.PrimaryKey"
             *
             *
             * @jboss.persistence datasource="java:/jdbc/pgtestdb" datasource-mapping="PostgreSQL 8.0" table-name="zz" create-table="true" remove-table="false" alter-table="false"
             * <!-- end-xdoclet-definition -->
             * @generated
             **/
            
            public abstract class OrderItemBean implements javax.ejb.EntityBean {
            
             /**
             * Generated Primary Key Class
             * @generated
             */
             public class PrimaryKey implements java.io.Serializable {
             /**
             * @generated
             */
             public java.lang.Integer ordernumber;
            
             /**
             * @generated
             */
             public java.lang.Integer orderitemnumber;
             }
            
             /**
             *
             * <!-- begin-user-doc -->
             * The ejbCreate method.
             * <!-- end-user-doc -->
             *
             * <!-- begin-xdoclet-definition -->
             * @ejb.create-method
             * <!-- end-xdoclet-definition -->
             * @generated
             */
             public OrderItemBean.PrimaryKey ejbCreate(Integer aOrdernumber,
             Integer aOrderitemnumber,
             Integer aQuantity)
             throws javax.ejb.CreateException {
            
             setOrdernumber(aOrdernumber);
             setOrderitemnumber(aOrderitemnumber);
             setQuantity(aQuantity);
             return null;
            
             }
            
             /**
             * <!-- begin-user-doc -->
             * The container invokes this method immediately after it calls ejbCreate.
             * <!-- end-user-doc -->
             *
             * @generated
             */
             public void ejbPostCreate() throws javax.ejb.CreateException {
             // begin-user-code
             // end-user-code
             }
            
             /**
             *
             *
             * <!-- begin-user-doc -->
             * CMP Field ordernumber
             *
             * Returns the ordernumber
             * @return the ordernumber
             *
             * <!-- end-user-doc -->
             *
             * <!-- begin-xdoclet-definition -->
             *
             * @ejb.persistent-field
             * @ejb.persistence
             * column-name="ordernumber"
             * jdbc-type="INTEGER"
             * sql-type="int4"
             * read-only="false"
             * @ejb.pk-field
             *
             * @ejb.interface-method
             *
             * <!-- end-xdoclet-definition -->
             * @generated
             */
             public abstract java.lang.Integer getOrdernumber();
            
             /**
             * <!-- begin-user-doc -->
             * Sets the ordernumber
             *
             * @param java.lang.Integer the new ordernumber value
             * <!-- end-user-doc -->
             *
             * <!-- begin-xdoclet-definition -->
             * @ejb.interface-method
             * <!-- end-xdoclet-definition -->
             * @generated
             */
             public abstract void setOrdernumber(java.lang.Integer ordernumber);
            
             /**
             *
             *
             * <!-- begin-user-doc -->
             * CMP Field orderitemnumber
             *
             * Returns the orderitemnumber
             * @return the orderitemnumber
             *
             * <!-- end-user-doc -->
             *
             * <!-- begin-xdoclet-definition -->
             *
             * @ejb.persistent-field
             * @ejb.persistence
             * column-name="orderitemnumber"
             * jdbc-type="INTEGER"
             * sql-type="int4"
             * read-only="false"
             * @ejb.pk-field
             *
             * @ejb.interface-method
             *
             * <!-- end-xdoclet-definition -->
             * @generated
             */
             public abstract java.lang.Integer getOrderitemnumber();
            
             /**
             * <!-- begin-user-doc -->
             * Sets the orderitemnumber
             *
             * @param java.lang.Integer the new orderitemnumber value
             * <!-- end-user-doc -->
             *
             * <!-- begin-xdoclet-definition -->
             * @ejb.interface-method
             * <!-- end-xdoclet-definition -->
             * @generated
             */
             public abstract void setOrderitemnumber(java.lang.Integer orderitemnumber);
            
             /**
             *
             *
             * <!-- begin-user-doc -->
             * CMP Field quantity
             *
             * Returns the quantity
             * @return the quantity
             *
             * <!-- end-user-doc -->
             *
             * <!-- begin-xdoclet-definition -->
             *
             * @ejb.persistent-field
             * @ejb.persistence
             * column-name="quantity"
             * jdbc-type="INTEGER"
             * sql-type="int4"
             * read-only="false"
             *
             *
             * @ejb.interface-method
             *
             * <!-- end-xdoclet-definition -->
             * @generated
             */
             public abstract java.lang.Integer getQuantity();
            
             /**
             * <!-- begin-user-doc -->
             * Sets the quantity
             *
             * @param java.lang.Integer the new quantity value
             * <!-- end-user-doc -->
             *
             * <!-- begin-xdoclet-definition -->
             * @ejb.interface-method
             * <!-- end-xdoclet-definition -->
             * @generated
             */
             public abstract void setQuantity(java.lang.Integer quantity);
            
            }



            • 3. Re: Composite Primary Key in an EntityBean (CMP) - XDoclet
              Bronks Private Newbie

              I have found the mistake:

              It was the sub class "PrimaryKey" and this:
              * @ejb.pk class="OrderItemBean.PrimaryKey"

              It generated a primary class in the wrong package ...

              • 4. Re: Composite Primary Key in an EntityBean (CMP) - XDoclet
                None None Master

                Are you really sure that you want to have a composite primary key? From what I understand that is almost always a bad idea.

                • 5. Re: Composite Primary Key in an EntityBean (CMP) - XDoclet
                  Bronks Private Newbie

                   

                  "SmokingAPipe" wrote:
                  Are you really sure that you want to have a composite primary key? From what I understand that is almost always a bad idea.

                  Oh, yes. In such cases composite primary keys are absolutely usual an necessary.