Hi All,
I have a class I'm persisting with NHibernate. The basic CRUD operations all work, except for a one-to-many relationship on the class. The property that exposes the relationship is a generic IList of Requestor objects.
When I call SaveOrUpdate, the collection is cleared by the following lines (SessionImpl.cs:947):
Code:
if (substitute)
{
persister.SetPropertyValues(theObj, values);
}
... so by the time it actually gets to generating SQL, there are no Requestors to persist.
Could someone please give me a heads-up here? I'm fairly sure I'm missing something simple in terms of the attributes I'm using to mark up the model but I'm damned if I can figure out what.
Yours,
Duncan Bayne
Hibernate version: 1.2.0.4000
Mapping documents:I'm generating mapping documents from the following NHibernate attributes:
Code:
[Serializable]
[Class(Table = "HistologyCase", Lazy = false)]
[DataContract]
public class HistologyCase : AuditableDomainObject
{
private List<Requestor> m_requestors = new List<Requestor>();
[Id(Name = "HistologyCaseId", Column = "HistologyCaseId", UnsavedValueObject = 0)]
[Generator(1, Class = "native")]
[DataMember]
public virtual int HistologyCaseId { get; set; }
...
[Bag(Cascade = CascadeStyle.All, Inverse = true)]
[Key(1, Column = "HistologyCaseId")]
[OneToMany(2, ClassType = typeof (Requestor))]
[DataMember]
public virtual IList<Requestor> Requestors
{
get { return m_requestors; }
set
{
if (m_requestors != null)
{
m_requestors.Clear();
m_requestors.AddRange(value);
foreach (Requestor requestor in m_requestors)
{
requestor.HistologyCase = this;
}
}
}
}
}
Code between sessionFactory.openSession() and session.close():Code:
session.SaveOrUpdate(histologyCase);
session.Flush();
Debug level Hibernate log excerpt:Code:
16:16:06.322 [7] DEBUG NHibernate.Impl.SessionImpl - creating collection wrapper:[MyServer.CaseManagement.Entities.HistologyCase.Requestors#15]
16:16:06.322 [7] DEBUG NHibernate.Impl.SessionImpl - initializing collection [MyServer.CaseManagement.Entities.HistologyCase.Requestors#15]
16:16:06.322 [7] DEBUG NHibernate.Impl.SessionImpl - checking second-level cache
16:16:06.322 [7] DEBUG NHibernate.Impl.SessionImpl - collection not cached
16:16:06.322 [7] DEBUG NHibernate.Loader.Loader - loading collection: [MyServer.CaseManagement.Entities.HistologyCase.Requestors#15]
16:16:06.322 [7] DEBUG NHibernate.Impl.BatcherImpl - Opened new IDbCommand, open IDbCommands: 1
16:16:06.322 [7] DEBUG NHibernate.Impl.BatcherImpl - Building an IDbCommand object for the SqlString: SELECT requestors0_.HistologyCaseId as Histolog5___1_, requestors0_.PersonId as PersonId1_, requestors0_.PersonId as PersonId0_0_, requestors0_.RequestorTypeId as Requesto2_7_0_, requestors0_.ProviderNumber as Provider3_7_0_, requestors0_.RequestorIdentifier as Requesto4_7_0_, requestors0_.HistologyCaseId as Histolog5_7_0_, requestors0_1_.UpdatedDate as UpdatedD2_0_0_, requestors0_1_.FirstName as FirstName0_0_, requestors0_1_.MiddleName as MiddleName0_0_, requestors0_1_.LastName as LastName0_0_, requestors0_1_.GenderId as GenderId0_0_, requestors0_1_.DateOfBirth as DateOfBi7_0_0_, requestors0_1_.UpdatedBy as UpdatedBy0_0_, requestors0_1_.CreatedDate as CreatedD9_0_0_, requestors0_1_.CreatedBy as CreatedBy0_0_ FROM Requestor requestors0_ inner join Person requestors0_1_ on requestors0_.PersonId=requestors0_1_.PersonId WHERE requestors0_.HistologyCaseId=?
16:16:06.322 [7] DEBUG NHibernate.Type.Int32Type - binding '15' to parameter: 0
16:16:06.322 [7] INFO NHibernate.Loader.Loader - SELECT requestors0_.HistologyCaseId as Histolog5___1_, requestors0_.PersonId as PersonId1_, requestors0_.PersonId as PersonId0_0_, requestors0_.RequestorTypeId as Requesto2_7_0_, requestors0_.ProviderNumber as Provider3_7_0_, requestors0_.RequestorIdentifier as Requesto4_7_0_, requestors0_.HistologyCaseId as Histolog5_7_0_, requestors0_1_.UpdatedDate as UpdatedD2_0_0_, requestors0_1_.FirstName as FirstName0_0_, requestors0_1_.MiddleName as MiddleName0_0_, requestors0_1_.LastName as LastName0_0_, requestors0_1_.GenderId as GenderId0_0_, requestors0_1_.DateOfBirth as DateOfBi7_0_0_, requestors0_1_.UpdatedBy as UpdatedBy0_0_, requestors0_1_.CreatedDate as CreatedD9_0_0_, requestors0_1_.CreatedBy as CreatedBy0_0_ FROM Requestor requestors0_ inner join Person requestors0_1_ on requestors0_.PersonId=requestors0_1_.PersonId WHERE requestors0_.HistologyCaseId=@p0
16:16:06.322 [7] DEBUG NHibernate.SQL - SELECT requestors0_.HistologyCaseId as Histolog5___1_, requestors0_.PersonId as PersonId1_, requestors0_.PersonId as PersonId0_0_, requestors0_.RequestorTypeId as Requesto2_7_0_, requestors0_.ProviderNumber as Provider3_7_0_, requestors0_.RequestorIdentifier as Requesto4_7_0_, requestors0_.HistologyCaseId as Histolog5_7_0_, requestors0_1_.UpdatedDate as UpdatedD2_0_0_, requestors0_1_.FirstName as FirstName0_0_, requestors0_1_.MiddleName as MiddleName0_0_, requestors0_1_.LastName as LastName0_0_, requestors0_1_.GenderId as GenderId0_0_, requestors0_1_.DateOfBirth as DateOfBi7_0_0_, requestors0_1_.UpdatedBy as UpdatedBy0_0_, requestors0_1_.CreatedDate as CreatedD9_0_0_, requestors0_1_.CreatedBy as CreatedBy0_0_ FROM Requestor requestors0_ inner join Person requestors0_1_ on requestors0_.PersonId=requestors0_1_.PersonId WHERE requestors0_.HistologyCaseId=@p0; @p0 = '15'
16:16:06.322 [7] DEBUG NHibernate.Connection.DriverConnectionProvider - Obtaining IDbConnection from Driver
16:16:06.322 [7] DEBUG NHibernate.Impl.BatcherImpl - Opened IDataReader, open IDataReaders: 1
16:16:06.322 [7] DEBUG NHibernate.Loader.Loader - result set contains (possibly empty) collection: [MyServer.CaseManagement.Entities.HistologyCase.Requestors#15]
16:16:06.322 [7] DEBUG NHibernate.Impl.SessionImpl - uninitialized collection: initializing