I have a question about CMP Entity Bean relationships under EJB 2.0:

I want to create two entity beans, say one is called Group and one is called User.  
The group bean contains a name and other attributes.  The user bean has a bunch of its 
own attributes, one of which is a group.  A User can belong to only one Group, and a 
Group can have hundreds of thousands of Users.

If I were creating a straight Java application using JDBC to access the database, and 
did everything myself (no J2EE) I would have created a Group table and a User table.  
The User table would have a foreign key for whatever Group it belonged to, and the 
Group would NOT have any foreign keys back to the users.  After all, this is a 
One-To-Many (Group-To-User) relationship, and it need only be unidirectional.

Now, with J2EE, I figure I should create two CMP 2.0 beans.  The Group bean would have 
local (rather than remote) interfaces, and the User bean would be able to get and set 
the GroupLocal.  Of course, the User's remote interface would not allow access to the 
Group's local interface, but instead would be able to offer a GroupView object, or 
something like that.

(In fact, in the long run, it is unlikely the client will ever actually use the Entity 
Beans, but will use some sort of Session Bean as an intermediary).

So far, so good.  But I started getting confused when trying to write the deployment 
descriptor, especially when I got to the ejb-relation section.

Am I correct in doing the following?:

<ejb-relation>
  <ejb-relation-name>GroupUser</ejb-relation-name>
  <ejb-relationship-role>
    <ejb-relationship-role-name>GroupHasUsers</ejb-relationship-role-name>
    <multiplicity>One</multiplicity>
    <relationship-role-source>
      <ejb-name>Group</ejb-name>
    </relationship-role-source>
  </ejb-relationship-role>
  <ejb-relationship-role>
    <ejb-relationship-role-name>UserInGroup</ejb-relationship-role-name>
    <multiplicity>Many</multiplicity>
    <relationship-role-source>
      <ejb-name>User</ejb-name>
    </relationship-role-source>
    <cmr-field>
      <cmr-field-name>group</cmr-field-name>
    </cmr-field>
  </ejb-relationship-role>
</ejb-relation>

The reason I am confused as to whether or not this is right is that all the examples I 
have seen showing unidirectional one-to-many relationships are always showing the 
object with multiplicity "one" being the object with the reference (and it is always a 
Collection or a Set, therefore).  In my case, however, it is the object with 
multiplicity "many" that has the reference (and of course it is therefore a single 
object, rather than a Set or Collection).

I know I could simply forget about how I personally would have defined the tables, and 
instead say that the undirectionality is from Group to User, the way I usually see 
examples defined.  This way the Group contains a Collection or Set of Users, and the 
Users don't need to have a reference to the Group.  But I can't get the fact out of my 
head that this seems like it simply can't be implemented with any efficiency 
(especially when a Group is likely to have hundreds of thousands of Users).

Thanks for any help,
David

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to