This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes Container trying to save CMR field Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Container trying to save CMR field " Watch "Container trying to save CMR field " New topic
Author

Container trying to save CMR field

Naveen Sampra
Ranch Hand

Joined: Jun 11, 2003
Posts: 70
Can someone help me out with this problem.

I am using CMP EJB2.0 with CMR.

When trying to save a child entity(which is part of a CMR relationship), should the container call the setParent(parentEntity) method also.

I get a database error which says, "trying to set a null value to a not null column".

How do I solve this problem. The setParentId() method is setting the correct parent Id. I don't want container to call the abstract setParent() method.
Karthik Guru
Ranch Hand

Joined: Mar 06, 2001
Posts: 1209
Originally posted by Naveen Sampra:
Can someone help me out with this problem.

I am using CMP EJB2.0 with CMR.

When trying to save a child entity(which is part of a CMR relationship), should the container call the setParent(parentEntity) method also.

I get a database error which says, "trying to set a null value to a not null column".

How do I solve this problem. The setParentId() method is setting the correct parent Id. I don't want container to call the abstract setParent() method.


When creating the child record , are you setting the parent as well in ejbPostCreate of the child entity?.
I guess you have to since parent id is a non nullable parent key on child entity. Make sure that the parent entity that you are setting is a valid reference.
IMHO i find it surprising that the container allows you to do setParentId() instead of setParent(parentEntity) when you have a cmr between child and parent on that column (parentId). The container is expected to take care of that part (setting parent id ) implicitly
Naveen Sampra
Ranch Hand

Joined: Jun 11, 2003
Posts: 70
Hi,

I am not doing anything in ejbPostCreate() of the child entity. It's just an empty method. I don't have any post processing to do.

All my set logic is in the ejbCreate() method. I am setting a correct value in setParentId().

When I remove the setParent() method, generated ejb code sets null to that field. When I add setParent() and set it with a valid reference to parent, I get IllegalStateException thrown.

Any ideas as to how to handle CMR field in ejbCreate() ?
[ April 27, 2005: Message edited by: Naveen Sampra ]
Naveen Sampra
Ranch Hand

Joined: Jun 11, 2003
Posts: 70
Should the set operation of CMR field be called in ejbCreate() ?
Karthik Guru
Ranch Hand

Joined: Mar 06, 2001
Posts: 1209
Try setting the parent entity reference (ie do a setParent) in ejbPostCreate and not ejbCreate. The ejbPostCreate signature is exactly same as the ejbCreate except that you set the CMRs in this method call.
Naveen Sampra
Ranch Hand

Joined: Jun 11, 2003
Posts: 70
Thanks. Your solution is correct.

I found the solution yesterday and was about to post the finding today, when I saw your post.

Actually, the problem is application server specific. I am using WAS 5.1 currently and remember I did not run into this problem while using JBOSS.
Karthik Guru
Ranch Hand

Joined: Mar 06, 2001
Posts: 1209
Originally posted by Naveen Sampra:
Actually, the problem is application server specific.


It is always advisable to set CMRs in ejbPostCreate irrespective of the app server you are using. Atleast the bean is portable that way.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Container trying to save CMR field