Entity B does not really 'exist' until after ejbCreate returns, thus it cannot participate in any relationships at that time.
Put the code entityB.getRUsers().add(entityA) in ejbPostCreate. Alvin Wang wrote: > Also, I found I cannot do this work in ejbCreate(). (that means call > entityB.getRUsers().add(entityA) in entityB's ejbCreate, because I want to > create entityA when I create entityB) The exception says it voilates the > Spec. Do you know why? > > > > -----Original Message----- > From: Alvin Wang [mailto:[EMAIL PROTECTED]] > Sent: Monday, March 11, 2002 4:35 PM > To: Daniel De Luca; [EMAIL PROTECTED] > Subject: RE: An CMR related exception > > > Daniel, it works! Thank you very much! I really appreciate all your patience > and kindness. > > However, I got another exception, since I put it in a method that does not > support transaction: > "javax.ejb.EJBException: Attempt to access a collection valued cmr-field > outside > the scope of a transaction." > > So do I have to put it in a transaction scope? > > > > -----Original Message----- > From: A mailing list for Enterprise JavaBeans development > [mailto:[EMAIL PROTECTED]]On Behalf Of Daniel De Luca > Sent: Monday, March 11, 2002 11:02 AM > To: [EMAIL PROTECTED] > Subject: Re: An CMR related exception > > > Alvin, > > 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. > > > Daniel > > -----Original Message----- > From: Alvin Wang [mailto:[EMAIL PROTECTED]] > Sent: lundi 11 mars 2002 16:14 > To: Daniel De Luca; [EMAIL PROTECTED] > Subject: RE: An CMR related exception > > > Daniel, I feel that my problem is not initializing a PK, but any field > that is mapped to a CMR. Again, the exception is as below: > "javax.ejb.EJBException: When a cmp-field and a cmr-field (relationship) > are mapped to the same column, the setXXX method for the cmp-field may > not be called. The cmp-field is read-only." > > Yeah, the XXX happens to be a foreign key in my case > > > -----Original Message----- > From: A mailing list for Enterprise JavaBeans development > [mailto:[EMAIL PROTECTED]]On Behalf Of Daniel De Luca > Sent: Monday, March 11, 2002 9:52 AM > To: [EMAIL PROTECTED] > Subject: Re: An CMR related exception > > > Alvin > > Yes entityA is an existing instance which has a PK. > It won't be possible for you to use your PK in a relationship between > entities. If your instanceA PK and instanceB PK are used for the > relationship between each other, you need to change the PK of the > corresponding entities to make them relationship independent. This mean, > create a new CMP field in both entities that will be used as PK. Then > use the old PKs as "normal" fields (non used as PK) in order to be able > to used them as CMR and without being forced to initialize them in the > ejbCreate method. > > If this is not giving you a solution to your problem, could you send the > code of your entities (puttin in comment which are the field that are > used in your relationship and which one are PKs). > > Hope this help > > Daniel > > -----Original Message----- > From: Alvin Wang [mailto:[EMAIL PROTECTED]] > Sent: lundi 11 mars 2002 15:44 > To: Daniel De Luca; [EMAIL PROTECTED] > Cc: [EMAIL PROTECTED] > Subject: RE: An CMR related exception > > > Daniel, I read the section you pointed out. I did not find either how it > is related to my problem and how your solution solves my problem. > > in "entityBRef.getREntityA().add(entityARef)", is entityARef an existing > Bean A instance initialized with PK? Then when/how did that XXX field > INITIALIZED? > > I'm a little slow. Please give me details. Thanks! > > > > -----Original Message----- > From: Daniel De Luca [mailto:[EMAIL PROTECTED]] > Sent: Monday, March 11, 2002 4:08 AM > To: [EMAIL PROTECTED] > Subject: RE: An CMR related exception > > > Alvin, > > Yes that's right. > Why? Well that's how the specs are defined and how the App Server > container have to do things. Check the EJB 2.0 specs for more details: > 10.3.8 Collections managed by the Container, 10.3.6 Semantics of > assignment for relationships > > Daniel > > -----Original Message----- > From: Alvin Wang [mailto:[EMAIL PROTECTED]] > Sent: samedi 9 mars 2002 2:23 > To: Daniel De Luca; [EMAIL PROTECTED] > Subject: RE: An CMR related exception > > > Deniel, thanks for replying. Do you mean > "entityBRef.getREntityA().add(entityARef)" will initialize that field? > Why? Or when and how that field will be initialized in later time? > Please give more details! > > Thanks! > > > -----Original Message----- > From: A mailing list for Enterprise JavaBeans development > [mailto:[EMAIL PROTECTED]]On Behalf Of Daniel De Luca > Sent: Friday, March 08, 2002 11:58 AM > To: [EMAIL PROTECTED] > Subject: Re: An CMR related exception > > > Alvin, > > You can't apply a Set method on a cmp-field that is used in a > relationship between 2 entities. You don't necessarily need to > initialize that field in the ejbCreate method. What you have to do is > create the Entity bean and initialize only the PK. > > Then suppose EntityA has a relation with EntityB and the CMR fields are > rEntityB (in EntityA) and rEntityA (in EntityB) In order to "set" the > relationship between the 2 you need to use the > entityBRef.getREntityA().add(entityARef). > After that your CMP-field will be up to date. > > > I would suggest you to read the EJB 2.0 Spec, everything is explained > there. > > Hope this help. > > Daniel > > -----Original Message----- > From: A mailing list for Enterprise JavaBeans development > [mailto:[EMAIL PROTECTED]] On Behalf Of Alvin Wang > Sent: vendredi 8 mars 2002 16:12 > To: [EMAIL PROTECTED] > Subject: An CMR related exception > > > Hi! I am using Weblogic 6.1 SP2 and EJB 2.0. I built a CMR between two > Entity Beans. However, I got the following exception while running: > > "javax.ejb.EJBException: When a cmp-field and a cmr-field (relationship) > are mapped to the same column, the setXXX method for the cmp-field may > not be called. The cmp-field is read-only." > > However, in the ejbCreate() method in CMP in EJB 2.0, I have to use > setXXX method to initialize the XXX cmp-field. Am I wrong? > > Can any guru help? thanks! > > ======================================================================== > === > 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". > > ======================================================================== > === > 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". > > =========================================================================== > 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". > > > -- _______________________________________________________________________________ Evan Ireland Sybase EAServer Engineering [EMAIL PROTECTED] Wellington, New Zealand +64 4 934-5856 =========================================================================== 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".