The key ideas were
(1) Do not have set method for the foreign key field in the dependent ejbs create method.
(2) Use the relationship method of your primary EJB to associate a collection of your created foreign children. The container will then automatically put the primary key into the foreign key field.

解决方案 »

  1.   

    来个例子解释的详细点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.
      

  2.   

    但是就算
    setUserId(userid);
    setUserName(username);
    try{
          authHome=getAuthHome();
        Auth auth = authHome.findByPrimaryKey(authType);
        auth.getUser().add(this);
    仍会提示说:没有把authType插入啊?
    能再详细说说吗?
      

  3.   

    看例子就知道了
    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.
      

  4.   

    我试了除非你在entityA中的BankID改成可以为空,这样才能把BankID插入。否则,不行。