I have a model such as:
entity1
hasmany(entity2)
entity2
hasmany(entity3)
at my test I am trying to get entity3 through entity1
and getting this message .
does anyone have some clue how to resolve it ?
my entities looks like this :
public class Entity1 : Entity
{
#region "Relationships"
private ICollection< Entity2> _col_ Entity2 = new HashSet<
Entity2 >();
public virtual ICollection< Entity2 > Col_ Entity2
{
get { return _col_ Entity2 ; }
protected set { _col_ Entity2 = value; }
}
#endregion
}
public class Entity2 : Entity
{
#region "Relationships"
private ICollection< Entity3> _col_ Entity3 = new HashSet<
Entity3 >();
public virtual ICollection< Entity3 > Col_ Entity3
{
get { return _col_ Entity3 ; }
protected set { _col_ Entity3 = value; }
}
#endregion
}
at the mapping :
public class Entity1 Mapping : ClassMap< Entity1 >
{
public Entity1Mapping()
{
try
{
LazyLoad();
Table("tableSource");
Id(x => x.ID).Length(10).Column("RECID");
Map(x => x.Col1).Column("COLUMN1");
HasMany<Entity2>(x => x.Col_ Entity2).AsBag()
.KeyColumn("COLUMN_OF_CHILD_TO_JOINBY")
.PropertyRef("Col1");
}
catch (NHibernate.HibernateException ex)
{
throw new Exception
(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType + "." +
System.Reflection.MethodBase.GetCurrentMethod().Name, ex);
}
}
}
----------------------
public class Entity2 Mapping : ClassMap < Entity2 >
{
//public virtual int Shm_medinaid { get; set; }
//public virtual int Shm_sherutid { get; set; }
public Entity2Mapping()
{
try
{
LazyLoad();
Table("tableSource2");
Id(x => x.ID).Length(10).Column("RECID");
Map(x => x.Col1).Column("COL1");
Map(x => x.Col2).Column("COL2");
HasMany<Entity3>(x => x.Col_Entity3).AsBag()
.KeyColumn("COLUMN_OF_CHILD_TO_JOINBY")
.PropertyRef("Col2");
}
catch (NHibernate.HibernateException ex)
{
throw new Exception
(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType + "." +
System.Reflection.MethodBase.GetCurrentMethod().Name, ex);
}
}
}
--------------------------------------
So at my test I am trying to reach the Entity3
[Test]
public void TestMapping()
{
Entity1 _entity1;
using (ISession Session =
NHibernateSessionProvider.GetSession())
{
long RECID = 847;
using (NHibernate.ITransaction Tran =
Session.BeginTransaction())
{
_entity1 = Session.Get< Entity1>(TMD_RECID);
Assert.AreNotEqual(_entity1.Col_Entity2, null);
IList<Entity2> _entity2 = new List<Entity2>
(_entity1.Col_Entity2);
IList<Entity3> _entity3 = new List<Entity3>
(_entity2[0].Col_Entity3);
Assert.AreNotEqual(_entity3[0].Col1, 0);
Tran.Commit();
}
}
}
-----------------------
I am failing at IList<Entity3> _entity3 = new List<Entity3>(_entity2
[0].Col_Entity3);
Initializing[Entity2#1]-failed to lazily initialize a collection of
role: Entity2.Col_Entity3, no session or session was closed
NHibernate.LazyInitializationException: Initializing[Entity2#1]-
failed to lazily initialize a collection of role:
Entity2.Col_Entity3, no session or session was closed
at
NHibernate.Collection.AbstractPersistentCollection.ThrowLazyInitializationException
(String message)
will appreciate any input on the subject .
--
You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group.
To post to this group, send email to fluent-n...@googlegroups.com.
To unsubscribe from this group, send email to fluent-nhibern...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/fluent-nhibernate?hl=en.
1). Removed LazyLoad from general mapping and mapped only the
relationships as LazyLoad.
2).Removed the try- catch from the mapping, and left only try-catch in
configuration file
3).The previous test looked like this :
_productRetrieval = Session.Get<Entity1>(TMD_RECID);
Assert.AreNotEqual(_productRetrieval.Col_Entity2,
null);
IEnumerator<Entity2> _entity2List = _entity1.Col_
Entity2.GetEnumerator();
_Entity2List.MoveNext();
IEnumerator<Entity3> _entity3List =
_entity2List.Current.Col_Entity3.GetEnumerator();
_entity3List.MoveNext();
Assert.AreNotEqual(_entity3List.Current.Col2, 0);
And still I've got the mistake of initializing lazyload.
4).I"ll check the option of
CompositeId().AddPropertyKey(x=> x.Col1).AddPropertyKey(x=>x.Col2)
And get back to you on that.
If you have some example that shows joining by 2 column other than ID
that will help us a lot .
> > fluent-nhibern...@googlegroups.com<fluent-nhibernate%2Bunsubscr i...@googlegroups.com>
Session.Refresh(_entity3List.Current);
than it seems to open the proxies , refresh the object and to get the
data.
it seems the wrong approach . doesn't feel wright.
> ...
>
> read more »
To unsubscribe from this group, send email to fluent-nhibern...@googlegroups.com.