1 to many cmr with composite primary key
martin0 Mar 15, 2004 4:24 AMHi,
I'm trying to set up a relationship between account and role, for DatabaseServerLoginModule, for new user registration.
My T_ROLES table is
username, userRoles, roleGroup
and my T_ACCOUNT table is
userId, pwd
All three columns in T_ROLES form the primary key, and userId is the primary key for T_ACCOUNT.
The entities are declared as follows in ejb-jar.xml
<entity >
<description><![CDATA[]]></description>
<ejb-name>Account</ejb-name>
<local-home>xpetstore.domain.signon.interfaces.AccountLocalHome</local-home>
<local>xpetstore.domain.signon.interfaces.AccountLocal</local>
<ejb-class>xpetstore.domain.signon.ejb.AccountCMP</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.String</prim-key-class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>Account</abstract-schema-name>
<cmp-field >
<description><![CDATA[]]></description>
<field-name>userId</field-name>
</cmp-field>
<cmp-field >
<description><![CDATA[]]></description>
<field-name>passwordHash</field-name>
</cmp-field>
<primkey-field>userId</primkey-field>
<!-- Write a file named ejb-finders-AccountEJB.xml if you want to define extra finders. -->
</entity>
<entity >
<description><![CDATA[]]></description>
<ejb-name>Role</ejb-name>
<local-home>xpetstore.domain.signon.interfaces.RoleLocalHome</local-home>
<local>xpetstore.domain.signon.interfaces.RoleLocal</local>
<ejb-class>xpetstore.domain.signon.ejb.RoleCMP</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>xpetstore.domain.signon.interfaces.RolePK</prim-key-class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>Role</abstract-schema-name>
<cmp-field >
<description><![CDATA[]]></description>
<field-name>userId</field-name>
</cmp-field>
<cmp-field >
<description><![CDATA[]]></description>
<field-name>userRoles</field-name>
</cmp-field>
<cmp-field >
<description><![CDATA[]]></description>
<field-name>roleGroup</field-name>
</cmp-field>
<!-- Write a file named ejb-finders-RoleEJB.xml if you want to define extra finders. -->
</entity>
The relationship is
<ejb-relation >
<ejb-relation-name>account-roles</ejb-relation-name>
<ejb-relationship-role >
<ejb-relationship-role-name>account-has-roles</ejb-relationship-role-name>
<multiplicity>One</multiplicity>
<relationship-role-source >
<ejb-name>Account</ejb-name>
</relationship-role-source>
<cmr-field >
<cmr-field-name>roles</cmr-field-name>
<cmr-field-type>java.util.Collection</cmr-field-type>
</cmr-field>
</ejb-relationship-role>
<ejb-relationship-role >
<ejb-relationship-role-name>role-belongs_to-account</ejb-relationship-role-name>
<multiplicity>Many</multiplicity>
<cascade-delete/>
<relationship-role-source >
<ejb-name>Role</ejb-name>
</relationship-role-source>
</ejb-relationship-role>
</ejb-relation>
jbosscmp-jdbc.xml relationship is
<ejb-relation>
<ejb-relation-name>account-roles</ejb-relation-name>
<foreign-key-mapping/>
<ejb-relationship-role>
<ejb-relationship-role-name>account-has-roles</ejb-relationship-role-name>
<key-fields>
<key-field>
<field-name>userId</field-name>
<column-name>username</column-name>
</key-field>
</key-fields>
</ejb-relationship-role>
<ejb-relationship-role>
<ejb-relationship-role-name>role-belongs_to-account</ejb-relationship-role-name>
<key-fields/>
</ejb-relationship-role>
</ejb-relation>
What I find is that the userId is being written to the roleGroup field, even though I set the RolePK as follows:
RolePK r=new RolePK(account.getUserId(),"customer","Roles");
roles.add(getRoleLocalHome().create(r));
r=new RolePK(account.getUserId(),"caller_"+account.getUserId(),"CallerPrincipal");
roles.add(getRoleLocalHome().create(r));
// this stuff requires admin privilege
account.setRoles(roles);
I presume this error has something to do with it being a compound primary key, and is the last field of the key - I dont know.
Can anyone shed some light on why the userGroup fields don't have the values I set for them, but the value of userId instead. (RolePK is autogenerated by xdoclet)
Thanks
Martin