0 Replies Latest reply on Sep 16, 2003 1:32 PM by shiva0

    CMR Relation containing FK field. Error on ejbCreate

    shiva0 Newbie

      I got a CMP bean having a One to many relationship.

      Let say, Message got MessageDescriptions

      The MessageDescription PK is a compound from the FK of Message and the language code.

      MessageDescPK(messageId, lang);

      when I try to create a new MessageDesc, JBoss throws me this:

      java.lang.IllegalStateException: Can't modify relationship: CMR field MessageAlertDesc.messageAlert has foreign key fields mapped to the primary key columns. Primary key may only be set once in ejbCreate [EJB 2.0 Spec. 10.3.5].
      at org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMRFieldBridge.setValue(JDBCCMRFieldBridge.java:534)
      at org.jboss.ejb.plugins.cmp.bridge.EntityBridgeInvocationHandler.invoke(EntityBridgeInvocationHandler.java:126)
      at org.jboss.proxy.compiler.Runtime.invoke(Runtime.java:59)
      at com.cgi.portal.messagealert.ejb.MessageAlertDescCMP$Proxy.setMessageAlert()
      at com.cgi.portal.messagealert.ejb.MessageAlertDescBean.ejbPostCreate(MessageAlertDescBean.java:276)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:324)
      at org.jboss.ejb.plugins.CMPPersistenceManager.postCreateEntity(CMPPersistenceManager.java:260)
      at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.postCreateEntity(CachedConnectionInterceptor.java:278)
      at org.jboss.ejb.EntityContainer.postCreateLocalHome(EntityContainer.java:585)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:324)
      at org.jboss.ejb.EntityContainer$ContainerInterceptor.invoke(EntityContainer.java:1032)
      at org.jboss.ejb.plugins.cmp.jdbc.JDBCRelationInterceptor.invoke(JDBCRelationInterceptor.java:95)
      at org.jboss.ejb.plugins.EntitySynchronizationInterceptor.invoke(EntitySynchronizationInterceptor.java:301)
      at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:186)
      at org.jboss.ejb.plugins.EntityReentranceInterceptor.invoke(EntityReentranceInterceptor.java:82)
      at org.jboss.ejb.plugins.EntityInstanceInterceptor.invoke(EntityInstanceInterceptor.java:174)
      at org.jboss.ejb.plugins.EntityLockInterceptor.invoke(EntityLockInterceptor.java:89)
      at org.jboss.ejb.plugins.EntityCreationInterceptor.invokeHome(EntityCreationInterceptor.java:43)
      at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:88)
      at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:273)
      at org.jboss.ejb.plugins.TxInterceptorCMT.invokeHome(TxInterceptorCMT.java:74)
      at org.jboss.ejb.plugins.SecurityInterceptor.invokeHome(SecurityInterceptor.java:92)
      at org.jboss.ejb.plugins.LogInterceptor.invokeHome(LogInterceptor.java:120)
      at org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor.invokeHome(ProxyFactoryFinderInterceptor.java:93)
      at org.jboss.ejb.EntityContainer.internalInvokeHome(EntityContainer.java:477)
      at org.jboss.ejb.Container.invoke(Container.java:694)
      at org.jboss.ejb.plugins.local.BaseLocalProxyFactory.invokeHome(BaseLocalProxyFactory.java:272)
      at org.jboss.ejb.plugins.local.LocalHomeProxy.invoke(LocalHomeProxy.java:110)
      at $Proxy80.create(Unknown Source)
      at com.cgi.portal.messagealert.outboxmessagesbl.OutboxMessagesBLBean.insertCustomerServiceMessage(OutboxMessagesBLBean.java:79)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:324)
      at org.jboss.ejb.StatelessSessionContainer$ContainerInterceptor.invoke(StatelessSessionContainer.java:629)
      at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:186)
      at org.jboss.ejb.plugins.StatelessSessionInstanceInterceptor.invoke(StatelessSessionInstanceInterceptor.java:72)
      at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:84)
      at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:243)
      at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCMT.java:104)
      at org.jboss.ejb.plugins.SecurityInterceptor.invoke(SecurityInterceptor.java:117)
      at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:191)
      at org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor.invoke(ProxyFactoryFinderInterceptor.java:122)
      at org.jboss.ejb.StatelessSessionContainer.internalInvoke(StatelessSessionContainer.java:322)
      at org.jboss.ejb.Container.invoke(Container.java:674)
      at sun.reflect.GeneratedMethodAccessor43.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:324)
      at org.jboss.mx.capability.ReflectedMBeanDispatcher.invoke(ReflectedMBeanDispatcher.java:284)
      at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:549)
      at org.jboss.invocation.jrmp.server.JRMPInvoker.invoke(JRMPInvoker.java:359)
      at sun.reflect.GeneratedMethodAccessor47.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:324)
      at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:261)
      at sun.rmi.transport.Transport$1.run(Transport.java:148)
      at java.security.AccessController.doPrivileged(Native Method)
      at sun.rmi.transport.Transport.serviceCall(Transport.java:144)
      at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:460)
      at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:701)
      at java.lang.Thread.run(Thread.java:536)

      I found that I can't set a field value for a PK in the ejbPostCreate. This has to be done in the ejbCreate.

      So I added a: setMessageId(MessageLocal.getMessageId()) in the ejbCreate and removed the setMessageLocal(messageLocal) in the ejbPostCreate to be able to make it work.

      Now, is that good? I guess not. I'm probably screwing up all the relations. Well then, how can I fix that?

      Any hints?

      HEre are some of my xml files related to this problem:
      <!-- ejb-jar.xml CUT & PASTE -->


      <![CDATA[This class is part of CGI's BusinessPortal, and it is a CMP EJB accessing the TB_RPMESSAGEALERT table.]]>

      <ejb-name>MessageAlert</ejb-name>

      <local-home>com.cgi.portal.messagealert.interfaces.MessageAlertLocalHome</local-home>
      com.cgi.portal.messagealert.interfaces.MessageAlertLocal

      <ejb-class>com.cgi.portal.messagealert.ejb.MessageAlertCMP</ejb-class>
      <persistence-type>Container</persistence-type>
      <prim-key-class>java.lang.Long</prim-key-class>
      False
      <cmp-version>2.x</cmp-version>
      <abstract-schema-name>MessageAlert</abstract-schema-name>
      <cmp-field >
      <![CDATA[Returns the messageId]]>
      <field-name>messageId</field-name>
      </cmp-field>
      <cmp-field >
      <![CDATA[Returns the endDate]]>
      <field-name>endDate</field-name>
      </cmp-field>
      <cmp-field >
      <![CDATA[Returns the beginDate]]>
      <field-name>beginDate</field-name>
      </cmp-field>
      <cmp-field >
      <![CDATA[Returns the modDate]]>
      <field-name>modDate</field-name>
      </cmp-field>
      <cmp-field >
      <![CDATA[Returns the email]]>
      <field-name>email</field-name>
      </cmp-field>
      <cmp-field >
      <![CDATA[Returns the loginId]]>
      <field-name>loginId</field-name>
      </cmp-field>
      <cmp-field >
      <![CDATA[Returns the important]]>
      <field-name>important</field-name>
      </cmp-field>
      <cmp-field >
      <![CDATA[Returns the type]]>
      <field-name>type</field-name>
      </cmp-field>
      <cmp-field >
      <![CDATA[Returns the loginCategoryH]]>
      <field-name>loginCategoryH</field-name>
      </cmp-field>
      <primkey-field>messageId</primkey-field>


      <query-method>
      <method-name>findAll</method-name>
      <method-params>
      </method-params>
      </query-method>
      <result-type-mapping>Local</result-type-mapping>
      <ejb-ql><![CDATA[SELECT OBJECT(o) FROM MessageAlert o]]></ejb-ql>


      <![CDATA[END_DATE is not indexed.]]>
      <query-method>
      <method-name>findByEndDate</method-name>
      <method-params>
      <method-param>java.sql.Timestamp</method-param>
      </method-params>
      </query-method>
      <result-type-mapping>Local</result-type-mapping>
      <ejb-ql><![CDATA[SELECT DISTINCT OBJECT(o) FROM MessageAlert o WHERE o.endDate = ?1]]></ejb-ql>


      <![CDATA[BEGIN_DATE is not indexed.]]>
      <query-method>
      <method-name>findByBeginDate</method-name>
      <method-params>
      <method-param>java.sql.Timestamp</method-param>
      </method-params>
      </query-method>
      <result-type-mapping>Local</result-type-mapping>
      <ejb-ql><![CDATA[SELECT DISTINCT OBJECT(o) FROM MessageAlert o WHERE o.beginDate = ?1]]></ejb-ql>


      <![CDATA[MOD_DATE is not indexed.]]>
      <query-method>
      <method-name>findByModDate</method-name>
      <method-params>
      <method-param>java.sql.Timestamp</method-param>
      </method-params>
      </query-method>
      <result-type-mapping>Local</result-type-mapping>
      <ejb-ql><![CDATA[SELECT DISTINCT OBJECT(o) FROM MessageAlert o WHERE o.modDate = ?1]]></ejb-ql>


      <![CDATA[EMAIL is not indexed.]]>
      <query-method>
      <method-name>findByEmail</method-name>
      <method-params>
      <method-param>java.lang.String</method-param>
      </method-params>
      </query-method>
      <result-type-mapping>Local</result-type-mapping>
      <ejb-ql><![CDATA[SELECT DISTINCT OBJECT(o) FROM MessageAlert o WHERE o.email = ?1]]></ejb-ql>


      <![CDATA[LOGIN_ID is not indexed.]]>
      <query-method>
      <method-name>findByLoginId</method-name>
      <method-params>
      <method-param>long</method-param>
      </method-params>
      </query-method>
      <result-type-mapping>Local</result-type-mapping>
      <ejb-ql><![CDATA[SELECT DISTINCT OBJECT(o) FROM MessageAlert o WHERE o.loginId = ?1]]></ejb-ql>


      <![CDATA[IMPORTANT is not indexed.]]>
      <query-method>
      <method-name>findByImportant</method-name>
      <method-params>
      <method-param>java.lang.String</method-param>
      </method-params>
      </query-method>
      <result-type-mapping>Local</result-type-mapping>
      <ejb-ql><![CDATA[SELECT DISTINCT OBJECT(o) FROM MessageAlert o WHERE o.important = ?1]]></ejb-ql>


      <![CDATA[TYPE is not indexed.]]>
      <query-method>
      <method-name>findByType</method-name>
      <method-params>
      <method-param>java.lang.String</method-param>
      </method-params>
      </query-method>
      <result-type-mapping>Local</result-type-mapping>
      <ejb-ql><![CDATA[SELECT DISTINCT OBJECT(o) FROM MessageAlert o WHERE o.type = ?1]]></ejb-ql>


      <![CDATA[LOGIN_CATEGORY_H is not indexed.]]>
      <query-method>
      <method-name>findByLoginCategoryH</method-name>
      <method-params>
      <method-param>java.lang.String</method-param>
      </method-params>
      </query-method>
      <result-type-mapping>Local</result-type-mapping>
      <ejb-ql><![CDATA[SELECT DISTINCT OBJECT(o) FROM MessageAlert o WHERE o.loginCategoryH = ?1]]></ejb-ql>

      <!-- Write a file named ejb-finders-MessageAlertBean.xml if you want to define extra finders. -->


      <![CDATA[This class is part of CGI's BusinessPortal, and it is a CMP EJB accessing the TB_RPMESSAGEALERT_DESC table.]]>

      <ejb-name>MessageAlertDesc</ejb-name>

      <local-home>com.cgi.portal.messagealert.interfaces.MessageAlertDescLocalHome</local-home>
      com.cgi.portal.messagealert.interfaces.MessageAlertDescLocal

      <ejb-class>com.cgi.portal.messagealert.ejb.MessageAlertDescCMP</ejb-class>
      <persistence-type>Container</persistence-type>
      <prim-key-class>com.cgi.portal.pks.messagealert.MessageAlertDescPK</prim-key-class>
      False
      <cmp-version>2.x</cmp-version>
      <abstract-schema-name>MessageAlertDesc</abstract-schema-name>
      <cmp-field >
      <![CDATA[Returns the messageId]]>
      <field-name>messageId</field-name>
      </cmp-field>
      <cmp-field >
      <![CDATA[Returns the cdLang]]>
      <field-name>cdLang</field-name>
      </cmp-field>
      <cmp-field >
      <![CDATA[Returns the subject]]>
      <field-name>subject</field-name>
      </cmp-field>
      <cmp-field >
      <![CDATA[Returns the url]]>
      <field-name>url</field-name>
      </cmp-field>
      <cmp-field >
      <![CDATA[Returns the content]]>
      <field-name>content</field-name>
      </cmp-field>


      <query-method>
      <method-name>findAll</method-name>
      <method-params>
      </method-params>
      </query-method>
      <result-type-mapping>Local</result-type-mapping>
      <ejb-ql><![CDATA[SELECT OBJECT(o) FROM MessageAlertDesc o]]></ejb-ql>


      <![CDATA[SUBJECT is not indexed.]]>
      <query-method>
      <method-name>findBySubject</method-name>
      <method-params>
      <method-param>java.lang.String</method-param>
      </method-params>
      </query-method>
      <result-type-mapping>Local</result-type-mapping>
      <ejb-ql><![CDATA[SELECT DISTINCT OBJECT(o) FROM MessageAlertDesc o WHERE o.subject = ?1]]></ejb-ql>


      <![CDATA[URL is not indexed.]]>
      <query-method>
      <method-name>findByUrl</method-name>
      <method-params>
      <method-param>java.lang.String</method-param>
      </method-params>
      </query-method>
      <result-type-mapping>Local</result-type-mapping>
      <ejb-ql><![CDATA[SELECT DISTINCT OBJECT(o) FROM MessageAlertDesc o WHERE o.url = ?1]]></ejb-ql>


      <![CDATA[CONTENT is not indexed.]]>
      <query-method>
      <method-name>findByContent</method-name>
      <method-params>
      <method-param>java.lang.String</method-param>
      </method-params>
      </query-method>
      <result-type-mapping>Local</result-type-mapping>
      <ejb-ql><![CDATA[SELECT DISTINCT OBJECT(o) FROM MessageAlertDesc o WHERE o.content = ?1]]></ejb-ql>

      <!-- Write a file named ejb-finders-MessageAlertDescBean.xml if you want to define extra finders. -->



      <ejb-relation >
      <ejb-relation-name>TB_RPMESSAGEALERT-cmp20-TB_RPMESSAGEALERT_DESC-cmp20</ejb-relation-name>

      <ejb-relationship-role >
      <ejb-relationship-role-name>TB_RPMESSAGEALERT_DESC-cmp20-has-TB_RPMESSAGEALERT-cmp20</ejb-relationship-role-name>
      Many
      <relationship-role-source >
      <ejb-name>MessageAlertDesc</ejb-name>
      </relationship-role-source>
      <cmr-field >
      <cmr-field-name>messageAlert</cmr-field-name>
      </cmr-field>
      </ejb-relationship-role>

      <ejb-relationship-role >
      <ejb-relationship-role-name>TB_RPMESSAGEALERT-cmp20-has-TB_RPMESSAGEALERT_DESC-cmp20</ejb-relationship-role-name>
      One
      <relationship-role-source >
      <ejb-name>MessageAlert</ejb-name>
      </relationship-role-source>
      <cmr-field >
      <cmr-field-name>messageAlertDescs</cmr-field-name>
      <cmr-field-type>java.util.Collection</cmr-field-type>
      </cmr-field>
      </ejb-relationship-role>

      </ejb-relation>

      <!-- jbosscmp-jdbc.xml CUT & PASTE -->

      <ejb-name>MessageAlertDesc</ejb-name>
      java:/OracleUMDS
      <datasource-mapping>Oracle8</datasource-mapping>
      <create-table>false</create-table>
      <remove-table>false</remove-table>
      <read-only>false</read-only>
      <pk-constraint>true</pk-constraint>
      <table-name>TB_RPMESSAGEALERT_DESC</table-name>

      <cmp-field>
      <field-name>cdLang</field-name>
      <column-name>CD_LANG</column-name>

      </cmp-field>
      <cmp-field>
      <field-name>content</field-name>
      <column-name>CONTENT</column-name>

      </cmp-field>
      <cmp-field>
      <field-name>messageId</field-name>
      <column-name>MESSAGE_ID</column-name>

      </cmp-field>
      <cmp-field>
      <field-name>subject</field-name>
      <column-name>SUBJECT</column-name>

      </cmp-field>
      <cmp-field>
      <field-name>url</field-name>
      <column-name>URL</column-name>

      </cmp-field>

      <!-- merge point: jbosscmp-jdbc-load-{0}.xml -->



      <ejb-name>MessageAlert</ejb-name>
      java:/OracleUMDS
      <datasource-mapping>Oracle8</datasource-mapping>
      <create-table>false</create-table>
      <remove-table>false</remove-table>
      <read-only>false</read-only>
      <pk-constraint>true</pk-constraint>
      <table-name>TB_RPMESSAGEALERT</table-name>

      <cmp-field>
      <field-name>beginDate</field-name>
      <column-name>BEGIN_DATE</column-name>

      </cmp-field>
      <cmp-field>
      <field-name>email</field-name>
      <column-name>EMAIL</column-name>

      </cmp-field>
      <cmp-field>
      <field-name>endDate</field-name>
      <column-name>END_DATE</column-name>

      </cmp-field>
      <cmp-field>
      <field-name>important</field-name>
      <column-name>IMPORTANT</column-name>

      </cmp-field>
      <cmp-field>
      <field-name>loginCategoryH</field-name>
      <column-name>LOGIN_CATEGORY_H</column-name>

      </cmp-field>
      <cmp-field>
      <field-name>loginId</field-name>
      <column-name>LOGIN_ID</column-name>

      </cmp-field>
      <cmp-field>
      <field-name>messageId</field-name>
      <column-name>MESSAGE_ID</column-name>

      </cmp-field>
      <cmp-field>
      <field-name>modDate</field-name>
      <column-name>MOD_DATE</column-name>

      </cmp-field>
      <cmp-field>
      <field-name>type</field-name>
      <column-name>TYPE</column-name>

      </cmp-field>

      <!-- merge point: jbosscmp-jdbc-load-{0}.xml -->


      <ejb-relation>
      <ejb-relation-name>TB_RPMESSAGEALERT-cmp20-TB_RPMESSAGEALERT_DESC-cmp20</ejb-relation-name>
      <foreign-key-mapping/>
      <ejb-relationship-role>
      <ejb-relationship-role-name>TB_RPMESSAGEALERT_DESC-cmp20-has-TB_RPMESSAGEALERT-cmp20</ejb-relationship-role-name>
      </ejb-relationship-role>
      <ejb-relationship-role>
      <ejb-relationship-role-name>TB_RPMESSAGEALERT-cmp20-has-TB_RPMESSAGEALERT_DESC-cmp20</ejb-relationship-role-name>
      <fk-constraint>true</fk-constraint>
      <key-fields>
      <key-field>
      <field-name>messageId</field-name>
      <column-name>MESSAGE_ID</column-name>
      </key-field>
      </key-fields>
      </ejb-relationship-role>
      </ejb-relation>