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]