2 Replies Latest reply on Mar 14, 2006 1:30 PM by Fabrizio Giovannetti

    CMP Entity Bean, Compound Key with foreign reference

    Fabrizio Giovannetti Newbie


      Hi everybody!!

      I have a undefined problem during th development of a entity bean.
      I list the details of this entity bean here:

      1) The primary key is compound and have 2 fields, ID_Testata (Integer) and NSigillo( Integer ).
      2) A field of the primary key have a foreign reference with another entity bean(table)

      I have tried different solution but without success.

      I list the files I have wrote:

      EJB-JAR.XML

      ...

      jdbc:mysql://127.0.0.1:3306/lvbo [jboss on Default schema]
      <display-name>SigilliPerTestataEB</display-name>
      <ejb-name>SigilliPerTestataBean</ejb-name>
      <local-home>it.lvcido.lvbo.ejb.entity.SigilliPerTestataLocalHome</local-home>
      it.lvcido.lvbo.ejb.entity.SigilliPerTestataLocal
      <ejb-class>it.lvcido.lvbo.ejb.entity.SigilliPerTestataBean</ejb-class>
      <persistence-type>Container</persistence-type>
      <prim-key-class>it.lvcido.lvbo.ejb.entity.SigilliPerTestataPK</prim-key-class>
      false
      <abstract-schema-name>SigilliPerTestata</abstract-schema-name>
      <cmp-field>
      <field-name>nSigillo</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>destrizioneSigillo</field-name>
      </cmp-field>
      <cmp-field>

      <field-name>idTestata</field-name>
      </cmp-field>

      auto generated method
      <query-method>
      <method-name>findByNSigillo</method-name>
      <method-params>
      <method-param>java.lang.Integer</method-param>
      </method-params>
      </query-method>
      <ejb-ql>SELECT OBJECT(s) FROM SigilliPerTestata AS s WHERE s.nSigillo = ?1</ejb-ql>


      auto generated method
      <query-method>
      <method-name>findByDestrizioneSigillo</method-name>
      <method-params>
      <method-param>java.lang.String</method-param>
      </method-params>
      </query-method>
      <ejb-ql>SELECT OBJECT(s) FROM SigilliPerTestata AS s WHERE s.destrizioneSigillo = ?1</ejb-ql>


      <query-method>
      <method-name>findByTestataId</method-name>
      <method-params>
      <method-param>java.lang.Integer</method-param>
      </method-params>
      </query-method>
      <ejb-ql>SELECT OBJECT(s) FROM SigilliPerTestata AS s WHERE s.idTestata = ?1</ejb-ql>


      ...
      ...
      <ejb-relation>

      <ejb-relation-name>SigilliPerTestataBean-TestatePerOpdogBean</ejb-relation-name>
      <ejb-relationship-role>
      <ejb-relationship-role-name>SigilliPerTestataBean</ejb-relationship-role-name>
      Many
      <relationship-role-source>
      <ejb-name>SigilliPerTestataBean</ejb-name>
      </relationship-role-source>
      <cmr-field>
      <cmr-field-name>IDtestata</cmr-field-name>
      </cmr-field>
      </ejb-relationship-role>
      <ejb-relationship-role>
      <ejb-relationship-role-name>IDtestata</ejb-relationship-role-name>
      One
      <relationship-role-source>
      <ejb-name>TestatePerOpdogBean</ejb-name>
      </relationship-role-source>
      <cmr-field>
      <cmr-field-name>sigilliPerTestata</cmr-field-name>
      <cmr-field-type>java.util.Collection</cmr-field-type>
      </cmr-field>
      </ejb-relationship-role>
      </ejb-relation>
      ...

      JBOSSCMP_JDBC.XML

      ...

      <ejb-name>SigilliPerTestataBean</ejb-name>
      <table-name>sigilli_per_testata</table-name>
      <cmp-field>
      <field-name>destrizioneSigillo</field-name>
      <column-name>DestrizioneSigillo</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>nSigillo</field-name>
      <column-name>NSigillo</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>idTestata</field-name>
      <column-name>ID_Testata</column-name>
      </cmp-field>

      ....
      ...

      <ejb-name>TestatePerOpdogBean</ejb-name>
      <table-name>testate_per_opdog</table-name>
      <cmp-field>
      <field-name>cinCodiceLuogoVisitaMerci</field-name>
      <column-name>CinCodiceLuogoVisitaMerci</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>nazMezzoFront</field-name>
      <column-name>nazMezzoFront</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>iDTestata</field-name>
      <column-name>ID_Testata</column-name>
      </cmp-field>
      <unknown-pk>
      <unknown-pk-class>java.lang.Integer</unknown-pk-class>
      <field-name>iDTestata</field-name>
      <column-name>ID_Testata</column-name>
      <auto-increment/>
      </unknown-pk>
      <entity-command name="mysql-get-generated-keys"/>

      ...
      ...
      <ejb-relation>
      <ejb-relation-name>SigilliPerTestataBean-TestatePerOpdogBean</ejb-relation-name>
      <foreign-key-mapping/>
      <ejb-relationship-role>
      <ejb-relationship-role-name>SigilliPerTestataBean</ejb-relationship-role-name>
      <key-fields/>
      </ejb-relationship-role>
      <ejb-relationship-role>
      <ejb-relationship-role-name>IDtestata</ejb-relationship-role-name>
      <key-fields>
      <key-field>
      <field-name>iDTestata</field-name>
      <column-name>ID_TESTATA</column-name>
      </key-field>
      </key-fields>
      </ejb-relationship-role>
      </ejb-relation>
      ...

      I have met these problems:

      - the message error "Entity with primary key it.lvcido.lvbo.ejb.entity.SigilliPerTestataPK@1 already exists" when I create a new SigilliPerTestataBean (entity bean) .

      - wrong values when i execute this query:"SELECT OBJECT(s) FROM SigilliPerTestata AS s WHERE s.idTestata = ?1"

      I don't know what I have to do..

      Thanks..

      Fabrizio

      PS: I use mysql as database and Netbeans as IDE