...
I have two tables in my database, Alfa and Beta.
They have the following columns
Alfa - ID (String), primary key
- DESCRIPTION (String)
Beta - Alfa_ID (String) \
- PLACE (int) _ compound primary key
- DESCRIPTION (String)
I have mapped them succesfully, and created a helper class
Beta_PK which I use to persist table Beta. Without relationships
the tables and persistance work perfectly.
These tables should have unidirectional 1:n relationships,
better said Alfa-contains-many-Betas. I'd like to be able to do
Collection betas=AlfaLocalInterface.getBetas()
However I can't map the relationship if my life depended on it.
Can somebody please tell me how do the relationship parts of
ejb-jar.xml and the rest of DDs look like (I use Weblogic 8.1)?
Basically it's the compound key which gives me the most trouble.
This is a bit tricky, and actually often needs implementation-specific
tricks.
A compound key by itself usually isn't much of a trouble, but there are some
intricate details depending on the EJB-conainer itself.
Anyway, here are som discoveries I made when doing the same thing:
- if the PK is adressing a cmr-field, it still have to declare the contained
fields as cmp-fields.
- if this cmr-field is a compund key, it's separate fields will be mapped
for the entity.
- This can mean that the fields will be "doubled" in the table in the db.
=======================================================
<?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>
<entity>
<ejb-name>Alfa</ejb-name>
<local-home>AlfaHome</local-home>
<local>Alfa</local>
<ejb-class>AlfaEJB</ejb-class>
<prim-key-class>java.lang.String</prim-key-class>
<primkey-field>id</primkey-field>
<persistence-type>Container</persistence-type>
<reentrant>False</reentrant>
<abstract-schema-name>Alfas</abstract-schema-name>
<cmp-field><field-name>id</field-name></cmp-field>
<cmp-field><field-name>description</field-name></cmp-field>
<query>
<query-method>
<method-name>findAll</method-name>
<method-params/>
</query-method>
<ejb-ql>SELECT OBJECT(o) FROM Alfas AS o</ejb-ql>
</query>
</entity>
<entity>
<ejb-name>Beta</ejb-name>
<local-home>BetaHome</local-home>
<local>Beta</local>
<ejb-class>BetaEJB</ejb-class>
<prim-key-class>Beta_PK</prim-key-class>
<persistence-type>Container</persistence-type>
<reentrant>False</reentrant>
<cmp-field><field-name>alfa_id</field-name></cmp-field>
<cmp-field><field-name>place</field-name></cmp-field>
<cmp-field><field-name>description</field-name></cmp-field>
<abstract-schema-name>Betas</abstract-schema-name>
<query>
<query-method>
<method-name>findAll</method-name>
<method-params/>
</query-method>
<ejb-ql>SELECT OBJECT(o) FROM Betas AS o</ejb-ql>
</query>
</entity>
</enterprise-beans>
<relationships>
<ejb-relation>
<ejb-relation-name>Alfa_Beta</ejb-relation-name>
<ejb-relationship-role>
<ejb-relationship-role-name>Alfa-has-Betas</ejb-relationship-role-name>
<multiplicity>One</multiplicity>
<relationship-role-source>
<ejb-name>Alfa</ejb-name>
</relationship-role-source>
<cmr-field>
<cmr-field-name>Betas</cmr-field-name>
<cmr-field-type>java.util.Collection</cmr-field-type>
</cmr-field>
</ejb-relationship-role>
<ejb-relationship-role>
<ejb-relationship-role-name>Betas-in-Alfa</ejb-relationship-role-name>
<multiplicity>Many</multiplicity>
<relationship-role-source>
<ejb-name>Beta</ejb-name>
</relationship-role-source>
<cmr-field>
<cmr-field-name>Alfa</cmr-field-name>
</cmr-field>
</ejb-relationship-role>
</ejb-relation>
</relationships>
</ejb-jar>
=======================================================
And here comes the implementation specific part.
In e.g. JBoss there's a possibility to map the cmp-fields to the
columnnames, that would be generated by the cmr-fields, so the "doubling"
never occurs. I'm not that familiar with Weblogic, but my guess is that it
has similar possibilities. In JBoss this would be in a file named
jbosscmp-jdbc.xml. If it's at all possible in Weblogic, the filename is of
course another. Weblogic might also have another syntax in the corresponding
file, but here's what it'd look like in JBoss:
=======================================================
<enterprise-beans>
<entity>
<ejb-name>Beta</ejb-name>
<cmp-field>
<field-name>alfa_id</field-name>
<column-name>alfa</column-name>
</cmp-field>
</entity>
</enterprise-beans>
=======================================================
I hope this at least gives a hint and a starting point on how you can
proceed.
// Bjorn A