-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Insertion into Link table and unexpected table updates
PostPosted: Fri May 16, 2008 3:20 pm 
Newbie

Joined: Tue Apr 25, 2006 1:17 pm
Posts: 19
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.