I'm making the assumption that you're planning to use the latest EJB
spec, and so you'll also be using CMP 2.x
On Fri, May 31, 2002 at 01:58:48PM +0200, Bj�rn Smolinski wrote:
>
> I am a newbie at EJB peculiar CMR and have a problem with a solved
> N:M relation. I have found a theoretical solution for this in the
> "EJB-Professional" Book, but don't know how to map it to a working
> EJB.
>
> I have 3 Tables:
> a) Order
> b) Items
> c) realtion between Order and Item (solved N:M), includes just the
> foreign keys of Order and Item
Back to basics. Assuming that you're going from scratch here, you just
need to create your EJBs for Order and Item. When JBoss deploys them,
it should create the necessary tables, and if you've got the
relationships mapped out right in your ejb-jar.xml file, then JBoss
will create the relationship table for you. You can modify the names
of the tables using the jbosscmp-jdbc.xml file. For the format of
that, take a look at it's DTD (which I think are included in the JBoss
RC3 bundle)
> The book says, i need two EntityBeans OrderBean and ItemBean and one
> CMR-Field at OrderBean. With the statement 'SELECT OBJECT(o) FROM
> ORDER o, IN(o.item) i' I should get all orders with the
> corresponding items.
So, in the Order EJB, you have a CMR field called "item". In your
bean's code this looks like:
public abstract Collection getItem();
public abstract void setItem( Collection items );
As a stylistic note, you might find your code a little easier to read
any methods that return Collections/Sets are named after the plural
("items" in this case) In your ejb-jar.xml file, in the relationships
section, you should have something that looks a little like:
<ejb-relation>
<ejb-relation-name>Order-Items</ejb-relation-name>
<ejb-relationship-role>
<ejb-relationship-role-name>Order-Has-Items</ejb-relationship-role-name>
<multiplicity>Many</multiplicity>
<relationship-role-source>
<ejb-name>order</ejb-name>
</relationship-role-source>
<cmr-field>
<cmr-field-name>items</cmr-field-name>
<cmr-field-type>java.util.Collection</cmr-field-type>
</cmr-field>
</ejb-relationship-role>
<ejb-relationship-role>
<ejb-relationship-role-name>Items-Are-Owned-By-Order</ejb-relationship-role-name>
<multiplicity>Many</multiplicity>
<relationship-role-source>
<ejb-name>item</ejb-name>
</relationship-role-source>
</ejb-relationship-role>
</ejb-relation>
This sets up the required CMR fields (in this case a unidirectional
M:N mapping between the Order and the Item) You'll prolly have to
fiddle with the "ejb-name" field. You should _not_ have a reference to
"items" in the list of CMP fields in your Order EJB's declaration:
this is CMR, not CMP ;)
BTW, your EJB-QL isn't quite right: how do you which Item you're
looking for?
SELECT DISTINCT OBJECT(o) FROM Orders AS o, IN (o.items) AS i WHERE i.key=?1
might be closer to the mark, but is prolly wrong (correct me someone!)
I'm assuming that you've got your EJBs declared okay?
> Now I don't know how to initialize the CMR-Field and and all the
> other EJB QL stuff. What kind of object will i receive, my opinion
> is an Order-Object, but where are the item informations?
If you're using CMR, it's a Really Good Thing to use local interfaces
(in fact, I believe that the spec demands it) So, create an example of
your LocalHome, create or obtain your Item's local interface using a
finder/create method, and then get the Collection of Items from Order
and add your new item:
// Get your Item's LocalHome
InitialContext initial = new InitialContext();
Object objref = initial.lookup( ITEM_JNDI_NAME );
ItemLocalHome ilh = (ItemLocalHome) objref;
// Create the item. NB: Local interface being used
ItemLocal item = ilh.create();
// Get the Order LocalHome object
InitialContext initial = new InitialContext();
Object objref = initial.lookup( ORDER_JNDI_NAME );
OrderLocalHome olh = (OrderLocalHome) objref;
// Find the appropriate order. Again, use the local interface
OrderLocal order = olh.findByPrimaryKey( primaryKey );
// Now add the item to the order
order.getItems().add( item );
Obviosuly, there's no error checking there, but that's the basic
idea.
> Sorry the newbie question, but i'm really stuck in this and there
> was no examplecode in the book.
Hope that helps. You might find it instructive to browse the latest
version of the Java PetStore for some concrete examples.
Cheers,
Simon
--
"Why waste time learning when ignorance is instantaneous?"
-- Hobbes
_______________________________________________________________
Don't miss the 2002 Sprint PCS Application Developer's Conference
August 25-28 in Las Vegas -- http://devcon.sprintpcs.com/adp/index.cfm
_______________________________________________
JBoss-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-user