OK then here is the thing:When you use EJB relationships, you don't have to think only relational
database but also Object Oriented database. I mean you don't have to
store in an entityA field the PK of entityB but rather say to the
system, (in this case the EJB container) my entityA instance is linked
to this instance of entity B and the system (OO DB or the container is
taking care of the persistency of that specific instances' relation).For example:
EntityA represents BANK's users (PK= String userID + String bankID)
EntityB represents BANKs (PK= String bankID). A Bank may have several
users within the bank.Suppose you want to create a EJB relationship between users and banks.
So you create a CMR field in EntityA called rBank. This relationship
needs to be persisted so you specify in the deployment descriptor that
the relationship is related to the table Users, column field bankID
(which is, from a relational DB point of vue, a FOREIGN KEY). You need
to "tell" the container how it should persist the relationship. The DD
is there for that.Of course you have that column represented in the entityA as a CMP field
(bankID) so you may (NOT AN OBLIGATION) have defined (your IDE may have
done that for you automatically) a SET method for that CMP field.In EntityB you have a CMR field called rUsers for which the getRUsers
method return a Collection of EntityA instances. Again here, that CMR is
linked to the CMP field bankID in the table BANK for persistency
purposes.Note: CMR fields like CMP fields can have getters and setters.Now, you want to create a new user.>From the EntityA home interface you use the create method which will
return you a new EntityA instance.
Within the ejbCreate method of EntityA you may initialise the
relationship with a bank's instance. You can't use the setBankID of
EntityA (THAT's THE RULE in the SPEC) but you need to ge the bank's
instance (findByPK) and apply the method:
instanceB.getRUsers().add(this). Of course I do not recommend to do that
there.. But rather in a SessionBean.
So in the session bean:EntityA entityA = null;
EntityB entityB = null;
Try
{
        entityA = entityAHome.create(new PK);
        entityA.setXXX(...) for CMP fields not being part in a relation
(you can do the setter in the ejbCreate method).
        entityA.setBankID(bankID); /// THIS WILL CAUSE AN EXCEPTION
        entityB = entityBHome.findByPK(bankID);
        entityB.getRUsers().add(entityA);  //here the container will
update CMP field bankID in entityA with the right info based on the info
provided in the deployment descriptor). }Note: I'd recommend you not to provide any setters for CMP-CMR fields to
avoid any tentation!!
Note: in the case of a N-M table relationship, you need to have a
intermediate table (typical database technique) and CMR fields in
entnityA/B needs to be "physically" linked (in the DD) to that
intermediary table.

解决方案 »

  1.   

    It is the correct answer.I have tried myself.It is O.K.
    Good Luck!
      

  2.   

    you are right,i can create value into table as your way,but i have to set User table's fk allow to null.are you read the code about weblogic's sample,it have tow table as follows:
    CREATE TABLE bands (
       name VARCHAR(50),
       founder VARCHAR(50),
       startDate date,
       CONSTRAINT bands_pk PRIMARY KEY
         (name, founder));CREATE TABLE recordings (
       title VARCHAR(50),
       bandName VARCHAR(50),
       bandFounder VARCHAR(50),
       numberSold INT,
       sales NUMERIC(10, 2),
       recordingDate DATE,
       CONSTRAINT recordings_pk PRIMARY KEY
         (title, bandName, bandFounder),
       CONSTRAINT recordings_fk FOREIGN KEY
         (bandName, bandFounder)
         REFERENCES bands(name, founder));it use cmp to realize relationship,for fk name and founder it also use set**() method in record.create(*,*..),but it run success.i always analysis why it run?i agree you opinion that "When you use EJB relationships, you don't have to think only relational database but also Object Oriented database",use cmr need to think of concrete requirement.thank you!