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.
 

Reply via email to