EJB relationships headache

H

horseface

Hi everybody. I am completely stuck on mapping on particular relationship
and would be immensely grateful if somebody would untangle my problem. I'll
try to present it as clearly as possible.

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.

I thank you for any kind of help/advice. Cheers!
 
B

Bjorn Abelli

...
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top