Dears,
I have a parent with two children objects in database. I try to update the parent and its children via creating new parent (with same id) and adding a new children to this parent. I expect that all old children should be removed when I persist this new parent object. But, those two children are remained unchanged!!
Sorry for long log trace, i hope this help you to detect the problem.
Thanks for your attention,
Masoud.
Hibernate version: Hibernate 2.1.6
Mapping documents:
Code:
<class name="Post" table="T04_POSTS">
...
<set
name="postalBags"
cascade="all-delete-orphan"
inverse="true">
<key column="POST_ID"/>
<one-to-many class="PostalBag"/>
</set>
...
</class>
<class name="PostalBag" table="T04_POSTAL_BAGS">
...
<many-to-one name="post" column="POST_ID" not-null="true"/>
...
</class>
Code between sessionFactory.openSession() and session.close():Code:
Post post = new Post();
post.setId(new Long(1));
post.setName("Post-11112");
PostalBag pb2 = new PostalBag("bag-44");
post.addPostalBag(pb2);
session.saveOrUpdate(post);
Full stack trace of any exception that occurs:no exception
Name and version of the database you are using:Oracle 10g
The generated SQL (show_sql=true):
Hibernate: select S04_PostalBag_Sequence.nextval from dual
Hibernate: insert into T04_POSTAL_BAGS (POST_ID, name, created, id) values (?, ?, ?, ?)
Hibernate: update T04_POSTS set name=? where id=?
Debug level Hibernate log excerpt:
[java] DEBUG [main] (JDBCTransaction.java:41) - current autocommit status:false
[java] DEBUG [main] (Cascades.java:341) - id unsaved-value strategy NULL
[java] DEBUG [main] (SessionImpl.java:1392) - saveOrUpdate() previously saved instance with id: 1
[java] DEBUG [main] (SessionImpl.java:1440) - updating [net.sf.hiber.four.domain.Post#1]
[java] DEBUG [main] (SessionImpl.java:1287) - collection dereferenced while transient [net.sf.hiber.four.domain.Post.postalBags#1]
[java] DEBUG [main] (Cascades.java:497) - processing cascades for: net.sf.hiber.four.domain.Post
[java] DEBUG [main] (Cascades.java:524) - cascading to collection: net.sf.hiber.four.domain.Post.postalBags
[java] DEBUG [main] (Cascades.java:113) - cascading to saveOrUpdate()
[java] DEBUG [main] (SessionImpl.java:1387) - saveOrUpdate() unsaved instance
[java] DEBUG [main] (BatcherImpl.java:200) - about to open: 0 open PreparedStatements, 0 open ResultSets
[java] DEBUG [main] (BatcherImpl.java:226) - select S04_PostalBag_Sequence.nextval from dual
[java] Hibernate: select S04_PostalBag_Sequence.nextval from dual
[java] DEBUG [main] (BatcherImpl.java:249) - preparing statement
[java] DEBUG [main] (SequenceGenerator.java:81) - Sequence identifier generated: 5
[java] DEBUG [main] (BatcherImpl.java:207) - done closing: 0 open PreparedStatements, 0 open ResultSets
[java] DEBUG [main] (BatcherImpl.java:269) - closing statement
[java] DEBUG [main] (SessionImpl.java:778) - generated identifier: 5
[java] DEBUG [main] (SessionImpl.java:825) - saving [net.sf.hiber.four.domain.PostalBag#5]
[java] DEBUG [main] (Cascades.java:497) - processing cascades for: net.sf.hiber.four.domain.PostalBag
[java] DEBUG [main] (Cascades.java:113) - cascading to saveOrUpdate()
[java] DEBUG [main] (SessionImpl.java:1372) - saveOrUpdate() persistent instance
[java] DEBUG [main] (Cascades.java:506) - done processing cascades for: net.sf.hiber.four.domain.PostalBag
[java] DEBUG [main] (Cascades.java:497) - processing cascades for: net.sf.hiber.four.domain.PostalBag
[java] DEBUG [main] (Cascades.java:506) - done processing cascades for: net.sf.hiber.four.domain.PostalBag
[java] DEBUG [main] (Cascades.java:506) - done processing cascades for: net.sf.hiber.four.domain.Post
[java] DEBUG [main] (JDBCTransaction.java:59) - commit
[java] DEBUG [main] (SessionImpl.java:2246) - flushing session
[java] DEBUG [main] (Cascades.java:497) - processing cascades for: net.sf.hiber.four.domain.Post
[java] DEBUG [main] (Cascades.java:524) - cascading to collection: net.sf.hiber.four.domain.Post.postalBags
[java] DEBUG [main] (Cascades.java:113) - cascading to saveOrUpdate()
[java] DEBUG [main] (SessionImpl.java:1372) - saveOrUpdate() persistent instance
[java] DEBUG [main] (Cascades.java:506) - done processing cascades for: net.sf.hiber.four.domain.Post
[java] DEBUG [main] (Cascades.java:497) - processing cascades for: net.sf.hiber.four.domain.PostalBag
[java] DEBUG [main] (Cascades.java:113) - cascading to saveOrUpdate()
[java] DEBUG [main] (SessionImpl.java:1372) - saveOrUpdate() persistent instance
[java] DEBUG [main] (Cascades.java:506) - done processing cascades for: net.sf.hiber.four.domain.PostalBag
[java] DEBUG [main] (SessionImpl.java:2439) - Flushing entities and processing referenced collections
[java] DEBUG [main] (WrapVisitor.java:81) - Wrapped collection in role: net.sf.hiber.four.domain.Post.postalBags
[java] DEBUG [main] (SessionImpl.java:2533) - Updating entity: [net.sf.hiber.four.domain.Post#1]
[java] DEBUG [main] (SessionImpl.java:2884) - Collection found: [net.sf.hiber.four.domain.Post.postalBags#1], was: [<unreferenced>]
[java] DEBUG [main] (SessionImpl.java:2780) - Processing unreferenced collections
[java] DEBUG [main] (SessionImpl.java:2794) - Scheduling collection removes/(re)creates/updates
[java] DEBUG [main] (SessionImpl.java:2270) - Flushed: 1 insertions, 1 updates, 0 deletions to 2 objects
[java] DEBUG [main] (SessionImpl.java:2275) - Flushed: 1 (re)creations, 0 updates, 1 removals to 1 collections
[java] DEBUG [main] (Printer.java:75) - listing entities:
[java] DEBUG [main] (Printer.java:82) - net.sf.hiber.four.domain.PostalBag{created=null, name=bag-44, post=Post#1, id=5}
[java] DEBUG [main] (Printer.java:82) - net.sf.hiber.four.domain.Post{created=null, postalBags=[PostalBag#5], name=Post-11112, id=1}
[java] DEBUG [main] (SessionImpl.java:2359) - executing flush
[java] DEBUG [main] (EntityPersister.java:453) - Inserting entity: [net.sf.hiber.four.domain.PostalBag#5]
[java] DEBUG [main] (BatcherImpl.java:200) - about to open: 0 open PreparedStatements, 0 open ResultSets
[java] DEBUG [main] (BatcherImpl.java:226) - insert into T04_POSTAL_BAGS (POST_ID, name, created, id) values (?, ?, ?, ?)
[java] Hibernate: insert into T04_POSTAL_BAGS (POST_ID, name, created, id) values (?, ?, ?, ?)
[java] DEBUG [main] (BatcherImpl.java:249) - preparing statement
[java] DEBUG [main] (EntityPersister.java:388) - Dehydrating entity: [net.sf.hiber.four.domain.PostalBag#5]
[java] DEBUG [main] (NullableType.java:46) - binding '1' to parameter: 1
[java] DEBUG [main] (NullableType.java:46) - binding 'bag-44' to parameter: 2
[java] DEBUG [main] (NullableType.java:41) - binding null to parameter: 3
[java] DEBUG [main] (NullableType.java:46) - binding '5' to parameter: 4
[java] DEBUG [main] (BatchingBatcher.java:28) - Adding to batch
[java] DEBUG [main] (BatchingBatcher.java:50) - Executing batch size: 1
[java] DEBUG [main] (BatchingBatcher.java:58) - success of batch update unknown: 0
[java] DEBUG [main] (BatcherImpl.java:207) - done closing: 0 open PreparedStatements, 0 open ResultSets
[java] DEBUG [main] (BatcherImpl.java:269) - closing statement
[java] DEBUG [main] (EntityPersister.java:648) - Updating entity: [net.sf.hiber.four.domain.Post#1]
[java] DEBUG [main] (BatcherImpl.java:200) - about to open: 0 open PreparedStatements, 0 open ResultSets
[java] DEBUG [main] (BatcherImpl.java:226) - update T04_POSTS set name=? where id=?
[java] Hibernate: update T04_POSTS set name=? where id=?
[java] DEBUG [main] (BatcherImpl.java:249) - preparing statement
[java] DEBUG [main] (EntityPersister.java:388) - Dehydrating entity: [net.sf.hiber.four.domain.Post#1]
[java] DEBUG [main] (NullableType.java:46) - binding 'Post-11112' to parameter: 1
[java] DEBUG [main] (NullableType.java:46) - binding '1' to parameter: 2
[java] DEBUG [main] (BatchingBatcher.java:28) - Adding to batch
[java] DEBUG [main] (BatchingBatcher.java:50) - Executing batch size: 1