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