a different object with the same identifier value was already associated with the session

I have found two solution to this issue. Here it is, there are two.

1. Use cascase = none.

2. Use merge instead of saveandupdate.

 

The problem here is that you pass in a detached object which gets reattached to the Session. You also fetch the same object with the find. This causes the exception.

  • You could change the query to do all the validation you require and therefore don't return it into the Session.
  • Otherwise you'd have to evict the entity returned from the query.

你可能感兴趣的:(hibernate,java)