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.  [ 2 posts ] 
Author Message
 Post subject: Why is delete being called prior to insert on a *new* object
PostPosted: Tue Jun 06, 2006 4:15 am 
Newbie

Joined: Fri Feb 24, 2006 4:17 am
Posts: 4
I'm seeing some weird behavior with new transient objects being saved for the first time. The transient object, say Foo, has a child Set of "related" Foo instances. It's a one-to-many back to itself.

When I create a new Foo instance and also add some related Foo elements in the relatedFoo set what I'm seeing is that hibernate will insert the parent and then call a DELETE on the related_foos table even though none exist yet because this foo instance didn't even exist. Here's the SQL I'm seeing:

Code:

Hibernate: insert into foo (name, relative_popularity) values (?, ?)
Hibernate: update foo set name=?, relative_popularity=? where foo_id=?
Hibernate: delete from related_foos where foo_id=?
Hibernate: insert into related_foos (foo_id, related_foo_id, strength) values (?, ?, ?)
Hibernate: insert into related_fooss (foo_id, related_foo_id, strength) values (?, ?, ?)



Why the "DELETE from related foos" statement? I assume this has more to do with Sets of an existing object is being updated?

(also, as an asside, is there an easy way to make it so that the prepared statements that hibernate uses are set into a "debug" mode so that you can actually see the values being used for the parameters of the prepared statements instead of the "?" place holders?)

In general, I'm worried that hibernate will call delete statements often even though it doesn't really have to. The main reason I'm worried is because our DBAs won't actually give us DELETE privileges on any of our tables. They want us as a matter of policy to use a delete flag instead.

Hibernate version: 3.1.2

Mapping documents:

This is what the mapping looks like:

Code:

<hibernate-mapping default-access="field">
    <class name="FooImpl" table="foo">
        <id name="id" column="foo_id" unsaved-value="0">
            <generator class="native"/>
        </id>

        <property name="name" column="name" unique="true" length="50" not-null="true"/>
        <property name="relativePopularity" column="relative_popularity" not-null="true"/>

        <!-- many-to-many join table for related foos (which are optional) -->
      <set name="relatedFoos" table="related_foos" cascade="save-update" lazy="false">
            <key column="foo_id"/>
         <composite-element class="RelatedFooImpl">
            <parent name="foo"/>
            <many-to-one name="relatedFoo"
                      class="FooImpl"
                      column="related_foo_id"
                      not-null="true"
                      lazy="false"
                      cascade="save-update"/>
            <property name="strengthOfRelation" column="strength" not-null="true"/>
         </composite-element>
        </set>

    </class>
</hibernate-mapping>




Code between sessionFactory.openSession() and session.close():

All I'm simply doing is creating a new FooImpl object, adding to the Set of related Foos and calling save or update.

Full stack trace of any exception that occurs:

N/A

Name and version of the database you are using:

Occurs in both Mysql 5.x and Oracle 9.2.x

The generated SQL (show_sql=true):

Code:

Hibernate: insert into foo (name, relative_popularity) values (?, ?)
Hibernate: update foo set name=?, relative_popularity=? where foo_id=?
Hibernate: delete from related_foos where foo_id=?
Hibernate: insert into related_foos (foo_id, related_foo_id, strength) values (?, ?, ?)
Hibernate: insert into related_fooss (foo_id, related_foo_id, strength) values (?, ?, ?)



Debug level Hibernate log excerpt:

NA[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 3:15 am 
Beginner
Beginner

Joined: Fri Apr 28, 2006 7:48 am
Posts: 20
Add inverse="true" within your <set> element.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.