Paul Sturrock wrote:
Ismael Upright wrote:
Paul Sturrock wrote:If your entity does not have an ID it is not an update. Rather than creating a new instance of Book, load the existing one and update it.
My entities have IDs - hey are marked with @Id annotation.
Where in this code do you set the ID? The instance of Book associated with the instance of Shop has no identifier. You create a new instance of Book, and call merge. Merge will create a new instance in the database if it is not identified by anything. So if you had used the state of shop as is and updated the existing book you'd get the behaviour you want. Otherwise how else will Hibernate know to update an existing book, and which book to update?