Discussion:
[Middlegen-user] CMR in ejbPostCreate and NOT NULL foreign keys
Russell Black
2002-11-15 15:15:05 UTC
Permalink
I know the spec says that CMR fields should be set in the post-create method, but it causes a problem when your table has a NOT NULL constraint on the foreign key (CMR) column. Incidentally the airlines example suffers from this problem. I believe the issue is this: consider the following table:

CREATE TABLE "reservations"(
"reservation_id" INT NOT NULL,
"person_id_fk" INT NOT NULL,
"flight_id_fk" INT NOT NULL,
"comment" VARCHAR,
PRIMARY KEY ("reservation_id","person_id_fk","flight_id_fk"),
FOREIGN KEY ("person_id_fk") REFERENCES "persons"("person_id"),
FOREIGN KEY ("flight_id_fk") REFERENCES "flights"("flight_id")
);

ejbCreate is called, and it sets the CMP fields ONLY, and it tries to do the following SQL (copied from my JBOSS console)

INSERT INTO reservation (reservation_id, person_id_fk, flight_id_fk, comment, flight_id_fk, person_id_fk) VALUES (5, NULL, NULL, 'A comment', NULL, NULL)

Which fails because the foreign keys can't be null. The foreign keys aren't going to be set until the ejbPostCreate which happens AFTER the INSERT, so it never gets that far because the INSERT fails.

Does this imply that foreign keys can't have NOT NULL values when using CMR? This seems bad, because the NOT NULL constraint is there for a reason.
Eivind Waaler
2002-11-16 13:22:03 UTC
Permalink
Hmm, according to the SQL you're printing out there the two foreign keys
are being set twice in the same statement. Are you using the
fkcmp="false" attribute in your cmp20 plugin? Read the documentation about
this attribute here:

http://boss.bekk.no/boss/middlegen/plugins/cmp20.html#fkcmp

I guess that wouldn't help about the keys being set to null, weird we
haven't seen this error before though. I'll look into it on my example
running on weblogic..

Regards
.eivind
Post by Russell Black
CREATE TABLE "reservations"(
"reservation_id" INT NOT NULL,
"person_id_fk" INT NOT NULL,
"flight_id_fk" INT NOT NULL,
"comment" VARCHAR,
PRIMARY KEY ("reservation_id","person_id_fk","flight_id_fk"),
FOREIGN KEY ("person_id_fk") REFERENCES "persons"("person_id"),
FOREIGN KEY ("flight_id_fk") REFERENCES "flights"("flight_id")
);
ejbCreate is called, and it sets the CMP fields ONLY, and it tries to do the following SQL (copied from my JBOSS console)
INSERT INTO reservation (reservation_id, person_id_fk, flight_id_fk, comment, flight_id_fk, person_id_fk) VALUES (5, NULL, NULL, 'A comment', NULL, NULL)
Which fails because the foreign keys can't be null. The foreign keys aren't going to be set until the ejbPostCreate which happens AFTER the INSERT, so it never gets that far because the INSERT fails.
Does this imply that foreign keys can't have NOT NULL values when using CMR? This seems bad, because the NOT NULL constraint is there for a reason.
Russell Black
2002-11-16 17:04:03 UTC
Permalink
No, I left fkcmp="true". Without that the struts won't compile. Anyway as
you said, that wouldn't help about the keys being set to null.

It seems like this would be a problem intrinsic to all J2EE implementations,
since:

a) The J2EE container doesn't know the FK information until ejbPostCreate is
called, and
b) the INSERT statement happens before ejbPostCreate, therefore it *has* to
insert NULL for the foreign keys, violating the NOT NULL constraint so
common on foreign keys.

Since Sun obviously thought this issue out, there is probably an error in my
logic above. Can anyone see it? Is anyone else having this problem?

Thanks,

Russell

----- Original Message -----
From: "Eivind Waaler" <***@tihlde.org>
To: "Russell Black" <***@iarchives.com>
Cc: <middlegen-***@lists.sourceforge.net>
Sent: Saturday, November 16, 2002 8:21 AM
Subject: Re: [Middlegen-user] CMR in ejbPostCreate and NOT NULL foreign keys
Post by Eivind Waaler
Hmm, according to the SQL you're printing out there the two foreign keys
are being set twice in the same statement. Are you using the
fkcmp="false" attribute in your cmp20 plugin? Read the documentation about
http://boss.bekk.no/boss/middlegen/plugins/cmp20.html#fkcmp
I guess that wouldn't help about the keys being set to null, weird we
haven't seen this error before though. I'll look into it on my example
running on weblogic..
Regards
.eivind
Post by Russell Black
I know the spec says that CMR fields should be set in the post-create
method, but it causes a problem when your table has a NOT NULL constraint on
the foreign key (CMR) column. Incidentally the airlines example suffers
from this problem. I believe the issue is this: consider the following
Post by Eivind Waaler
Post by Russell Black
CREATE TABLE "reservations"(
"reservation_id" INT NOT NULL,
"person_id_fk" INT NOT NULL,
"flight_id_fk" INT NOT NULL,
"comment" VARCHAR,
PRIMARY KEY ("reservation_id","person_id_fk","flight_id_fk"),
FOREIGN KEY ("person_id_fk") REFERENCES "persons"("person_id"),
FOREIGN KEY ("flight_id_fk") REFERENCES "flights"("flight_id")
);
ejbCreate is called, and it sets the CMP fields ONLY, and it tries to do
the following SQL (copied from my JBOSS console)
Post by Eivind Waaler
Post by Russell Black
INSERT INTO reservation (reservation_id, person_id_fk, flight_id_fk,
comment, flight_id_fk, person_id_fk) VALUES (5, NULL, NULL, 'A comment',
NULL, NULL)
Post by Eivind Waaler
Post by Russell Black
Which fails because the foreign keys can't be null. The foreign keys
aren't going to be set until the ejbPostCreate which happens AFTER the
INSERT, so it never gets that far because the INSERT fails.
Post by Eivind Waaler
Post by Russell Black
Does this imply that foreign keys can't have NOT NULL values when using
CMR? This seems bad, because the NOT NULL constraint is there for a reason.
Mustafa Radi
2002-11-17 05:45:02 UTC
Permalink
yes - I think you are right, I had the same problems and had to remove the
NOT NULL statements in the database with jboss. Since jboss doesn't support
the fkcmp="true" there is even no way to set this value at least to the
right
value when creating an EntityBean. The question is - where is the bug
!!! Currently
I don't believe its a middlegen problem ...

cheers
Post by Russell Black
No, I left fkcmp="true". Without that the struts won't compile. Anyway as
you said, that wouldn't help about the keys being set to null.
It seems like this would be a problem intrinsic to all J2EE implementations,
a) The J2EE container doesn't know the FK information until ejbPostCreate is
called, and
b) the INSERT statement happens before ejbPostCreate, therefore it *has* to
insert NULL for the foreign keys, violating the NOT NULL constraint so
common on foreign keys.
Since Sun obviously thought this issue out, there is probably an error in my
logic above. Can anyone see it? Is anyone else having this problem?
Thanks,
Russell
----- Original Message -----
Sent: Saturday, November 16, 2002 8:21 AM
Subject: Re: [Middlegen-user] CMR in ejbPostCreate and NOT NULL foreign keys
Post by Eivind Waaler
Hmm, according to the SQL you're printing out there the two foreign keys
are being set twice in the same statement. Are you using the
fkcmp="false" attribute in your cmp20 plugin? Read the documentation about
http://boss.bekk.no/boss/middlegen/plugins/cmp20.html#fkcmp
I guess that wouldn't help about the keys being set to null, weird we
haven't seen this error before though. I'll look into it on my example
running on weblogic..
Regards
.eivind
Post by Russell Black
I know the spec says that CMR fields should be set in the post-create
method, but it causes a problem when your table has a NOT NULL constraint on
the foreign key (CMR) column. Incidentally the airlines example suffers
from this problem. I believe the issue is this: consider the following
Post by Eivind Waaler
Post by Russell Black
CREATE TABLE "reservations"(
"reservation_id" INT NOT NULL,
"person_id_fk" INT NOT NULL,
"flight_id_fk" INT NOT NULL,
"comment" VARCHAR,
PRIMARY KEY ("reservation_id","person_id_fk","flight_id_fk"),
FOREIGN KEY ("person_id_fk") REFERENCES "persons"("person_id"),
FOREIGN KEY ("flight_id_fk") REFERENCES "flights"("flight_id")
);
ejbCreate is called, and it sets the CMP fields ONLY, and it tries to do
the following SQL (copied from my JBOSS console)
Post by Eivind Waaler
Post by Russell Black
INSERT INTO reservation (reservation_id, person_id_fk, flight_id_fk,
comment, flight_id_fk, person_id_fk) VALUES (5, NULL, NULL, 'A comment',
NULL, NULL)
Post by Eivind Waaler
Post by Russell Black
Which fails because the foreign keys can't be null. The foreign keys
aren't going to be set until the ejbPostCreate which happens AFTER the
INSERT, so it never gets that far because the INSERT fails.
Post by Eivind Waaler
Post by Russell Black
Does this imply that foreign keys can't have NOT NULL values when using
CMR? This seems bad, because the NOT NULL constraint is there for a reason.
-------------------------------------------------------
This sf.net email is sponsored by: To learn the basics of securing
your web site with SSL, click here to get a FREE TRIAL of a Thawte
Server Certificate: http://www.gothawte.com/rd524.html
_______________________________________________
middlegen-user mailing list
https://lists.sourceforge.net/lists/listinfo/middlegen-user
Gavin Hughes
2002-11-17 21:04:02 UTC
Permalink
This is a JBoss problem, so I don't think there is anything you can do
about it in Middlegen.

JBoss does an INSERT after ejbCreate - as you are supposed to set CMR's
in ejbPostCreate
this results in an error as the FK field(s) are null at this point.

You could try to work around this by declaring the foreign key(s) as CMP
fields as well as CMR
fields then setting the fk in ejbCreate - except that JBoss doesn't
allow fields to be defined as
CMP and CMR.

There has been plenty of discussion on the topic on the JBoss mailing
lists - at this point
the usual recommendation is "don't define your foreign keys as NOT
NULL".

You can still keep your foreign key definitions - just define the
constraints as deferrable, so
they aren't checked until the "commit" (instead of happening after the
insert in ejbCreate).

For example (this works on Oracle) :

ALTER TABLE LOCATION ADD CONSTRAINT FK_LOCATION_COUNTRY_ID
FOREIGN KEY (country_id)
REFERENCES COUNTRY (id)
INITIALLY DEFERRED DEFERRABLE;


The following post suggests there is a patch for JBoss that fixes the
problem - but
according to the details for this patch on sourceforge it doesn't
actually work :-(

http://www.mail-archive.com/jboss-***@lists.sourceforge.net/msg23045.ht
ml

https://sourceforge.net/tracker/?func=detail&atid=376687&aid=630707&grou
p_id=22866

Cheers,
Gavin.

-----Original Message-----
From: Mustafa Radi [mailto:***@kabsi.at]
Sent: Sunday, 17 November 2002 6:48 PM
To: Russell Black
Cc: Eivind Waaler; middlegen-***@lists.sourceforge.net
Subject: Re: [Middlegen-user] CMR in ejbPostCreate and NOT NULL
foreign keys


yes - I think you are right, I had the same problems and had to
remove the
NOT NULL statements in the database with jboss. Since jboss
doesn't support
the fkcmp="true" there is even no way to set this value at least
to the right
value when creating an EntityBean. The question is - where is
the bug !!! Currently
I don't believe its a middlegen problem ...

cheers


Russell Black wrote:


No, I left fkcmp="true". Without that the struts won't
compile. Anyway as
you said, that wouldn't help about the keys being set to
null.

It seems like this would be a problem intrinsic to all
J2EE implementations,
since:

a) The J2EE container doesn't know the FK information
until ejbPostCreate is
called, and
b) the INSERT statement happens before ejbPostCreate,
therefore it *has* to
insert NULL for the foreign keys, violating the NOT NULL
constraint so
common on foreign keys.

Since Sun obviously thought this issue out, there is
probably an error in my
logic above. Can anyone see it? Is anyone else having
this problem?

Thanks,

Russell

----- Original Message -----
From: "Eivind Waaler" <***@tihlde.org>
<mailto:***@tihlde.org>
To: "Russell Black" <russell.black
@iarchives.com> <mailto:***@iarchives.com>
Cc: <middlegen-***@lists.sourceforge.net>
<mailto:middlegen-***@lists.sourceforge.net>
Sent: Saturday, November 16, 2002 8:21 AM
Subject: Re: [Middlegen-user] CMR in ejbPostCreate and
NOT NULL foreign keys



Hmm, according to the SQL you're printing out
there the two foreign keys
are being set twice in the same statement. Are
you using the
fkcmp="false" attribute in your cmp20 plugin?
Read the documentation about
this attribute here:


http://boss.bekk.no/boss/middlegen/plugins/cmp20.html#fkcmp

I guess that wouldn't help about the keys being
set to null, weird we
haven't seen this error before though. I'll look
into it on my example
running on weblogic..

Regards
.eivind

On Fri, 15 Nov 2002, Russell Black wrote:


I know the spec says that CMR fields
should be set in the post-create

method, but it causes a problem when your table has a
NOT NULL constraint on
the foreign key (CMR) column. Incidentally the airlines
example suffers
from this problem. I believe the issue is this:
consider the following
table:

CREATE TABLE "reservations"(
"reservation_id" INT NOT NULL,
"person_id_fk" INT NOT NULL,
"flight_id_fk" INT NOT NULL,
"comment" VARCHAR,
PRIMARY KEY
("reservation_id","person_id_fk","flight_id_fk"),
FOREIGN KEY ("person_id_fk") REFERENCES
"persons"("person_id"),
FOREIGN KEY ("flight_id_fk") REFERENCES
"flights"("flight_id")
);

ejbCreate is called, and it sets the CMP
fields ONLY, and it tries to do

the following SQL (copied from my JBOSS console)

INSERT INTO reservation (reservation_id,
person_id_fk, flight_id_fk,

comment, flight_id_fk, person_id_fk) VALUES (5, NULL,
NULL, 'A comment',
NULL, NULL)

Which fails because the foreign keys
can't be null. The foreign keys

aren't going to be set until the ejbPostCreate which
happens AFTER the
INSERT, so it never gets that far because the INSERT
fails.

Does this imply that foreign keys can't
have NOT NULL values when using

CMR? This seems bad, because the NOT NULL constraint is
there for a reason.






-------------------------------------------------------
This sf.net email is sponsored by: To learn the basics
of securing
your web site with SSL, click here to get a FREE TRIAL
of a Thawte
Server Certificate: http://www.gothawte.com/rd524.html
_______________________________________________
middlegen-user mailing list
middlegen-***@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/middlegen-user
Matthias Flor
2002-11-26 08:27:02 UTC
Permalink
Post by Gavin Hughes
This is a JBoss problem, so I don't think there is anything you can do
about it in Middlegen.
You may find this links useful:

(Insert on PostCreate)
http://www.jboss.org/forum/thread.jsp?forum=46&thread=20232&message=3737561

(set not null foreign key in ejbCreate)
http://www.jboss.org/forums/thread.jsp?forum=46&thread=19006&message=3733285

As Dain stated there it's on the todo-list. It's planned for JBoss 4.0
he said.

cheers,
Matthias
--
Matthias Flor <***@isb-ag.de>
------------------------------------------------------------------
ISB AG Fon: +49 (0)721/82800-0
Karlstrasse 52-54 Fax: +49 (0)721/82800-82
D-76133 Karlsruhe Web: http://www.isb-ag.de
Loading...