Almost immediately after making this post I found an article
describing a similar, though not quite the same, situation:
http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/05/14/how-to-map-a-tree-in-nhibernate.aspx.
I was able to load the objects from the database with the following
mapping:
<set name="RelatedEntities" table="Entity">
<key column="CommonKey" property-ref="CommonKey" />
<one-to-many class="Entity"/>
</set>
The relationship really is a many-to-many relationship however this
set is only used to retrieve related entities not to manage their
relationship. The value of the CommonKey column will never be
changed. For how it is used, the one-to-many setup will suffice. If
there is a way to map the relationship as many-to-many it would be
helpful, though, as there are several relationships in the legacy
database I am mapping that have a similar setup to the one described
previously.
On Aug 3, 10:02 am, Aaron Alexander <[email protected]> wrote:
> I'm trying to map a table back to itself in a many-to-many
> collection. The table involved stores information about an entity
> across multiple environments. The entities are related by a common
> key which they share across environments. Basically, for every row in
> the Entity table there will be a unique pairing of EnvironmentId and
> CommonKey but there can be multiple entries with the same CommonKey.
> I want to setup a set that will get all Entities with the same common
> key.
>
> table Entity (
> EntityId uniqueidentifier not null,
> CommonKey nvarchar(36) not null,
> EnvironmentId uniqueidentifier not null
> )
>
> public class Entity
> {
> public virtual Guid Id { get; set; }
> public virtual string CommonKey { get; set; }
> public virtual Guid EnvironmentId { get; set; }
>
> public virtual ISet<Entity> RelatedEntities { get; set; }
>
> }
>
> <class name="Testing.Entity, Testing" table="Entity">
> <id column="EntityId" name="Id">
> <generator class="guid" />
> </id>
>
> <property name="CommonKey" />
> <property name="EnvironmentId" />
>
> <set name="RelatedEntities" table="Entity">
> <key column="SOMETHING" property-ref="SOMETHING" />
> <many-to-many column="SOMETHING" class="Testing.Entity,
> Testing" />
> </set>
> </class>
>
> The SQL I would use to get this is:
>
> select entity1.EntityId, entity1.CommonKey, entity1.EnvironmentId,
> entity2.EntityId, entity2.CommonKey, entity2.EnvironmentId
> from Entity entity1
> left outer join Entity entity2 on entity1.CommonKey =
> entity2.CommonKey
> where entity1.EntityId = ?
>
> It seems that property-ref should enable this but I cannot figure out
> the correct combination of columns and property names for the set
> mapping. I have tried this with NHibernate 2.0 and 2.1 with similar
> results. Is this a supported scenario with NHibernate? If so, how do
> I setup the mapping for the set?
>
> Thanks.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---