I discovered this problem while developing a new system with the latest NHibernate.Burrow distribution (which in turn uses the latest NHibernate distribution).I spent some time Googling around to check if anyboy else met the same problem and found some entries but none of them specified exactly why this failure was happening and how we can solve this problem.
The Problem
My code simply executes the following steps
-
Call a factory method
- Create a Curve
- Create CurveInterval objects
- Put CurveInterval objects inside the Curve.Intervals collection
- Call the same factory function to create another Curve instance
- Before creating Curve instance I perform some checks with HQL , this causes session to be flushed automaticaly
- I get the error when session is about to be flushed automatically
001 <?xml version='1.0' encoding='utf-8'?>
002
003<hibernate-mapping xmlns='urn:nhibernate-mapping-2.2'>
004
005<class
006
007name='GradeEntry.Domain.Curve, GradeEntry.Domain'
008
009 table= 'LetterGrade'>
010
011<id name='Id'
012
013column='LG_ID'
014
015unsaved-value='0'>
016
017<generator class='native'/>
018
019</id>
020
021
022
023<version name='ManagedVersion' column='NhVersion' type="System.Int64" unsaved-value="0"/>
024
025
026
027<property name='DateCreated'/>
028
029<property name='CreatedByPersonId'/>
030
031<property name='Locked'/>
032
033<property name='LockChangedById' column ='LockChangedByID'/>
034
035<property name='LockChangedOn'/>
036
037<property name='LockMessage'/>
038
039<property name='LogIdx'/>
040
041<property name='LogCnt'/>
042
043<property name='IsStable'/>
044
045
046
047<bag name='Intervals'
048
049order-by='LetterID asc'
050
051table='LetterGradeList'
052
053access='field.camelcase-underscore'
054
055inverse='true'
056
057cascade='delete-orphan, save-update'
058
059lazy='true'>
060
061<key column='LG_ID'/>
062
063<one-to-many class='GradeEntry.Domain.CurveInterval, GradeEntry.Domain' />
064
065</bag>
066
067
068
069<bag name='Log'
070
071order-by='Idx asc'
072
073table='LetterGradeLog'
074
075access='field.camelcase-underscore'
076
077inverse='true'
078
079cascade='delete-orphan,save-update'
080
081lazy='true'>
082
083<key column='LG_ID'/>
084
085<one-to-many class='GradeEntry.Domain.CurveLog, GradeEntry.Domain' />
086
087</bag>
088
089
090
091</class>
092
093</hibernate-mapping>
094
095
096
First Attempt To Reporoduce The Problem
I have prepared a sample project but I was not able to recreate the bug, and I know it is hard to tell what is the problem under these conditions. But I have some more findings about this issue.
- When I replace cascade of the bags to 'all-delete-orphan' from 'delete-orphan,save-update' everything is ok
- When I replace cascade of the bags to 'all, delete-orphan' from 'all-delete-orphan' I still get the assertion failure.
I think the problem is with the new implementation of cascade which is intended to support comma seperated list of cascade options.
Second Attempt To Reporoduce The Problem
After spending some more time trying to diagonise the problem I finally succeeded to reprouce the problem with atest case. I attached the test case.
Simply the problem resolves to something like this.
NOTE: All classes utilize managed versioning by defining ManagedVersion.
- I create 2 parent objects save them , flush the session and refresh them
- Then for each parent object I create some child objects of type Child1
- I perform HQL to get all Child3 instances. Child3 class does not have any association or cascade relation with Parent, Child1, Child2 and ParentRef classes. The HQL I performed causes the session to auto flush thus inserting Child1 instances to the database. But somehow while auto flushing the session versions of the parent objects are not updated. I expect two updates to be submitted to the database for parent objects since we associated Child1 instances with the parents and that must cause a version increment on parent objects.
- Then I try to perform another HQL query to get list of ParentRef objects and I get assertion failure. Note that ParentRef class is associated with Parent class which in turn defines cascade for Children1 and Children2 collections.
If we do not perform the HQL in step 3 Child1 instances will be inserted to the database and parent objects will be updated while performing the HQL in step 4 and we will not get the assertion failure.
I covered both cases in the attached test case.
Database scripts are included under DBScript folder.
Download test case
Requirements
Watch This
See Also
4eb234f0-f70a-4ac3-9186-3c326380b47a|0|.0