Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

EJB & Primary Key

0 views
Skip to first unread message

msenin

unread,
Aug 15, 2002, 7:37:43 PM8/15/02
to
I am trying to use CMP2 under JBuilder 6 and Borland Enterprise
Server.
We are using Informix, and all primary keys in tables are setup as
serialnumber (analog of MS Access'es Autonumber).

If I create() single Entity bean, then update its properties, and when
SessionBean's method exits, transaction is commited. The record is
inserted into the table with all fields correctly populated.

But if I perform a loop like

for (int i=0; i<100; i++) {
MyEntity entity = MyEntityHome.create();
entity.setProperty1 ("a");
entity.setProperty2 ("b");
}

and 3 entities will be created as result, when transaction is
commited, I will get one record correctly populated with data of the
100th entity bean, and the rest 99 records will have null values in
all fields.

I understand, that in CMP2, Integer MyEntityBean.ejbCreate() must
return null.
Since I rely on container to manage primary keys and take care of
referential integrity, I do not set primary key in this method, and
neither do I set other properties on the entity bean. Now, if I call
create(), I will get exception because container will try to insert
into table (pk, field, field2) values (null,null,null).

So I tried to set all values to (new Integer(0), "", ""), but then I
get effect described above - 1st record is populated with data of last
record, and other N-1 records are nulls (except for primary key, of
course).

What's the proper way to
o initialize entity beans in ejbCreate() for CMP2 ?
o make container set primary key back to EJB after record had been
inserted and Autonumber has been allocated for that record ?

Marcus

unread,
Aug 19, 2002, 5:32:01 PM8/19/02
to
You need to do 2 things:
1. tell the container to ignore the primary key field on insert (
because the database is going to look after providing a value for this
field)
2. tell the container to execute some sql after it has created the
record to obtain the newly inserted key value.

1:
<table-properties>
<table-name>TstTbl</table-name>
<column-properties>
<column-name>id</column-name>
<property>
<prop-name>ignoreOnInsert</prop-name>
<prop-type>java.lang.Boolean</prop-type>
<prop-value>true</prop-value>
</property>
</column-properties>
...
2:
...
</property>
<property>
<prop-name>getPrimaryKeyAfterInsertSql</prop-name>
<prop-type>String</prop-type>
<prop-value>select DBInfo('sqlca.sqlerrd1') from
systables where tabname='TstTbl'</prop-value>
</property>
</table-properties>

P.S you can configure this from within the ide by setting the table &
column properties, thus avoiding having to modify the DD manually

Hope this helps.
-Marcus.

maxim...@bigfoot.com (msenin) wrote in message news:<d1004ecb.02081...@posting.google.com>...

0 new messages