<vendor>
Some products provide support for EJB object references in
entity beans, some do not. For example, IAS4 provides support
such references, but only if the primary key is non-compound.
In the below quoted message, I discuss a workaround for this
limitation in IAS4.
The same workaround can be used with other products to
"simulate" support for references.
If you want to follow the full discussion, please go to:
news://newsgroups.borland.com/inprise.public.appserver
and look for the thread titled:
Compound Primary Keys and One-to-one Associations
<vendor>
-jkw
===================================================================
"Jonathan K. Weedon" wrote:
>
> Steve,
>
> Since you started this topic, which has discussed using
> compound primary keys, I thought it might be helpful
> for me to suggest another technique which can be used to
> work around a deficiency in the CMP engine's support
> for compound primary keys when used as foreign keys.
>
> Currently, the CMP engine does not support compound
> primary keys (e.g., wrapper PKs which contain more than
> one public field) when used as foreign keys. (Recall that
> wrapper PKs which contain a single public field are
> officially deprecated in EJB 1.1, so we are only
> concerned about the compound case.)
>
> There are two places where you would like the CMP engine
> to use compound primary keys as foreign keys:
>
> 1) As parameters in finder methods.
> 2) As container managed fields.
>
> Case (1) is discussed fully in the thread "Errors in
> association based CMP". Basically, to support compound
> primary keys in finder methods, I suggest augmenting the
> home interface with a finder method taking the primary
> key class. This method can be implemented by the CMP
> engine. Then, you can implement the original finder method
> in the bean by calling the PK-based finder.
>
> Case (2) is supported in a similar way, by using the CMP
> engine for most of the work, and then writing some bean
> code to do the rest. A further benefit of this approach,
> is that all the "work-around" code will go into its own
> sub-class, so when a subsequent release of IAS supports
> compound primary keys as foreign keys, this subclass can
> be discarded, and the original base class can be used.
>
> Consider, we have the an EJB called Simple which refers to
> another EJB, called Compound. As expected, the Compound
> primary key is compound. We might have the following
> implementation of the Simple EJB:
>
> public class SimpleBean implements EntityBean {
>
> // this field is the primary key
> public String name;
> // this field is a reference to a compound bean
> public Compound compound;
>
> public String ejbCreate(String n, Compound c) {
> name = n;
> compound = c;
> return null;
> }
>
> // other stuff omitted
>
> }
>
> Compound bean has the following implementation:
>
> public class CompoundBean implements EntityBean {
>
> public int compoundPrimaryKey1;
> public int compoundPrimaryKey2;
> public String otherContainerManagedField;
>
> // other stuff omitted
> }
>
> The compoundPrimaryKey1 and compoundPrimaryKey2 fields
> obviously correspond to the fields in the compound primary key:
>
> public class CompoundPK implements Serializable {
>
> public int compoundPrimaryKey1;
> public int compoundPrimaryKey2;
>
> }
>
> Now, as I said before, the CMP engine will not be able to
> map the field SimpleBean.compound, due to a lack of support for
> compound primary keys used as foreign keys.
>
> A workaround is to write a subclass of SimpleBean, which converts
> back and forth between the field SimpleBean.compound and the
> primary key CompoundPK. In code, this would be:
>
> public class SimpleBeanWithWorkaround extends SimpleBean {
>
> // add one more container managed field to the class
> public CompoundPK compoundPK;
>
> // this field is not container managed...
> private CompoundHome compoundHome;
>
> public String ejbCreate(String n, Compound c) {
> super.ejbCreate(n, c);
> compoundPK = (CompoundPK) compound.getPrimaryKey();
> }
>
> public void ejbLoad() {
> compound = compoundHome.findByPrimaryKey(compoundPK);
> super.ejbLoad();
> }
>
> public void ejbStore() {
> compoundPK = (CompoundPK) compound.getPrimaryKey();
> }
>
> public void setEntityContext(javax.ejb.EntityContext unused) {
> javax.naming.Context ctx = new javax.naming.InitialContext();
> compoundHome = (CompoundHome) javax.rmi.PortableRemoteObject.
> narrow(ctx.lookup("java:comp/env/ejb/compound"), CompoundHome.class);
> }
>
> // other stuff omitted
>
> }
>
> Then, when we go to deploy the beans, instead of having the
> field SimpleBean.compound be container managed, we have the
> field SimpleBeanWithWorkaround.compoundPK be container managed.
> The SimpleBeanWithWorkaround is actually providing the value
> for the field SimpleBean.compound, by using the container to
> manage the value of SimpleBeanWithWorkaround.compoundPK, and
> transalting between the reference and the primary key.
>
> Using the DDEditor, we will specify the Persitence of the bean
> SimpleBeanWithWorkaround. Here, we can specify that the two
> fields:
>
> SimpleBeanWithWorkaround.compoundPK.compoundPrimaryKey1
> SimpleBeanWithWorkaround.compoundPK.compoundPrimaryKey2
>
> map to the corresponding foreign key fields in the database
> table for the Simple bean:
>
> compound_fk1
> compound_fk2
>
> or whatever the columns are called in the table.
>
> -jkw
===================================================================
Rip Riley wrote:
>
> I was just asking myself the same question. Anyone
> have any suggestions on how to do the mapping. I am
> personally using WebLogic 4.5.1.
>
> Thanks
>
> --- DaveFord <[EMAIL PROTECTED]> wrote:
> > Suppose I have an EJB like this:
> >
> > public class PersonBean implelemts EntityBean{
> >
> > public String key;
> > public String firstName;
> > public String lastName;
> > public Company company;
> >
> > etc...
> > }
> >
> > How does ejb handle the object reference when it
> > goes to save it?
> >
> >
> > Thanks in advance.
> >
> > Dave
> >
> >
> ===========================================================================
> > 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".
> >
> >
>
> __________________________________________________
> Do You Yahoo!?
> Talk to your friends online with Yahoo! Messenger.
> http://im.yahoo.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".
===========================================================================
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".