This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Entity being merged doesn't return full instance  RSS feed

 
Nicky Formosa
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Quite new to the whole EJB3 tech so please bare with me....

I've created a generic method which persists/updates an object in the database using the Entity Manager method merge. The merge method should return the full new instance of the object that has been persisted but instead its only returning the same object i tried to persist. ie: the Id and Timestamp (for example) are not being returned.

Is there some setting i'm missing or?

Thanks
Nick
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The API doc is extremely spartan, stating only:
Merge the state of the given entity into the current persistence context.


But you should not read into this that the the two copies are merged together!

The current state of the object - i.e. the version you are passing to the entity manager - is being taken as the current state of that object, and it is "merged" into the managed persistent context.

The identifier(s) is used to find the version of this object in the managed pool and the object is in effect replaced with the new one.

It should return the newly managed instance, which could literally be a new instance, could be the old instance changed to look like yours, or it could be your copy now being managed by the entity manager - that's up to the implementation of the container (and obviously some of those options are fraught with danger).

When you pass an object to merge() it needs to have a valid @Id field, and have been persist()-ed previously.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!