I have two tables (Team and Employee) and whenever I add an association for the two via a linking table (EmployeeTeamLink), I see that an unnecessary update is happening to the Team table. How can I turn off this update as none of the columns in the Team table are being modified except for the reference it has to the employee collection?
Code used to create the Employee and update the Team collection:
Code:
// team has been created in another session and already exists in the db
Employee employee = new Employee();
// all necessary Employee data filled in
try
{
Session session = getSession();
Transaction tx = session.beginTransaction();
team.getEmployees().add(employee);
session.saveOrUpdate(team);
tx.commit();
session.flush();
}
catch (Exception ex)
{
ex.printStackTrace();
}
On tx.commit, what I see is not only an insert (which I'm expecting) but an update to the Team table which I was not:
Code:
Hibernate:
insert
into
Employee
(Name)
values
(?)
Hibernate:
update
Team
set
Name=?, Location = ?,
where
ID=?
Hibernate:
insert
into
EmployeeTeamLink
(TeamID, EmployeeID)
values
(?, ?)
Team mapping:
Code:
<class name="Team" table="Team" dynamic-update="true">
<id name="id" type="int">
<column name="ID" />
<generator class="identity"/>
</id>
<property name="name" type="string">
<column name="Name" length="128" not-null="true">
<comment></comment>
</column>
</property>
<property name="location" type="string">
<column name="Location" length="128" not-null="true">
<comment></comment>
</column>
</property>
<set name="employees" cascade="save-update" table="EmployeeTeamLink">
<key>
<column name="TeamID" not-null="true">
<comment></comment>
</column>
</key>
<many-to-many entity-name="Employee">
<column name="EmployeeID" not-null="true">
<comment></comment>
</column>
</many-to-many>
</set>
</class>
Employee mapping:
Code:
<class name="Employee" table="Employee" dynamic-update="true">
<id name="id" type="int">
<column name="ID" />
<generator class="identity" />
</id>
<property name="name" type="string">
<column name="Name" not-null="true">
<comment></comment>
</column>
</property>
<set name="teams" inverse="true" cascade="save-update" table="EmployeeTeamLink">
<key>
<column name="EmployeeID" not-null="true">
<comment></comment>
</column>
</key>
<many-to-many entity-name="Team">
<column name="TeamID" not-null="true">
<comment></comment>
</column>
</many-to-many>
</set>
</class>
Thanks,
Justin