Search code examples
mysqlspringhibernatemany-to-many

Spring/hibernate/MySQL doesnt save in auxiliar table states


I have 3 tables Users, Archivements and ArchivemetnsUSers. This is a relathionship Many-To-Many.

My problem is that i want save a new user in my DataBase but, i dont want save/update the archivement assigned to user, because i have only N pre-existent archivements.

Then, when i go to save hibernate session for my user... i get a exception: the selected archivement exists in DataBase, and hibernate cant save duplicate PrimaryKey.

How i avoid this problem?

Any annotation? or trick?


Solution

  • finally, the problem was that i need get archivements, set archivements to user and save user instance in ONE TRANSACTION, for hibernate understand that these archivements registers exist in DataBase.