This is a case where CMP would be nice, but if you don't have a sophisticated
CMP container then I offer the following as a BMP entity solution. (Disclaimer:
I wrote this very quickly so there are probably a few typos.).
Below is something I whipped together to demonstrate a solution I developed for
modeling relationships. In this case I am assuming (for no good reason) that
the relationships between your three business concepts are as follows:
Category --1------------------ 0..*--->Plateforms
Category --*------------------*------>Vendor
In this case I modeled Platforms as dependent objects of the Category. They are
not shared. The Plateform table (assuming RDBMS) has a foreign key that points
back to the platform's owning Category.
The Vendor is modeled as shared data (an entity bean) that joined to the
Category table via a Link table. Responsibility for managing the link records
is left to the Category, but the Vendor bean can use it to execute certain find
methods.
The idea I'm trying to communicate here is that the persistence logic does
always have to reside in the ejbLoad and ejbStore methods. If you are using BMP
then you can safely access data from business methods as well as the ejbLoad
and ejbStore -- the update will still be managed within the scope of a
transaction as long as those methods have the appropriate tx attributes.
public class CategoryBean extends EntityBean {
public long id;
public String categoryType;
public CategoryPK create(long id){
this.id = id;
}
public String getCategoryType( ){return categoryType;}
public void setCategoryType(String cat){this.categoryType = cat;}
public void ejbLoad( ){
// read the category type to the database using the id as a key
}
public void ejbStore( ){
// write category type to the database if it changes
}
public Collection platforms( ){
DataSource dataSource = jndiContext.lookup("java:comp/env/jdbc/mydb");
// use JDBC to perform a join from this category to the platform table.
Returns a result set of Platform records
Vector platforms = new Vector( );
while(resultSet.next()){
Platform platform = new Platform( );
platform.setID(resultSet.getLong("ID")):
platform.setValueX( resultSet.getString("ValueX"));
platform.setValueY(resultSet.getDouble("ValueY"));
...
platforms.addElement(plateform);
}
return platforms;
}
public void removePlatform(Plateform platform){
// use JDBC to remove the platform record with this category id as a
foreign key.
}
public void addPlatform(Plateform platform){
// use JDBC to add a platform record with this category id as a foreign
key.
}
public Collection vendors( ){
Object ref = jndiContext.lookup("java:comp/env/ejb/VendorHome");
VendorHome vendorHome = (VendorHome)
PortableRemoteObject.narrow(ref, VendorHome.class);
return vendorHome.findByCategory( this.id );// this may not work
depending on how the returned collection is implemented by the vendor
}
public void removeVendor(Vendor vendor){
// use JDBC to remove link record between this category and the vendor
}
public void addVendor(Vendor vendor) {
// use JDBC to add a link record between this category and the vendor
}
}
Roopa Kotha wrote:
> Hi everyone,
>
> How do I implement this sort of relationship in Entity beans
>
> 1. Category class
> String categorytype
> Vector platformlist
>
> 2. Platform class
> String platformDesc;
> Vector vendorlist
>
> 3. Vendor class
> String vendorname;
> Vector modellist;
>
> Any ideas/suggestions will be very helpfull.
>
> Thanks
> Roopa
>
> ===========================================================================
> 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".
--
Richard Monson-Haefel
Author of Enterprise JavaBeans, 2nd Edition
Published by O'Reilly & Associates
http://www.EjbNow.com
===========================================================================
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".