I have a RequestEntity with these fields:
Code:
@NotNull
@ManyToOne(fetch=FetchType.EAGER, cascade={CascadeType.MERGE, CascadeType.PERSIST})
@JoinColumn(name="request_type", referencedColumnName="id")
private RequestTypeEntity requestType;
@ManyToOne(fetch=FetchType.EAGER, cascade={CascadeType.MERGE, CascadeType.PERSIST})
@JoinColumn(name="item_source_type", referencedColumnName="item_source_type_name")
private ItemSourceTypeEntity itemSourceType;
Each of the relationships is presented to the user as a selectOneMenu in an xhtml page with lists of values from the DB. Pretty standard scenario.
When I persist the request, the requestType is set and saved properly. When the request is redisplayed, the correct requestType is preselected and displayed to the user.
The itemSourceType and itemSource, however, don't work. When I call (in my RequestDaoImpl class):
Code:
getEntityManager().persist(request);
I get the hibernate exception:
Code:
Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: org.aero.libreq.entity.ItemSourceEntity
Prior to the call to persist, I do the following:
Code:
...
request.setRequestType(getEntityManager().merge(request.getRequestType()));if (request.getItemSourceType() != null)
request.setItemSourceType(getEntityManager().merge(request.getItemSourceType()));
if (request.getItemSource() != null)
request.setItemSource(getEntityManager().merge(request.getItemSource()));
...
I see no difference between the itemSourceType and requestType setup, so I am confused as to why I am getting this error? When the request is saved, the itemSourceType is not persisted.
The differences between the relationships in the logic are that:
1) The requestType is required and has a default value when the request is first created.
2) the itemSourceType is not required and cannot be populated when the request is first created. It is added to the request in a subsequent update.
How can I properly set up these entities so that the updates to them are properly saved and refreshed for redisplay to the user?
Thank you for any help!!
Oh, and I have stepped through in the debugger and can see that request.itemSourceType is there with all the right values, as is requestType. The exception occurs right when persist is called.
Ginni