In my many-to-many association all data relating to the SystemUser is persisted correctly. But no record is added the intermediate table (NM_SYSTEM_USER_ROLE). I've scanned the log for INSERT statements and hibernate doesn't attempt to insert a record into the NM_SYSTEM_USER_ROLE table.
Interestingly when I retrieve the added SystemUser, the SELECT statement DOES contain the necessary join.
Regarding code - In my unit test is really simple, I open a Session and retrieve all of the roles, close the session, and add a couple of Role instances to a newly instantiated SystemUser, and set all of the other necessary beans properties. So the SystemUser id is null, but the Role.ids are set.
And then I open a new session, call session.saveOrUpdate(systemUser), followed by session.flush() and session.close().
I've been struggling with this for a couple of days now, so any help is much appreciated.
Here's the mapping for SystemUser.
<class
name="com.myemployer.myproject.domain.SystemUser"
table="NM_SYSTEM_USER"
dynamic-update="false"
dynamic-insert="false"
>
<id
name="id"
column="id"
type="java.lang.String"
>
<generator class="uuid.hex">
</generator>
</id>
<many-to-one
name="contactDetails"
class="com.myemployer.myproject.domain.ContactDetails"
cascade="save-update"
outer-join="auto"
update="true"
insert="true"
column="contact_details_fk"
not-null="true"
/>
<set
name="roles"
table="NM_SYSTEM_USER_ROLE"
lazy="false"
inverse="true"
cascade="all"
sort="unsorted"
>
<key
column="system_user_id"
/>
<many-to-many
class="com.myemployer.myproject.domain.Role"
column="role_id"
outer-join="auto"
/>
</set>
<many-to-one
name="login"
class="com.myemployer.myproject.domain.Login"
cascade="all"
outer-join="auto"
update="true"
insert="true"
column="login_fk"
not-null="true"
/>
<many-to-one
name="status"
class="com.myemployer.myproject.domain.Status"
cascade="none"
outer-join="auto"
update="true"
insert="true"
column="status_fk"
not-null="true"
/>
<property
name="lastModifier"
type="java.lang.String"
update="true"
insert="true"
column="lastModifier"
not-null="true"
/>
</class>
Here's the mapping for Role.
<class
name="com.myemployer.myproject.domain.Role"
table="NM_ROLE"
dynamic-update="false"
dynamic-insert="false"
>
<id
name="id"
column="id"
type="java.lang.String"
>
<generator class="uuid.hex">
</generator>
</id>
<property
name="name"
type="java.lang.String"
update="true"
insert="true"
column="name"
not-null="true"
/>
<property
name="lastModifier"
type="java.lang.String"
update="true"
insert="true"
column="lastModifier"
not-null="true"
/>
</class>
The SQL used to create the relevant tables:
create table NM_SYSTEM_USER (
id VARCHAR2(255) not null,
contact_details_fk VARCHAR2(255) not null,
login_fk VARCHAR2(255) not null,
status_fk VARCHAR2(255) not null,
lastModifier VARCHAR2(255) not null,
primary key (id)
);
create table NM_ROLE (
id VARCHAR2(255) not null,
name VARCHAR2(255) not null,
lastModifier VARCHAR2(255) not null,
primary key (id)
);
create table NM_SYSTEM_USER_ROLE (
system_user_id VARCHAR2(255) not null,
role_id VARCHAR2(255) not null,
primary key (system_user_id, role_id)
);