I am facing issue in CMP relationship.
I have a product table with product_id and product_name with product_id as auto_increment. I can able to add the product successfully.
Then I have created a table product_key with pk_id, pk_key and product_id. pk_id as auto_increment and product_id as foreign key.
I have configured the relationship in both ejb.jar.xml and jbosscmp-jdbc.xml as given below:
What I am trying to do is:
1. User inputting the product name and the key in the form.
2. In
struts action, I am retrieving the form values.
3. In struts action, I am getting the Remote Session bean from JNDI.
4. In session bean, first I am getting the Product, by passing the product name to findByName.
5. In session bean, then I am trying to insert a productkey in productkey table.
6. In session bean, then I am trying to set the product to productkey.
But I am getting the following error:
My ProductKey ejbCreate is having only one argument for key. Since id is auto_increment and product_id is foreign, I didn't included it in the ejbCreate
But in the debug log, how the insert query has product_id too?
<!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>
<entity>
<ejb-name>ProductEJB</ejb-name>
<local-home>com.example.ejb.product.ProductHomeLocal</local-home>
<local>com.example.ejb.product.ProductLocal</local>
<ejb-class>com.example.ejb.product.ProductBean</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.Integer</prim-key-class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>Product</abstract-schema-name>
<cmp-field><field-name>id</field-name></cmp-field>
<cmp-field><field-name>name</field-name></cmp-field>
<primkey-field>id</primkey-field>
<security-identity><use-caller-identity /></security-identity>
<query>
<query-method>
<method-name>findByName</method-name>
<method-params>
<method-param>java.lang.String</method-param>
</method-params>
</query-method>
<ejb-ql>
SELECT OBJECT(p) FROM Product p WHERE p.name = ?1
</ejb-ql>
</query>
</entity>
<entity>
<ejb-name>ProductKeyEJB</ejb-name>
<local-home>com.example.ejb.productkey.ProductKeyHomeLocal</local-home>
<local>com.example.ejb.productkey.ProductKeyLocal</local>
<ejb-class>com.example.ejb.productkey.ProductKeyBean</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.Integer</prim-key-class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>ProductKey</abstract-schema-name>
<cmp-field><field-name>id</field-name></cmp-field>
<cmp-field><field-name>key</field-name></cmp-field>
<primkey-field>id</primkey-field>
<security-identity><use-caller-identity /></security-identity>
</entity>
<session>
<ejb-name>ProductManagerEJB</ejb-name>
<home>com.example.ejb.productmanager.ProductManagerHomeRemote</home>
<remote>com.example.ejb.productmanager.ProductManagerRemote</remote>
<ejb-class>com.example.ejb.productmanager.ProductManagerBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
<ejb-local-ref>
<ejb-ref-name>ProductHomeLocal</ejb-ref-name>
<ejb-ref-type>Entity</ejb-ref-type>
<local-home>com.example.ejb.product.ProductHomeLocal</local-home>
<local> com.example.ejb.product.ProductLocal</local>
<!-- ejb-link is required by jboss for local-refs. -->
<ejb-link>ProductEJB</ejb-link>
</ejb-local-ref>
<ejb-local-ref>
<ejb-ref-name>ProductKeyHomeLocal</ejb-ref-name>
<ejb-ref-type>Entity</ejb-ref-type>
<local-home>com.example.ejb.productkey.ProductKeyHomeLocal</local-home>
<local> com.example.ejb.product.ProductKeyLocal</local>
<!-- ejb-link is required by jboss for local-refs. -->
<ejb-link>ProductKeyEJB</ejb-link>
</ejb-local-ref>
</session>
</enterprise-beans>
<relationships>
<ejb-relation>
<ejb-relation-name>Product-ProductKey</ejb-relation-name>
<ejb-relationship-role>
<ejb-relationship-role-name>Product-has-many-ProductKeys</ejb-relationship-role-name>
<multiplicity>One</multiplicity>
<relationship-role-source>
<ejb-name>ProductEJB</ejb-name>
</relationship-role-source>
<cmr-field>
<cmr-field-name>productKeys</cmr-field-name>
<cmr-field-type>java.util.Set</cmr-field-type>
</cmr-field>
</ejb-relationship-role>
<ejb-relationship-role>
<ejb-relationship-role-name>ProductKey-belongs-to-Product</ejb-relationship-role-name>
<multiplicity>Many</multiplicity>
<cascade-delete/>
<relationship-role-source>
<ejb-name>ProductKeyEJB</ejb-name>
</relationship-role-source>
<cmr-field>
<cmr-field-name>product</cmr-field-name>
</cmr-field>
</ejb-relationship-role>
</ejb-relation>
</relationships>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>ProductEJB</ejb-name>
<method-name>*</method-name>
</method>
<method>
<ejb-name>ProductKeyEJB</ejb-name>
<method-name>*</method-name>
</method>
<method>
<ejb-name>ProductManagerEJB</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>
jbosscmp-jdbc.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jbosscmp-jdbc PUBLIC
"-//JBoss//DTD JBOSSCMP-JDBC 4.0//EN"
"http://www.jboss.org/j2ee/dtd/jbosscmp-jdbc_4_0.dtd">
<jbosscmp-jdbc>
<defaults>
<datasource>java:/MySqlDS</datasource>
<datasource-mapping>mySQL</datasource-mapping>
<create-table>false</create-table>
<remove-table>false</remove-table>
</defaults>
<enterprise-beans>
<entity>
<ejb-name>ProductEJB</ejb-name>
<pk-constraint>false</pk-constraint>
<table-name>Product</table-name>
<cmp-field>
<field-name>name</field-name>
<column-name>product_name</column-name>
</cmp-field>
<!-- unknown-pk>
<unknown-pk-class>java.lang.Integer</unknown-pk-class>
<field-name>id</field-name>
<column-name>product_id</column-name>
<jdbc-type>INTEGER</jdbc-type>
<sql-type>INTEGER</sql-type>
<auto-increment/>
</unknown-pk-->
<cmp-field>
<field-name>id</field-name>
<column-name>product_id</column-name>
<auto-increment />
</cmp-field>
<entity-command name="mysql-get-generated-keys" />
</entity>
<entity>
<ejb-name>ProductKeyEJB</ejb-name>
<table-name>ProductKey</table-name>
<cmp-field>
<field-name>id</field-name>
<column-name>pk_id</column-name>
<auto-increment />
</cmp-field>
<cmp-field>
<field-name>key</field-name>
<column-name>pk_key</column-name>
</cmp-field>
<entity-command name="mysql-get-generated-keys" />
</entity>
</enterprise-beans>
<relationships>
<ejb-relation>
<ejb-relation-name>Product-ProductKey</ejb-relation-name>
<foreign-key-mapping />
<ejb-relationship-role>
<ejb-relationship-role-name>
Product-has-many-ProductKeys
</ejb-relationship-role-name>
<key-fields>
<key-field>
<field-name>id</field-name>
<column-name>product_id</column-name>
</key-field>
</key-fields>
</ejb-relationship-role>
<ejb-relationship-role>
<ejb-relationship-role-name>
ProductKey-belongs-to-Product
</ejb-relationship-role-name>
<key-fields />
</ejb-relationship-role>
</ejb-relation>
</relationships>
</jbosscmp-jdbc>
Session Bean