0

I am getting NonUniqueObjectException when i persist an object using JPA.

The object where i make persisent, have other objects and in some cases the objects have the same id.

Make a merge in object where is already saved in DB is a possibility but i only can make persist at one object (this object have all information)

I can put any annotation on my id to make a merge when this id is already saved in MySQL database?

Vítor Nóbrega
  • 1,219
  • 4
  • 26
  • 53

1 Answers1

1

Use entityManager.merge(..) or leave the id field empty. Otherwise hibernate will check if an entity exists in the database with the same id and will fail if it finds one.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • I had try to use entityManager.merge but i get an error because the hibernate dont have one of my object with a specified id. I dont know the reason for this because in that point i didnt save nothing in my DB and it's normal hibernate cannot find ir – Vítor Nóbrega Mar 15 '12 at 10:25
  • 1
    I change my cascade type to merge and know is working, thanks to help me solve my problem – Vítor Nóbrega Mar 15 '12 at 11:10