• Post Reply Bookmark Topic Watch Topic
  • New Topic

A simple Relationship among tables (Entity Beans)

 
DPP Patrick
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've found some difficulties to relate that tables.
I'm working with eclipse, xdoclet, jboss.

Table1:
tab1_id - PK
tab1_nm
tab1_ds

Table2:
tab2_id - PK
tab1_id - FK
tab2_nm
tab2_ds

Table3:
tab3_id - PK
tab2_nm - FK
tab3_nm
tab3_ds

How can I do it? Where can I start from?

Thanks all
Patrick
 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start from your entity beans (if you have any), specify your relationships
and your CMP fields, and GO !!
 
DPP Patrick
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have already created my Entity Beans through Eclipse and Lomboz.
But I don't know how to specify my relationships.

Could somebody give me an example according my example tables?

Thanks all
 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DPP, I suggest you look at J2EE tutorial at java.sun.com which contains examples about EJB applications.
and take a look at :
http://www.onjava.com/pub/a/onjava/excerpt/ejb4_chap4/index.html
http://www.onjava.com/pub/a/onjava/excerpt/ejb4_chap4/index1.html
you specify your relations in ejb-jar.xml and in your component interface.
 
DPP Patrick
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear John Todd, thanks for those links.
I've already read some chapters that book.

But my really problem is where I should write the xdoclet tags to make a relation among entity beans. For example:

In Table1Bean:

/**
* @ejb.relation name = "Table1-Table2"
* role-name = "Table1-do-table2"
*
* @jboss.relation related-pk-field = "tab1_id"
*
*/
public abstract java.util.Collection getTables2();
public abstract void settables2(java.util.Collection tables2);


In Table2Bean:

/**
* @ejb.relation name = "Table1-Table2"
* role-name = "Table2-da-Table1"
*
* @jboss.relation fk-column = "tab1_id"
*
*/
public abstract Table1 gettable1();
public abstract void setTable1(Table1 table1);

And the ejb-jar generated:
<ejb-relation-name>table1-table2</ejb-relation-name>
<ejb-relationship-role >
<ejb-relationship-role-name>table2-da-table1</ejb-relationship-role-name>
<multiplicity>Many</multiplicity>
<relationship-role-source >
<ejb-name>Table1</ejb-name>
</relationship-role-source>
<cmr-field >
<cmr-field-name>table2</cmr-field-name>
</cmr-field>
</ejb-relationship-role>

<ejb-relationship-role >
<ejb-relationship-role-name>table1-do-tables2</ejb-relationship-role-name>
<multiplicity>One</multiplicity>
<relationship-role-source >
<ejb-name>Table2</ejb-name>
</relationship-role-source>
<cmr-field >
<cmr-field-name>table1</cmr-field-name>
<cmr-field-type>java.util.Collection</cmr-field-type>
</cmr-field>
</ejb-relationship-role>

But when I deploy I receive this message:

15:45:08,215 ERROR [EntityContainer] Starting failed jboss.j2ee:jndiName=Table1,service=EJB
org.jboss.deployment.DeploymentException: Role: Table1-da-Table2 with multiplicity many using foreign-key mapping is not allowed to have key-fields


What is it wrong in the relationship?

Thanks all

Patrick
 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok.
look at your DD :
<ejb-relationship-role >
<ejb-relationship-role-name>table2-da-table1</ejb-relationship-role-name>
<multiplicity>Many</multiplicity>
<relationship-role-source >
<ejb-name>Table1</ejb-name>
</relationship-role-source>
<cmr-field >
<cmr-field-name>table2</cmr-field-name>
</cmr-field>
</ejb-relationship-role>
Table1 should have multiplicity One not many.
look at your code, Table1 has two methods :
Collection getTables2( ) // and setter
so this side of the relation should be :

and the second side is :

I am sure that you are using local view, right ?
make sure that you are using the same relation name in jboss.xml
my advice for you :
buy a good EJB book and don't use xdoclet if you are not sure where to set the attributes.
xdoclet may add to your life tons of heartace if you are not sure about the use and the location of the attributes.
hope this help.
 
DPP Patrick
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, thanks a lot.

What book would you advice me to buy?
 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
theserverside.com has the complete copy of Mastering Enterprise Java Beans 2.0
if you are entresting in EJB 2.1 , I will recommend EJB 4 edition by Richard Monson Haefel.
it is a great book and has changed my mind about EJB.
good luck.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by DPP Patrick:
But my really problem is where I should write the xdoclet tags to make a relation among entity beans.

First, I'd recommend getting yourself on to the XDoclet user's mailing list. The developers and users are pretty active there.

Second, here is a one-to-many bidirectional relationship from my application that you can use as an example. Post again if you need more assistance.
 
DPP Patrick
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, thanks a lot.
Following your example:

User <-1----N-> Address
id user_id
UserEJB.java:
/**
* @ejb.interface-method
* @ejb.transaction type="Supports"
* @ejb.relation
* name="User-Address"
* role-name="User-has-many-Addresses"
* cascade-delete="no"
*/
public abstract Collection getAddresses ( ) ;
public abstract void setAddresses ( Collection addresses ) ;

AddressEJB.java:
/**
* @ejb.interface-method
* view-type="local"
* @ejb.transaction type="Supports"
* @ejb.relation
* name="User-Address"
* role-name="Addresses-have-a-User"
* cascade-delete="yes"
* @weblogic.column-map
* foreign-key-column="user_id"
* key-column="id"
*/
public abstract UserLocal getUser ( ) ;
public abstract void setUser ( UserLocal user ) ;

it generated:

ejb-jar:
<ejb-relation >
<ejb-relation-name>User-Address</ejb-relation-name>
<ejb-relationship-role >
<ejb-relationship-role-name>Addresses-have-a-User</ejb-relationship-role-name>
<multiplicity>Many</multiplicity>
<cascade-delete/>
<relationship-role-source >
<ejb-name>Address</ejb-name>
</relationship-role-source>
<cmr-field >
<cmr-field-name>user</cmr-field-name>
</cmr-field>
</ejb-relationship-role>

<ejb-relationship-role >
<ejb-relationship-role-name>User-has-many-Addresses</ejb-relationship-role-name>
<multiplicity>One</multiplicity>
<relationship-role-source >
<ejb-name>User</ejb-name>
</relationship-role-source>
<cmr-field >
<cmr-field-name>addresses</cmr-field-name>
<cmr-field-type>java.util.Collection</cmr-field-type>
</cmr-field>
</ejb-relationship-role>

</ejb-relation>


jbosscmp-jdbc:

<ejb-relation>
<ejb-relation-name>User-Address</ejb-relation-name>
<foreign-key-mapping/>
<ejb-relationship-role>
<ejb-relationship-role-name>Addresses-have-a-User</ejb-relationship-role-name>
<key-fields/>
</ejb-relationship-role>
<ejb-relationship-role>
<ejb-relationship-role-name>User-has-many-Addresses</ejb-relationship-role-name>
<key-fields/>
</ejb-relationship-role>
</ejb-relation>


When I deploy that I receive this message:
Atleast one role of a foreign-key mapped relationship must have key fields (or <primkey-field> is missing from ejb-jar.xml): ejb-relation-name=User-Address

Then in jbosscmp-jdbc I insert this:
..
<ejb-relationship-role-name>User-has-many-Addresses</ejb-relationship-role-name>
<key-fields>
<key-field>
<field-name>user_id</field-name>
<column-name>id</column-name>
</key-field>
</key-fields>
..

And I receive this:

Role 'User-has-many-Addresses' on Entity Bean 'User' : CMP field for key not found: field name='id'

Why isn't it finding the id?

Thanks all
Patrick
 
Sloan Bowman
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using XDoclet specify the @ejb.pk-field tag on the primary id set method of your entity bean. For example:



Hope this helps.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!