Alex,
Let me repeat what I think happens during entity bean creation:
In ejbCreate we set all primary key fields.
In ejbPostCreate we can set up any CMR fields.
Any CMR fields set in ejbCreate are ignored, and must be set again in 
ejbPostCreate.

Now if we have a table, T1,  with PK = {pkCol1, pkCol2}, and pkCol1 acts 
as a foreign key to another table, T2, then our entity bean will have a 
problem, because the field mapped to pkCol1 will also be marked as a CMR 
field, and so it will be blanked out at the end of ejbCreate (actually 
throws the IllegalStateException seen below).

So, can we instead only nullify CMR fields that are not mapped to primary 
key fields. This should work fine if T1-T2 is many to one or one to one 
relationship. Many to many is not a problem in this case because we are 
using foreign key constraints and not a mapping table to implement the 
relationship.

So, in Frank's example, he sets up the two parts of the primary key in 
ejbCreate, and does nothing in ejbPostCreate. He can use his CMR getter no 
problem. Of course, the CMR setter would throw an exception as you can't 
change the primary key.

Have I missed anything?
Happy New Year,
Jonathan




Alex Loubyansky <[EMAIL PROTECTED]>
Sent by: [EMAIL PROTECTED]
31.12.2002 12:22
Please respond to jboss-user

 
        To:     [EMAIL PROTECTED]
        cc: 
        Subject:        [JBoss-user] Re: Using CMR with PK-Field as FK


Hello Frank,

see my answer below.

Tuesday, December 31, 2002, 2:06:52 PM, you wrote:

LF> I have two beans Feature and FeatureOption with CMR.
LF> JBoss-HEAD created the tables correct, using the new feature to have 
the 
LF> same field as FK to parent and part of PK:
LF>     create table feature ( feature_id smallint not null,  description 
LF> varchar(30),  primary key (feature_id)  constraint pk_feature );
LF>     create table feature_option ( feature_id smallint not null, 
LF>  option_id integer not null,  description varchar(30),  primary key 
LF> (feature_id,option_id) constraint pk_feature_option );
LF>     alter table feature_option add constraint (foreign key 
(feature_id) 
LF> references feature  constraint fk_featur_1isvfy8);

LF> But everytime I try to add FeatureOptions to Feature, I get an 
Exception:
LF>     java.lang.IllegalStateException: CMR field Feature_options is 
mapped 
LF> to a foreign key which is a part for a primary key,
LF>     and primary key may only be set once in ejbCreate [EJB 2.0 
Spec.10.3.5].

LF> My code looks like this:
LF> feature = featureHome.create( feature_id, description );
LF> coll = feature.getOptions();
LF> featureOption = featureOptionHome.create( feature_id, option_id, 
LF> description );
LF> coll.add( featureOption ); // this line throws the exception.

LF> So my Question is: How to add a FeatureOption to Feature ?

This is the expected behaviour.
In this case, the relationships are assigned between ejbCreate and 
ejbPostCreate.
Having the same values in pk field and the corresponding fk field
means having a relationship.
Any attempt to modify the relationship with CMR accessors or
with collection api will throw this exception.
The relationship is removed only with one of the participant removal.

Do you think it should work other way?

alex




-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
JBoss-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-user





-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
JBoss-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-user

Reply via email to