Hi, all
I'm building a 3-tier solution against an existing legacy database system, using NHibernate/.NET remoting for the middle-tier. I would prefer not to have any knowledge at all to NHibernate on client side, and NHibernate actually works like a charm except for the scenario below where I'm trying to serialize a collection to the client. The client bombs out, yelling about not finding the NHibernate assembly.
Hibernate version:
From svn
Mapping documents:
File A:
Code:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class
name="ida.db.Dokumenter, IdaFelles"
table="DOKUMENTER"
>
<id
name="Id"
type="integer"
column="DOKUMENT_ID"
unsaved-value="-1"
>
<generator class="sequence">
<param name="sequence">DOKUMENT_SEQ</param>
</generator>
</id>
<version
name="MetaVersjon"
column="META_VERSJON"
type="integer"
unsaved-value="negative"
/>
<property
name="Tittel"
column="TITTEL"
type="string"
not-null="true"
length="60"
/>
<property
name="DokumentDato"
column="DOKUMENT_DATO"
type="date"
not-null="true"
length="7"
/>
<property
name="FilNavn"
column="FIL_NAVN"
type="string"
not-null="false"
length="60"
/>
</class>
</hibernate-mapping>
File B:Code:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class
name="ida.db.Dokumentgrupper, IdaFelles"
table="DOKUMENTGRUPPER"
>
<id
name="Id"
type="integer"
column="GRUPPE_ID"
>
<generator class="assigned"/>
</id>
<many-to-one
name="Hoveddok"
column="HOVEDDOK_ID"
class="ida.db.Dokumenter, IdaFelles"
not-null="true"
>
</many-to-one>
<set
name="Koblinger"
table="DOKUMENTKOBLINGER"
cascade="all"
generic="true"
lazy="true"
>
<key column="GRUPPE_ID"/>
<many-to-many column="DOKUMENT_ID" class="ida.db.Dokumenter, IdaFelles"/>
</set>
</class>
</hibernate-mapping>
File C:Code:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class
name="ida.db.Dokumentkoblinger, IdaFelles"
table="DOKUMENTKOBLINGER"
>
<composite-id>
<key-many-to-one
name="Dokument"
class="ida.db.Dokumenter, IdaFelles"
column="DOKUMENT_ID"
/>
<key-many-to-one
name="Gruppe"
class="ida.db.Dokumentgrupper, IdaFelles"
column="GRUPPE_ID"
/>
</composite-id>
</class>
</hibernate-mapping>
Code called from client:Code:
public Dokumentgrupper HentDokumentGrupper(int dokumentId)
{
ISession session = DataSession.IdaSession;
try
{
ICriteria criteria = session.CreateCriteria(typeof(Dokumentgrupper));
criteria.SetFetchMode("Koblinger", FetchMode.Eager);
criteria.Add(Expression.Eq("Hoveddok.Id", dokumentId));
Dokumentgrupper dg = (Dokumentgrupper)criteria.UniqueResult();
return dg;
}
catch (Exception ex)
{
log.Error(ex);
throw new IDAException(ex.Message);
}
finally
{
session.Close();
}
}
Someone with a brain pointed out in
http://forum.hibernate.org/viewtopic.php?t=954345 to use FetchMode.Eager to initialize the collection and, as I understand it, in this way decouple NHibernate - but it just doesn't happen. However, if I use the following code snippet it works like expected:
Code:
...
ICriteria criteria = session.CreateCriteria(typeof(Dokumentgrupper));
criteria.SetFetchMode("Koblinger", FetchMode.Eager);
criteria.Add(Expression.Eq("Hoveddok.Id", dokumentId));
Dokumentgrupper dg = (Dokumentgrupper)criteria.UniqueResult();
Dokumentgrupper dg2 = new Dokumentgrupper();
dg2.Id = dg.Id;
dg2.Hoveddok = dg.Hoveddok;
dg2.Koblinger = new Iesi.Collections.Generic.HashedSet<Dokumenter>();
dg2.Koblinger.AddAll(dg.Koblinger);
return dg2;
...
As I'm using NHibernate from svn, I would not expect people come screaming to help me out, but I hope someone has a understandable answer to why it behaves like this - me being braindead, not implemented features, bug in NHibernate or whatever.
Regards
kjell