From: NHibernate J. <mik...@us...> - 2006-12-04 23:34:33
|
ICriteria does not automatically flush the session for many-to-many association change -------------------------------------------------------------------------------------- Key: NH-830 URL: http://jira.nhibernate.org/browse/NH-830 Project: NHibernate Type: Bug Components: Core Versions: 1.2.0.Beta2 Reporter: Kailuo Wang Attachments: SessFlush.zip It seems that, unlike IQuery, the ICriteria won't automatically flush the session for many-to-many association changes. Note that these symptom only happens when the following 2 conditions are satisfied: 1) the change is about a many-to-many association. It seems work fine with changes to one-to-many assocication 2) the related entities are just reloaded from the database prior to the query. If the entities are just created and saved, the criteria will flush the session prior the query. While you can also find the test case in the attachment, here is the code: //Setup the test data Cat mum = new Cat(); Cat son = new Cat(); sess.Save(mum); sess.Save(son); sess.Flush(); //reload the data and then setup the many-to-many association mum = sess.Get<Cat>(mum.Id); son = sess.Get<Cat>(son.Id); mum.Children.Add(son); son.Parents.Add(mum); sess.FlushMode = FlushMode.Auto; //Use criteria API to search first IList result = sess.CreateCriteria(typeof (Cat)) .CreateAlias("Children", "child") .Add(Expression.Eq("child.Id", son.Id)) .List(); //the criteria failed to find the mum cat with the child Assert.AreEqual(0, result.Count); //then use HQL query to search result = sess.CreateQuery("from Cat c where :son in elements c.Children ") .SetEntity("son", son) .List(); //HQL query automatically flush the sess and thus find the change Assert.AreEqual(1, result.Count); //then use the same criteria to search again result = sess.CreateCriteria(typeof(Cat)) .CreateAlias("Children", "child") .Add(Expression.Eq("child.Id", son.Id)) .List(); //find the result this time since the session was already flushed by the previous query Assert.AreEqual(1, result.Count); <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" schema="dbo" assembly="NHSessTest" default-lazy="false"> <class name="NHSessTest.Cat" > <id name="Id" type="Int32" unsaved-value="0" > <generator class="identity" /> </id> <set name="Children" lazy="true" table="ChildrenParents" > <key column="Parent" /> <many-to-many class="NHSessTest.Cat" column="Child" /> </set> <set name="Parents" lazy="true" inverse="true" table="ChildrenParents" > <key column="Child" /> <many-to-many class="NHSessTest.Cat" column="Parent" /> </set> </class> </hibernate-mapping> namespace NHSessTest { public class Cat { private int id; private ISet children = new HashedSet(); private ISet parents = new HashedSet(); public int Id { get { return id; } set { id = value; } } public ISet Children { get { return children; } set { children = value; } } public ISet Parents { get { return parents; } set { parents = value; } } } } -- This message is automatically generated by JIRA. - If you think it was sent incorrectly contact one of the administrators: http://jira.nhibernate.org/secure/Administrators.jspa - For more information on JIRA, see: http://www.atlassian.com/software/jira |