-->
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.  [ 4 posts ] 
Author Message
 Post subject: How to avoid needless sql updates for child collection
PostPosted: Fri Dec 01, 2006 3:56 pm 
Beginner
Beginner

Joined: Tue May 23, 2006 4:10 pm
Posts: 38
Location: Charleston, SC
I'm trying to determine the best way to avoid needless SQL updates being issued by Hibernate for collections when I update a detached parent object in a new session. I'm using Hiberate 3.1. Each time the parent object is updated, only one of the elements in the collection needs updating. The rest of the elements are unchanged. The parent/child association is not currently defined as bi-directional. That might be the easy answer to our problems, not sure, but I'd like to know if there are any other options besides making the association bi-directional. My mappings are below. The assoications involved are the crewMembers and passengers collections.

Grant


Code:
<hibernate-mapping package="gov.seahawk.model">
    <!-- Hibernate class mapping -->
    <union-subclass
            name="VesselArrival"
            entity-name="gov.seahawk.model.VesselArrivalNoticeEntity"
            extends="gov.seahawk.model.ArrivalNoticeEntity"
            table="VESSEL_NOA" dynamic-insert="true" dynamic-update="true" >

        <property name="facilityName" type="string" column="VESSEL_FACILITY_NAME" length="100" />
        <property name="berth" type="string" column="VESSEL_BERTH" length="100" />

        <many-to-one
                class="Vessel"
                name="vessel"
                column="VESSEL_ID"
                foreign-key="VESSEL_NOA_VESSEL_ID_FK"
                lazy="false"
                cascade="none"/>

        <many-to-one
                class="Facility"
                name="facility"
                column="FACILITY_ID"
                foreign-key="VESSEL_NOA_FACILITY_ID_FK"
                lazy="false"/>

        <many-to-one
                class="DataSource"
                name="dataSource"
                column="DATA_SOURCE_ID"
                foreign-key="VESSEL_NOA_DATA_SOURCE_ID_FK"
                lazy="false"/>

        <set name="cargo" table="VESSEL_NOA_CONVEYANCE" cascade="all">
            <key column="VESSEL_NOA_ID" foreign-key="VESSEL_NOA_CONVEYANCE_VESSEL_NOA_ID_FK"/>
            <many-to-many class="Conveyance" column="CONVEYANCE_ID" foreign-key="VESSEL_NOA_CONVEYANCE_CONVEYANCE_ID_FK"/>
        </set>

        <set name="passengers" cascade="all">
            <key column="VESSEL_NOA_ID" foreign-key="PASSENGER_VESSEL_NOA_ID_FK"/>
            <one-to-many class="Passenger"/>
        </set>

        <set name="crewMembers" cascade="all">
            <key column="VESSEL_NOA_ID" foreign-key="CREW_MEMBER_VESSEL_NOA_ID_FK"/>
            <one-to-many class="CrewMember"/>
        </set>

        <set name="certificates" table="VESSEL_NOA_CERTIFICATES" cascade="all">
            <key column="VESSEL_NOA_ID"
                 foreign-key="VESSEL_NOA_CERTIFICATES_VESSEL_NOA_ID_FK"/>
            <many-to-many
                    class="VesselCertificate"
                    column="VESSEL_CERTIFICATE_ID"
                    foreign-key="VESSEL_NOA_CERTIFICATES_VESSEL_CERTIFICATE_ID_FK"/>
        </set>

        <set name="equipmentInfo" table="VESSEL_NOA_EQUIPMENT_INFO" cascade="all">
            <key column="VESSEL_NOA_ID"
                 foreign-key="VESSEL_NOA_EQUIPMENT_INFO_VESSEL_NOA_ID_FK"/>
            <many-to-many
                    class="VesselEquipment"
                    column="VESSEL_EQUIPMENT_ID"
                    foreign-key="VESSEL_NOA_EQUIPMENT_INFO_VESSEL_EQUIPMENT_ID_FK"/>
        </set>

        <set name="securityInfo" table="VESSEL_NOA_SECURITY_INFO" cascade="all">
            <key column="VESSEL_NOA_ID"
                 foreign-key="VESSEL_NOA_SECURITY_INFO_VESSEL_NOA_ID_FK"/>
            <many-to-many
                    class="VesselSecurity"
                    column="VESSEL_SECURITY_ID"
                    foreign-key="VESSEL_NOA_SECURITY_INFO_VESSEL_SECURITY_ID_FK"/>
        </set>

        <list name="previousPorts" table="VESSEL_NOA_PREVIOUS_PORTS" cascade="all">
            <key column="VESSEL_NOA_ID"
                foreign-key="VESSEL_NOA_PREVIOUS_PORTS_VESSEL_NOA_ID_FK"/>
            <list-index column="PREVIOUS_PORT_INDEX"/>
            <many-to-many
                class="VesselPreviousPort"
                column="VESSEL_PREVIOUS_PORT_ID"
                foreign-key="VESSEL_NOA_PREVIOUS_PORTS_VESSEL_PREVIOUS_PORT_ID_FK"/>
        </list>
    </union-subclass>
</hibernate-mapping>

<hibernate-mapping package="gov.seahawk.model">
    <class name="CrewMember" table="CREW_MEMBER">
        <id name="key" column="ID" type="string">
            <generator class="guid"/>
        </id>

        <property name="position" column="POSITION" type="string"/>
        <property name="embarkDate" type="java.util.Date" column="EMBARK_DATE"/>
        <property name="debarkDate" type="java.util.Date" column="DEBARK_DATE"/>

        <component class="Location" name="embarkLocation">
            <property name="name" column="EMBARK_LOCATION_NAME" length="100"/>
        </component>

        <component class="Location" name="debarkLocation">
            <property name="name" column="DEBARK_LOCATION_NAME" length="100"/>
        </component>

        <many-to-one name="person"
                     class="Person"
                     column="PERSON_ID"
                     foreign-key="CREW_MEMBER_PERSON_ID_FK"
                     cascade="none"/>
    </class>
</hibernate-mapping>

<hibernate-mapping package="gov.seahawk.model">
    <class name="Passenger" table="PASSENGER">
        <id name="key" column="ID" type="string">
            <generator class="guid"/>
        </id>

        <property name="embarkDate" type="java.util.Date" column="EMBARK_DATE"/>
        <property name="debarkDate" type="java.util.Date" column="DEBARK_DATE"/>
        <component class="Location" name="embarkLocation">
            <property name="name" column="EMBARK_LOCATION_NAME"/>
        </component>

        <component class="Location" name="debarkLocation">
            <property name="name" column="DEBARK_LOCATION_NAME"/>
        </component>

        <many-to-one name="person"
                     class="Person"
                     column="PERSON_ID"
                     foreign-key="PASSENGER_PERSON_ID_FK"
                     cascade="none"/>
    </class>
</hibernate-mapping>




Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 01, 2006 5:36 pm 
Beginner
Beginner

Joined: Tue May 23, 2006 4:10 pm
Posts: 38
Location: Charleston, SC
We switched the crewMembers and passengers associations to bi-directional which did result in a dramatic performance improvement. I'm still curious if there are any other options.

Grant


Top
 Profile  
 
 Post subject: Avoid unnecessary updates
PostPosted: Sat Dec 02, 2006 1:28 am 
Newbie

Joined: Tue Sep 06, 2005 5:04 pm
Posts: 15
Use dynamic-update in your class mapping as indicated in the url below:

http://www.hibernate.org/hib_docs/refer ... pping.html


dynamic-update (optional, defaults to false): Specifies that UPDATE SQL should be generated at runtime and contain only those columns whose values have changed.

Hope this helps.

- San


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 02, 2006 11:29 am 
Beginner
Beginner

Joined: Tue May 23, 2006 4:10 pm
Posts: 38
Location: Charleston, SC
No, dynamic-update is not what I want. I already have dynamic-update set in my parent class mapping (vessel arrival). Our solution was to make the associations bi-directional. After making the switch our transaction times dropped from an average over 100 ms down to 60 ms. The bi-directional association allowed us to save a much smaller object graph each time through our loop. I was more curious about the versioning features of Hibernate and whether that might apply in my case.

Grant


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

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.