In reference to the MovieBean code with a CMR field in HFE on page 396, I wonder when the
"DirectorId" column, which is a foreign key, will get populated in the
"Movie" table. A reasonable guess seems to be that it gets popluated after the
setDirector method is called (inside
ejbPostCreate method in the sample code).
But it brings up two questions:
The first one is that if the above guess holds true, it may imply that it would take two separate steps for the container to create/populate a new record in the
"Movie" table: 1) insert a new record to the table while leaving the
"DirectorId" null -- this takes place after
ejbCreate and before
ejbPostCreate; 2) populate the
"DirectorId" column by updating the newly added record after
setDirector method is called. This insert/update sequence may
NOT work if there is a "not nullable" constraint on the foreign key, which can very well be the case in a well-designed database.
The bigger issue is that it also implies the container can
NOT manage relationships for newly created entities. In other words, the code needs to be explicitly written (just like the code inside "ejbPostCreate" on page 396) to establish the relationship. I really wonder why the
EJB spec doesn't allow the container to simply take the foreign key (presumably set inside "ejbCreate" along with other CMP fields) and find/set the CMR field automatically, which is exactly what the container does for the existing entities returned by the finder methods, i.e. figuring out the foreign key and setting the CMR field automatically. I cannot understand why the EJB designers didn't want the creator methods and finder methods to work in a more consistent manner in terms of how the container-managed relationship is handled.
Any insights are greatly appreciated.
[ March 03, 2004: Message edited by: Jiang Lin ]
[ March 03, 2004: Message edited by: Jiang Lin ]
[ March 03, 2004: Message edited by: Jiang Lin ]