2 Replies Latest reply on May 20, 2002 3:32 AM by Panagiotis Korros

    Problem with foreign key mapping

    Panagiotis Korros Newbie

      I have an CMP Entity Bean named Node with parent-child relations to other nodes.
      My problem is that when i call the getParent method
      the following sql statement is executed:

      SELECT parent FROM ttree WHERE (treeid=?)

      instead of:
      SELECT parentid FROM ttree WHERE (treeid=?)

      when i change my table field to use tha name 'parent'instead of 'parentid' everything works great.
      how can i map the CMR field 'parent' to table field 'parentid'?

      I use JBoss 3.0 RC2

      the xml descriptor is:
      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">
      <ejb-jar>
      <enterprise-beans>

      <display-name>NodeBean</display-name>
      <ejb-name>NodeBean</ejb-name>
      <local-home>ecentric.ejb.NodeLocalHome</local-home>
      ecentric.ejb.NodeLocal
      <ejb-class>ecentric.ejb.NodeBean</ejb-class>
      <persistence-type>Container</persistence-type>
      <prim-key-class>java.lang.Integer</prim-key-class>
      False
      <abstract-schema-name>node</abstract-schema-name>
      <cmp-field>
      <field-name>id</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>creationDate</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>modifiedDate</field-name>
      </cmp-field>
      <primkey-field>id</primkey-field>

      </enterprise-beans>

      <ejb-relation>
      <ejb-relation-name>node-node</ejb-relation-name>
      <ejb-relationship-role>
      nodeBean
      <ejb-relationship-role-name>node-has-children</ejb-relationship-role-name>
      One
      <relationship-role-source>
      nodeBean
      <ejb-name>NodeBean</ejb-name>
      </relationship-role-source>
      <cmr-field>
      nodeBean
      <cmr-field-name>children</cmr-field-name>
      <cmr-field-type>java.util.Set</cmr-field-type>
      </cmr-field>
      </ejb-relationship-role>
      <ejb-relationship-role>
      nodeBean
      <ejb-relationship-role-name>node-belongsto-parent</ejb-relationship-role-name>
      Many
      <relationship-role-source>
      nodeBean
      <ejb-name>NodeBean</ejb-name>
      </relationship-role-source>
      <cmr-field>
      nodeBean
      <cmr-field-name>parent</cmr-field-name>
      </cmr-field>
      </ejb-relationship-role>
      </ejb-relation>

      <assembly-descriptor>
      <container-transaction>

      <ejb-name>NodeBean</ejb-name>
      <method-name>*</method-name>

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      </assembly-descriptor>
      </ejb-jar>

      the jbosscmp-jdbc.xml file contains:

      <?xml version="1.0" encoding="UTF-8"?>
      <jbosscmp-jdbc>

      java:/SQLServerPool
      <type-mapping>MS SQLSERVER2000</type-mapping>
      true
      <create-table>true</create-table>
      <remove-table>false</remove-table>
      <read-only>true</read-only>
      <time-out>300000</time-out>
      <select-for-update>false</select-for-update>
      <pk-constraint>true</pk-constraint>
      <relation-mapping-style>foreign-key</relation-mapping-style>
      <read-ahead>
      on-load
      255
      <cache-size>1000</cache-size>
      </read-ahead>

      <enterprise-beans>

      <ejb-name>NodeBean</ejb-name>
      <table-name>ttree</table-name>
      <cmp-field>
      <field-name>id</field-name>
      <column-name>treeid</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>creationDate</field-name>
      <column-name>datecreated</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>modifiedDate</field-name>
      <column-name>datemodified</column-name>
      </cmp-field>

      </enterprise-beans>

      <ejb-relation>
      <ejb-relation-name>node-node</ejb-relation-name>
      <foreign-key-mapping>
      <ejb-relationship-role>
      <ejb-relationship-role-name>node-has-children</ejb-relationship-role-name>
      <foreign-key-fields/>
      </ejb-relationship-role>
      <ejb-relationship-role>
      <ejb-relationship-role-name>node-belongsto-parent</ejb-relationship-role-name>
      <foreign-key-fields>
      <foreign-key-field>
      <field-name>id</field-name>
      <column-name>treeid</column-name>
      </foreign-key-field>
      </foreign-key-fields>
      </ejb-relationship-role>
      </foreign-key-mapping>
      </ejb-relation>

      </jbosscmp-jdbc>





      The sql script used to create the db table is:

      CREATE TABLE [dbo].[ttree] (
      [treeid] [int] IDENTITY (1, 1) NOT NULL ,
      [parentid] [int] NULL ,
      [datecreated] [datetime] NULL ,
      [datemodified] [datetime] NULL
      ) ON [PRIMARY]

      ALTER TABLE [dbo].[ttree] WITH NOCHECK ADD
      CONSTRAINT [PK_ttree] PRIMARY KEY CLUSTERED
      (
      [treeid]
      ) ON [PRIMARY]
      GO

      ALTER TABLE [dbo].[ttree] ADD
      CONSTRAINT [FK_ttree_ttree] FOREIGN KEY
      (
      [parentid]
      ) REFERENCES [dbo].[ttree] (
      [treeid]
      )