Howdy All,
I'm wondering if there is any good documentation or if somebody out
there can help me understand how NHibernate hbm files should be
structured for multi-layered relationships and multi-key foreign keys.
I have a table(table1) that has a multi field foreign key to another
table(table2) that then one of those columns is a single foreign key
to another table(table3). From what I've been able to understand
about these relationships the foreign keys are not directly available
on the object itself but on the "child" object.
Here is what I have for the hbm files:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="MyDataLayer.Data.Entities" assembly="MyDataLayer.Data">
<class name="Table1" table="XYZ.Table1" >
<composite-id>
<key-property name="Sdocid" column="SDOCID" type="String"
length="64" />
<key-property name="Ipagestart" column="IPAGESTART"
type="Int32" />
<key-many-to-one name="Table2" class="Table2" >
<column name="SCASEID" />
<column name="DCREATED" />
</key-many-to-one>
</composite-id>
<property name="Ipageend" column="IPAGEEND" type="Int32" not-
null="true" />
<property name="Isendorder" column="ISENDORDER" type="Int32"
not-null="false" />
<property name="Sdoctypeid" column="SDOCTYPEID" type="String"
not-null="false" length="15" />
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="MyDataLayer.Data.Entities" assembly="MyDataLayer.Data">
<class name="Table2" table="XYZ.Table2" >
<composite-id>
<key-property name="Dcreated" column="DCREATED"
type="DateTime" />
<key-many-to-one name="Table3" class="Table3" >
<column name="SCASEID" />
</key-many-to-one>
</composite-id>
<property name="Suserid" column="SUSERID" type="String" not-
null="false" length="8" />
...
<property name="Ssendstatus" column="SSENDSTATUS" type="Char"
not-null="false" length="1" />
<bag name="Table1List" inverse="true" cascade="all-delete-
orphan" lazy="true" >
<key>
<column name="SCASEID" />
<column name="DCREATED" />
</key>
<one-to-many class="Table1" />
</bag>
<bag name="Table4List" inverse="true" cascade="all-delete-
orphan" lazy="true" >
<key>
<column name="SCASEID" />
<column name="DCREATED" />
</key>
<one-to-many class="Table4" />
</bag>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="MyDataLayer.Data.Entities" assembly="MyDataLayer.Data">
<class name="Table3" table="XYZ.Table3" >
<id name="Scaseid" column="SCASEID" type="String" length="16"
>
<generator class="native" />
</id>
<property name="Sworktype" column="SWORKTYPE" type="String"
not-null="true" length="16" />
...
<property name="Ftotpurchamount" column="FTOTPURCHAMOUNT"
type="Double" not-null="false" />
<many-to-one name="Sacqbinica" class="Acqbinica" not-
null="false" >
<column name="SACQBINICA" />
</many-to-one>
...
<bag name="Table2List" inverse="true" cascade="all-delete-
orphan" lazy="true" >
<key>
<column name="SCASEID" />
</key>
<one-to-many class="Table2" />
</bag>
</class>
</hibernate-mapping>
So that for the above tables the GetByKey for table one would look
like this:
public static MyDataLayer.Data.Entities.Table1 GetByKey(this
IQueryable<MyDataLayer.Data.Entities.Table1> queryable
, System.String sdocid, System.Int32 ipagestart
, System.String scaseid
, System.DateTime dcreated
)
{
return queryable
.Where(s => s.Table2.Table3.Scaseid == scaseid)
.Where(s => s.Table2.Dcreated == dcreated)
.Where(s => s.Sdocid == sdocid
&& s.Ipagestart == ipagestart)
.FirstOrDefault();
}
Is there a way to have NHibernate add all the primary key fields as
direct properties on the objects instead of making them layered like
this? Just as an FYI I am using CodeSmith to generate my NHibernate
data layer to get the compiled objects.
Thanks
dbl
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.