-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Serializing generic collection in tiered application
PostPosted: Mon May 22, 2006 5:05 pm 
Newbie

Joined: Mon May 22, 2006 3:57 pm
Posts: 1
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


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 23, 2006 8:55 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
You need to have NHibernate.dll assembly on the clients for persistent collections to work. I remember somebody writing some custom serialization code to work around this limitation, but I don't remember any details, so try searching this forum.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 23, 2006 9:38 am 
Senior
Senior

Joined: Thu Aug 25, 2005 3:35 am
Posts: 160
persistent collections are a type defined in the nh-assembly. So, you will have to replace that type when you serialize and go to the type.
This does mean that you will need to maintain all the goodness that the persistent type offered you. So, basically, if it's an update/insert or delete.

Concrete, you will have to build that into your domain model. Not too hard and once done, it works like a charm.

I hope that helps, but I can't give any code.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.