Relationships and Foreign Keys
mpforste Apr 9, 2004 11:40 AMSorry to be keep asking similar questions all the time but I am trying to get JBoss working after moving from another less useful bean server (openejb) and am trying to get the Container to manage the relationships and CMP using Foreign Keys, I have started with the simplest beans to allow me to understand and get the technique for how it works and have scoured the internet for help and advice and it all seems to come up wanting, I have had several errors from "You can have only 1 PK" to Cannot find cmpfield id in bean transtype.
I was under the impression that if I use a Foreign key I can pass the object in and it will save the reference automatically in the database (and vice versa when requesting the Bean - it will return the sub Bean as well.
Here are the DB tables I am trying to get JBoss to create.
transtype
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
tt_desc VARCHAR(30),
PRIMARY KEY ( `id` )
transaction
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT
tr_date DATETIME,
tr_state INTEGER,
tr_summary TINYINT,
tr_non_refundable TINYINT,
tr_type BIGINT UNSIGNED NOT NULL <--- This is the Foreign Key
PRIMARY KEY ( `id` )
In the Beans i am using different names in the bean (but this should also work if configured correctly) - DATETIME maps to a Date() Object, and other fields map to objects (we are not using int long etc....)
the TransTypeBean works fine for Create using the Autonumber key
and the TransTypeBean findByPrimaryKey works as well.
Now we are having problems getting the TransactionBean to create a new entry in the DB,
The Create is declared as follows
public Object ejbCreate( Date tr_date, Integer tr_state, Boolean tr_summary, Boolean tr_non_refundable, TransType tr_type)
how do we get the tr_type to save just the id field in the db - is it as simple as setTr_type(tr_type.getId());
also when doing a findByPrimaryKey() on the Transaction entity will it return automatically the TransType Object or will I have to do some other stuff to get it out?
my ejb-jar has the following entries
<entity> <ejb-name>TransTypeBean</ejb-name> <home>com.ingotz.entity.TransTypeHome</home> <remote>com.ingotz.entity.TransType</remote> <local-home>com.ingotz.entity.TransTypeLocalHome</local-home> <local>com.ingotz.entity.TransTypeLocal</local> <ejb-class>com.ingotz.entity.TransTypeBean</ejb-class> <persistence-type>Container</persistence-type> <!--<prim-key-class>java.lang.Object</prim-key-class>--> <prim-key-class>java.lang.Long</prim-key-class> <reentrant>False</reentrant> <cmp-field> <field-name>Id</field-name> </cmp-field> <cmp-field> <field-name>Description</field-name> </cmp-field> <primkey-field>Id</primkey-field> <cmp-version>2.x</cmp-version> <abstract-schema-name>TransTypeBeanAPS</abstract-schema-name> <resource-ref> <res-ref-name>jdbc/mysql</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> </entity> <entity> <ejb-name>TransactionBean</ejb-name> <home>com.ingotz.entity.TransactionHome</home> <remote>com.ingotz.entity.Transaction</remote> <local-home>com.ingotz.entity.TransactionLocalHome</local-home> <local>com.ingotz.entity.TransactionLocal</local> <ejb-class>com.ingotz.entity.TransactionBean</ejb-class> <persistence-type>Container</persistence-type> <!--<prim-key-class>java.lang.Object</prim-key-class>--> <prim-key-class>java.lang.Long</prim-key-class> <reentrant>False</reentrant> <cmp-field><field-name>id</field-name></cmp-field> <cmp-field><field-name>tr_date</field-name></cmp-field> <cmp-field><field-name>tr_state</field-name></cmp-field> <cmp-field><field-name>tr_summary</field-name></cmp-field> <cmp-field><field-name>tr_non_refundable</field-name></cmp-field> <cmp-field><field-name>tr_type</field-name></cmp-field> <primkey-field>id</primkey-field> <cmp-version>2.x</cmp-version> <abstract-schema-name>TransactionBeanAPS</abstract-schema-name> <resource-ref> <res-ref-name>jdbc/mysql</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> </entity> </enterprise-beans> <relationships> <ejb-relation> <ejb-relation-name>Type-Transaction</ejb-relation-name> <ejb-relationship-role> <ejb-relationship-role-name>TransTypeBeanEnd</ejb-relationship-role-name> <multiplicity>One</multiplicity> <relationship-role-source> <ejb-name>TransTypeBean</ejb-name> </relationship-role-source> </ejb-relationship-role> <ejb-relationship-role> <ejb-relationship-role-name>TransactionBeanEnd</ejb-relationship-role-name> <multiplicity>Many</multiplicity> <relationship-role-source> <ejb-name>TransactionBean</ejb-name> </relationship-role-source> <cmr-field> <cmr-field-name>Tr_type</cmr-field-name> </cmr-field> </ejb-relationship-role> </ejb-relation> </relationships>
My jbosscmp-jdbc.xml file has the following relationship entries
<relationships> <ejb-relation> <ejb-relation-name>Type-Transaction</ejb-relation-name> <foreign-key-mapping /> <ejb-relationship-role> <ejb-relationship-role-name>TransTypeBeanEnd</ejb-relationship-role-name> <key-fields> <key-field> <field-name>Id</field-name> <!-- Property this end --> <column-name>tr_type</column-name> <!-- Foreign key at other end --> </key-field> </key-fields> </ejb-relationship-role> <ejb-relationship-role> <ejb-relationship-role-name>TransactionBeanEnd</ejb-relationship-role-name> <key-fields/> </ejb-relationship-role> </ejb-relation> </relationships>
I do hope someone can help me understand what I am missing.
Mike.