Hi everyone,
I have a relation between "
User" and "
Role"s. But this association will not be mapped to the "
User" or the "
Role" object. She will be in a "
Affectation" object.
My tables (PostgreSQL) :
Code:
CREATE TABLE user (
uid SERIAL NOT NULL
);
CREATE TABLE role (
rid SERIAL NOT NULL
);
CREATE TABLE user_role (
uid INTEGER NOT NULL,
rid INTEGER NOT NULL,
-- FOREIGN KEYS ...
);
My "
Affectation" object that is used to store teh relation between one user and his roles.
Code:
public class Affectation {
private User user;
private List<Role> roles;
// get and set..
}
And finally, my mapping :
Code:
<class name="xx.Affectation" table="user_role">
<id column="uid" name="id">
<generator class="foreign">
<param name="property">user</param>
</generator>
</id>
<one-to-one name="user" constrained="true" />
<bag name="roles" table="user_role" lazy="false">
<key column="uid"/>
<many-to-many class="xx.Role" column="rid" unique="false"/>
</bag>
</class>
But when y try toplay with this mapping, I can't insert (and thus cannot continue) because Hibernate run this query
Code:
insert into user_role (uid) values ('2')
And I have a NOT NULL constraint on the
"user_role"."rid" column. I have tried with "cascade=all" or "cascade=save-update" and also "inverse=true" but generate insert is the same..
Can someone help me ?
Thanks
EDIT :If I remove the NOT NULL constraint in the column "rid" Hibernate play these two queries :
Code:
Hibernate: insert into user_role (uid) values (?)
Hibernate: insert into user_role(uid, rid) values (?, ?)
And my table contains two rows :
uid=2; rid=1
uid=2; rid=null
...