-->
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.  [ 6 posts ] 
Author Message
 Post subject: Nhibernate with WCF
PostPosted: Thu Sep 11, 2008 2:42 am 
Newbie

Joined: Tue Sep 09, 2008 3:20 am
Posts: 17
Hi,

I am using Nhibernate to retrieve data from database and pass the data to UI throw WCF, the retrieval of data from database works fine and when returning the data from WCF to UI i get the following error

The underlying connection was closed: The connection was closed unexpectedly.

Billow is the code i am using.

Code:
[DataContract]
    public class Patient
    {
        [DataMember]
        public virtual string DocId { get; set; }

        [DataMember]
        public virtual string FirstName
        {
            get;
            set;
        }

        [DataMember]
        public virtual string MiddleName
        {
            get;
            set;
        }

        [DataMember]
        public virtual string LastName
        {
            get;
            set;
        }
     
        [DataMember]
        public virtual Academic Academic
        {
            get;
            set;
        }
    }


[DataContract]
    public class Academic
    {
        [DataMember]
        public virtual string Code
        {
            get;
            set;
        }

        [DataMember]
        public virtual string Name
        {
            get;
            set;
        }
    }



This is the mapping i am using
Code:
<class name="MyProject.Patient, MyProject" table="PATIENTS" >

      <id name="DocId" column="DOCID" unsaved-value="0">
         <generator class="assigned" />
      </id>
      <property name ="FirstName" column="PATIENTFIRSTNAME"/>
      <property name ="MiddleName" column="PATIENTMIDDLENAME"/>
      <property name ="LastName" column="PATIENTLASTNAME"/>
      <many-to-one name="Academic" class="MyProject.Academic, MyProject" column="PATIENTEDUCATIONCODE" />
</class>
</hibernate-mapping>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
   <class name="MyProject.Academic, MyProject" table="Academics" >

      <id name="Code" column="ACADEMICCODE" unsaved-value="0">
         <generator class="assigned" />
      </id>
      <property name="Name" column="ACADEMICNAME" />
</class>
</hibernate-mapping>


The above code works fine if i don't use the many-to-one mapping to academic. but when i use this mapping ore any other mapping with relations i get the error,
The underlying connection was closed: The connection was closed unexpectedly.

I am new to NHibernate is there any thing i am missing.

Thanks

Arvind


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2008 7:53 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Hibernate uses lazy loading on associations per default. In your case a proxy object is instantiated for Academic. When you acccess the property, the proxy tries to load the object but has neither a connection nor the original session.

You have to either specify lazy="false" on the many-to-one or initialize the property before sending the object.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2008 1:57 am 
Newbie

Joined: Tue Sep 09, 2008 3:20 am
Posts: 17
Hi,

Thanks for your responce, I used lazy=false as u said on manu-to-one it gives me his error.

No row with the given identifier exists[MyProject.Academic#Academics]

this is what i did

<many-to-one name="Academic" class="MyProject.Academic, MyProject" column="PATIENTEDUCATIONCODE" lazy="false" />

I have initialized the property but still i get the error connection was not closed

Thanks

Arvind


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2008 3:08 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
1) Is Academics a mandatory value of a DataContract ? If not add not-found="ignore" to the many-to-one. Otherwise hibernate throws an exception if no object is found.

2) Sorry, I forgot that in the first place. Not only the association are lazily loaded by default but all objects. Add lazy="false" to the class or make sure that the objects are initialized before serialization (NHibernateUtil.Initialize(obj), NHibernate.Initialize(obj, "collection") I think)

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2008 5:06 am 
Newbie

Joined: Tue Sep 09, 2008 3:20 am
Posts: 17
Thank you very much it worked.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 03, 2009 5:17 pm 
Newbie

Joined: Fri Apr 03, 2009 5:15 pm
Posts: 1
It worked for me too, but I'd like too keep my relation lazy. What would be the best practice in that case?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.