-->
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: Mapped collections not being fetched
PostPosted: Tue Jun 05, 2007 8:41 am 
Newbie

Joined: Tue Jun 05, 2007 7:50 am
Posts: 2
Hello all, I hope someone can help me with what I'm sure is a simple mis-conception on my part.

I have a simple nhibernate setup working, except for relationship collections. I have a (what should be) simple many-to-many mapping between two entities.

Hibernate version: 1.2,
SQLServer/VS 2005,

Class:

Code:
public class Laboratory {
        private string labCode;
        private string address;
        private ISet<Test> tests;
         
        public virtual string LabCode {
            get { return this.labCode; }
            set { this.labCode = value; }
        }

        public virtual string Address {
            get { return this.address; }
            set { this.address = value; }
        }

        public virtual ISet<Test> Tests {
            get { return this.tests; }
            set { this.tests = value; }
        }
}   


Mapping document:

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Laboratory" table="Laboratory">
    <id name="LabCode" type="System.String" column="LabCode">
      <generator class="native" />
    </id>
    <property name="Address" type="System.String" column="Address">

    <set name="Tests" table="LabTest" lazy="false">
      <key>
         <column name="LabCode" not-null="true"/>
      </key>
      <many-to-many class="Test" lazy="false">
        <column name="TestId" not-null="true"/>
      </many-to-many>
    </set>

  </class>
</hibernate-mapping>



I also have simple code/hbm for a Test class (with no mapping back to Laboratory).

The SQL for the db is as follows:

Code:
CREATE TABLE [dbo].[Laboratory](
   [LabCode] [nchar](10) NOT NULL,
   [Address] [nvarchar](50)  NOT NULL,
        CONSTRAINT [PK_Laboratory] PRIMARY KEY CLUSTERED <...>


CREATE TABLE [dbo].[Test](
   [TestId] [nchar](10) NOT NULL,
   [Name] [nchar](10) NOT NULL,
        CONSTRAINT [PK_Test] PRIMARY KEY CLUSTERED <...>

CREATE TABLE [dbo].[LabTest](
   [LabCode] [nchar](10) NOT NULL,
   [TestId] [nchar](10) NOT NULL,
        CONSTRAINT [PK_LabTest] PRIMARY KEY CLUSTERED  <...>

ALTER TABLE [dbo].[LabTest]  WITH CHECK ADD  CONSTRAINT [FK_LabTest_Laboratory] FOREIGN KEY([LabCode])
REFERENCES [dbo].[Laboratory] ([LabCode])

ALTER TABLE [dbo].[LabTest]  WITH CHECK ADD  CONSTRAINT [FK_LabTest_Test] FOREIGN KEY([TestId])
REFERENCES [dbo].[Test] ([TestId])



The following code always sets Label2 to 0, regardless of then fact that there are a few valid mappings in the mapping table, but works otherwise.

Code:
Laboratory lab = session.Get<Laboratory>("MANC");
Label1.Text = lab.Address;
Label2.Text = lab.Tests.Count.ToString();


Generally, I can create and update properties/relationships and they are persisted to the db, but when I try (in a different session) to read that information back like above, it does not work. I have tried almost every option possible in the mapping spec regards loading, but to no avail. I have tried to enable SQL logging, but its in an asp.net website project and I couldn't get logging working at all.

Any ideas?

Cheers

Simon


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 06, 2007 3:43 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
I noticed that your Laboratory table has an NCHAR-based key, rather than NVARCHAR, and you are using keys with business meaning that do not fill up the key to its maximum length. We have some tables where this is also the case, and we've encountered weirdness where the collections never get lazy loaded.

It appears to be due to calling ISession.Get() or ISession.Load() with a trimmed version of the key. NHibernate then keeps that trimmed version in some internal table. However, the entity (and some other place internally in NHibernate?) the key value returned by the database is right-padded with spaces. They don't match, which apparently makes NHibernate fail to lazy-load the collections. To avoid the problem, we had to make a front-end to ISession.Get() and ISession.Load() that right-pads the passed-in key value when the key is NCHAR-based rather than NVARCHAR-based.

Try using a key value in your Laboratory table that's a full 10 characters (no whitespace), and see if the problem mysteriously disappears ...

BTW, regarding logging -- we had to manually copy log4net.dll to our web project's Bin folder to get logging to work, Visual Studio wouldn't pick up the dependency, and even creating a .refresh file didn't help ...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 21, 2007 6:59 am 
Newbie

Joined: Tue Jun 05, 2007 7:50 am
Posts: 2
Nels_P_Olsen wrote:
It appears to be due to calling ISession.Get() or ISession.Load() with a trimmed version of the key. NHibernate then keeps that trimmed version in some internal table. However, the entity (and some other place internally in NHibernate?) the key value returned by the database is right-padded with spaces. They don't match, which apparently makes NHibernate fail to lazy-load the collections. To avoid the problem, we had to make a front-end to ISession.Get() and ISession.Load() that right-pads the passed-in key value when the key is NCHAR-based rather than NVARCHAR-based


Bingo! That's the problem. Many thanks for your detailed answer, just what I needed.

Although, we were evaluating nHibernate along side Linq and other technologies, and the amount of effort involved in nHibernate was significantly more than Linq, so we decided to go with that technology in the end, even with its smaller feature set.


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.