Hi Jim,

this is a serious problem I had, too.

I know of two ways to solve this problem. The first solution is to
implement this bean completely bean-managed.

The second version is to implement this bean container-managed but
without any create methods (but with ALL persistent fields including the
PK). Then you'll have to implement another bean with all the persistent
fields of the original bean including the primaryKey fields. This second
bean is bean-managed and implements only the create methods (where it
doesn't write the PK fields) and a method to retrieve the primarykey
fields ( say getID() ) In the create-method you'll look up the created
primary key which can later on be retrieved by the getID() method.
Now,... to read, update an delete you only need the CMP bean. But to
create a Bean from a Session Bean, you'll first create this second bean,
call getID() , let loose of this bean and then findByPK an instance of
the original bean. Of course, now you need two database operations. But
leaving the bean mainly CMP is probably worth it since creating a bean
is not too often.

I hope this helps. I'd appreciate any comments on this solution.

dirk

Jim Archer wrote:
> 
> Hi All...
> 
> I am working on an EJB project. Currently, I am planning to use the
> JOnAS EJB server with PostgreSQL, but I don't think that is important.
> 
> When I designed my data tables, I had planned to use a sequence field for
> the primary key of each table. A sequence is called a few different things,
> depending upon the database in use. For example, MS SQL Server calls it an
> "identity field." Its just a field that is automatically assigned a unique
> value each time a record is inserted, so that each record has a guaranteed
> unique value in this field. A SQL app would insert a record and then do a
> select to read back the value assigned to that records sequence field.
> 
> Anyhow, I discovered that in my CMP entity beans ejbCreate method I must
> populate all the fields which are part of the primary key. When this method
> returns, the container creates an instance of the primary key object and
> then adds a record to the database. Thats all fine, but if I use a sequence
> field as the primary key it has no value until the record is created, at
> which point its too late for me to populate the primary key attribute in my
> class, since the ejbCreate method has already returned at this point.
> Therefor, it seems that this approach makes instansiating a primary key
> object problematic if it maps to a sequence field.
> 
> I had though about using the ejbPostCreate method to lookup the assigned
> value for the sequence field and produce a new primary key object, but I
> think its likely that the primary key object plays a part in the assignment
> of context to the entity bean after ejbCreate returns and before
> ejbPostCreate is called, so this might screw up that activity on some EJB
> implementations.
> 
> So, my question is, is it possible using only features outlined in the EJB
> 1.1 spec to use a database sequence fields as a primary key?
> 
> I hope I made this clear. Its kind of late (early) here...
> 
> Thanks to all!
> 
> Jim
> 
> ----
> To unsubscribe, send email to [EMAIL PROTECTED] and
> include in the body of the message "unsubscribe jonas-users".
> For general help, send email to [EMAIL PROTECTED] and
> include in the body of the message "help".

--
Dirk Haase                            Schlund + Partner AG
[EMAIL PROTECTED]                      http://www.schlund.de
----
To unsubscribe, send email to [EMAIL PROTECTED] and
include in the body of the message "unsubscribe jonas-users".
For general help, send email to [EMAIL PROTECTED] and
include in the body of the message "help".

Reply via email to