Well, I thought I was getting it, but I have a few more questions.
First, I can get the Header reference to map correctly for one of the
two Version pointers, but not both. It is very important that the
Header pointer for all versions point back to the same Header object,
i.e.,
Header header = ReadHeader();
// make sure all members of header are initialized
// all of the following should be true
object.ReferenceEquals(header, header.LatestApprovedVersion.Header);
object.ReferenceEquals(header, header.LatestVersion.Header);
object.ReferenceEquals(header, header.AllVersions[some_index].Header);
Ideally, it would also be true that
object.ReferenceEquals(header.LatestApprovedVersion,
GetLatestApprovedVersion(header.AllVersions));
object.ReferenceEquals(header.LatestVersion,
GetLatestVersion(header.AllVersions));
so that any modifications made to header.LatestVersion will be
reflected by the pointer in the AllVersions collection. This is less
important since the AllVersions collection will probably not be
mutable. All modifications will be made through the LatestVersion
reference due to workflow rules.
Secondly, in Hival's suggestion, it looks like the Version entity is
being mapped through two <class> elements. I am concerned about
having to create two mapping files for a single entity. What happens
when things are inconsistent between them (the id, generator, or a
property)? The schema for the mapping XML requires the id element to
be present, so I can't omit it in one of them. Is this even supported
by NHibernate? I can keep them in a single file, but this pattern
would have to be followed for a fairly large number of entities, so
even the smallest fudging of best practices would be magnified (if
this was indeed a fudging).
I tried to use a mapping file similar to this (which is based on
Hival's suggestion):
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
schema="Entities"
assembly="Entities" namespace="Entities">
<class name="Version" table="Version">
<id name="VersionID">
<generator class="identity"/>
</id>
<one-to-one name="Header" property-ref="LatestApprovedVersion">
</one-to-one>
<one-to-one name="Header" property-ref="LatestVersion">
</one-to-one>
<!-- other members mapped here -->
</class>
</hibernate-mapping>
but that (correctly) fails because the Header property is mapped more
than once. Is there a way to map this pattern in NHibernate (given
the same database schema). I read
http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/05/14/how-to-map-a-tree-in-nhibernate.aspx,
but the use case there does no involve a double circular reference
(only one pointer back to the parent Equipment and one child
collection). I feel like the best mapping would map the version
references from the Header mapping file, not from the Version mapping
file.
Thanks again for you help.
On Feb 20, 3:34 pm, Chris B <[email protected]> wrote:
> Ah....I think I see where I was going wrong. I expected the
> relationship between the Header -> LatestApprovedVersion to be a one-
> to-one.
>
> Thanks a lot!
>
> On Feb 20, 12:56 pm, hival <[email protected]> wrote:
>
> > I'm not sure whether it is the optimal solution but it works
>
> > <?xml version="1.0" encoding="utf-8" ?>
> > <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
> > assembly="NHTest" namespace="NHTest.Model" auto-import="true">
>
> > <class name="Header" table="Header">
> > <id name="HeaderId" column="HeaderId">
> > <generator class="native" />
> > </id>
>
> > <many-to-one name="LatestVersion"
> > entity-name="LatestVersion"
> > column="LatestVersionId"
> > cascade="save-update" not-null="true" /
>
> > <many-to-one name="LatestApprovedVersion" entity-
> > name="LatestApprovedVersion"
> > column="LatestApprovedVersionId"
> > cascade="save-update" not-
> > null="true" />
> > </class>
>
> > <class name="Version" entity-name="LatestVersion" table="Version">
> > <id name="VersionId" column="VersionId">
> > <generator class="native" />
> > </id>
>
> > <one-to-one name="Header" property-ref="LatestVersion"
> > constrained="true" />
> > </class>
>
> > <class name="Version" entity-name="LatestApprovedVersion"
> > table="Version">
> > <id name="VersionId" column="VersionId">
> > <generator class="native" />
> > </id>
>
> > <one-to-one name="Header"
> > property-ref="LatestApprovedVersion"
> > constrained="true" />
> > </class>
> > </hibernate-mapping>
>
> > Use constrained="true" only if the respective foreign key in the
> > Header table doesn't allow null values.
> > In this case NH will not use outer joins when loading Version
> > association.
--
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.