Ok, this is my Role.hbm.xml
Code:
<class name="org.ab.lvel.model.Role"
table="ROLE"
schema="lvel">
<id name="rid" unsaved-value="none">
<generator class="increment" />
</id>
<property name="role" column="ROLE" />
</class>
And this is, my User.hbm.xml
Code:
<class name="org.ab.lvel.model.User"
table="USERS"
schema="lvel">
<id name="id" column="ID">
<generator class="increment" />
</id>
<property name="username" column="USERNAME" />
<set name="roles" table="USER_ROLE"
cascade="save-update"
inverse="true">
<key column="ID" />
<many-to-many class="org.ab.lvel.model.Role" column="RID" />
</set>
</class>
Here is the SQL code generated by Hibernates
Hibernate: select role0_.rid as rid0_0_, role0_.ROLE as ROLE0_0_ from lvel.ROLE role0_ where role0_.rid=?
Hibernate: select max(ID) from USERS
Hibernate: select max(rid) from ROLE
Hibernate: insert into lvel.USERS (USERNAME, ID) values (?, ?)
Hibernate: insert into lvel.ROLE (ROLE, rid) values (?, ?)
The constraint violation comes from the fact that hibernates try to insert the Role object, but it's already in the ROLE table, causing a primary key violation.
Am I doing something wrong ? Thanks