The identity of an entity is established by the container when the call to
ejbCreate is over. This includes associating the primary key value with the
entity.
Now suppose that setting cmr-value in the ejbCreate method is allowed. The
container then, cannot enforce setting the primary key cmp-field value
before setting a cmr-field. With the presence of database constraints like
primary key-foreign key, while creating/updating the record for the child
table, primary key value of the parent table is required.
For example, consider one-to-one relationship between CustomerBean and
AddressBean and the corresponding table.
CustomerTable
----------------
customerId (primary key)
name
AddressTable
-----------------
addressId
zip
customerId (foreign key)
Without the restriction of setting cmr-field in the ejbPostCreate, ejbCreate
of CustomerBean can be written like:
CustomerBean:
ejbCreate(...){
AddressLocal address = createAddress(...);
setAddress(address);
//Setting pk after setting cmr-field
setCustomerId("cust-101");
...
}
In this case, a container is forced to defer setting the value of
AddressTable.customerId in the database after the record is created in the
CustomerTable. Moreover, the problem is compouneded by the fact that
container cannot differentiate the ejbCreate written in this manner with the
following ejbCreate.
CustomerBean:
ejbCreate(...){
//Setting pk before setting any cmr-field
setCustomerId("cust-101");
AddressLocal address = createAddress(...);
setAddress(address);
...
}
In either way, container has to do deferred insertion/update.
However, when the relationships are set in the ejbPostCreate, container may
or may not defer the inserts based upon the strategy it uses.
<vendor>
Pramati Application Server identifies the type of constraint existing
between the two related tables, so that the database insertion and cmr-field
value can be set while honouring the database constarints specified by the
user.
</vendor>
Regards,
Hemant
Server Team,
www.pramati.com
----- Original Message -----
From: "Evan Ireland" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, January 31, 2002 6:14 AM
Subject: Re: use of ejbPostCreate()
Chandra kumar wrote:
> ...
>
> Back to my question again, Let's say we set the relationship value in
ejbPostCreate() as specified in the spec. What if the
relationship(forignkey) field is set as not null in child table and DB
insert happening after ejbCreate(), insert will fail. I think I'm missing
something here, because I still don't seem to understand significance of the
statement in the spec that says "do not set the cmr-fields in ejbCreate()"
Two issues here:
(1) The spec is taking a 'logical' view that before ejbCreate returns, the
entity does not exist so you cannot enter it into relationships at that
time.
(2) Having foreign key references that are defined as "not null" will
generally be prone to causing constraint violations in some
situations. Whether inserts are deferred (or for that matter, ordered)
in such a way as to prevent foreign key constraint violations is not
covered by the spec, but it is a quality of implementation issue.
The spec does not guarantee that any EJB 2.0 CMP implementation will
be able to work with arbitrary database-level constraints that you
might have defined. The spec doesn't even require support for foreign
keys at all!
> I humbly seek help on this to understand better.
>
> Thanks,
> Chandra
>
> On Thu, 31 Jan 2002 10:09:51 +1300, Evan Ireland <[EMAIL PROTECTED]>
wrote:
>
>
>>Chandra kumar wrote:
>>
>>
>>>Section 10.5.2 of EJB 2.0 spec states the following:-
>>>
>>>"....... The entity Bean Provider must not attempt to modify the values
of cmr-fields in an ejbCreate<METHOD(...) method; this should be done in the
ejbPostCreate<METHOD(...) method instead. ......"
>>>
>>>I've tried my best to understand the above. But I could not succeed.
>>>Here are my questions (note: when we have strict DB constraints in
place):
>>>
>>>1. How the above statement holds good, if we want set the relationship
value in child's ejbPostCreate(), If DB insert is issued after ejbCreate().
>>>
>>
>>That's up to the CMP implementation. Sybase EAServer (by default) will do
>>an 'insert' after ejbCreate and will do an 'update' after ejbPostCreate
>>(of course if no fields have changed, the 'update' can be skipped).
>>
>>
>>
>>>2. Let's say if DB insert is deferred to end of ejbPostCreate(), then we
cannot create child object(s) in ejbPostCreate() of Parent.
>>>
>>
>>That depends on whether your database schema has foreign key constraints
that
>>will break if the insert is deferred.
>>
>>
>>
>>>If this is something very obvious, it'd probably irritate some to see a
long mail with examples. So I defer that to you.
>>>
>>>Any help would be greatly appreciated.
>>>
>>>Thanks for time and consideration,
>>>Chandra
>>>
>>>=========================================================================
==
>>>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".
>>
>>
>
>
--
____________________________________________________________________________
___
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".
===========================================================================
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".