NHibernate version: 1.2.0
Hi,
i have a little problem with creating my mapping files. I have the following tables:
Application: ApplicationID,....
Relation: RelationID,...
Relationship: RelationshipID, App1, App2, Relation, Direction
So i have a many to many relationship from Application to itself. But i also need information about what type of relationship two apps have. Therefore the Join Table Relationship. My problem is, that i don't know how to create the mapping files according to that.
My first idea was:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Spider.Domain.Objects" assembly="Spider.Domain">
<class name="Relationship" table="Relationship" lazy="true">
<id name="ID" column="RelationshipID">
<generator class="native" />
</id>
<property name="Direction" column="Direction" />
<many-to-one name="Application1" class="Application" column="Application1ID" cascade="none" />
<many-to-one name="Application2" class="Application" column="Application2ID" cascade="none" />
<many-to-one name="Relationtype" class="Relation" column="RelationID" cascade="none" />
</class>
</hibernate-mapping>
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Spider.Domain.Objects" assembly="Spider.Domain">
<class name="Application" table="Application" lazy="true">
<id name="ID" column="ApplicationID">
<generator class="native" />
</id>
<bag name="Relationships" table="Relationship" inverse="true" cascade="none">
<key column="ApplicationID" />
<one-to-many class="Relationship" />
</bag>
</class>
</hibernate-mapping>
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Spider.Domain.Objects" assembly="Spider.Domain">
<class name="Relation" table="Relation" lazy="true">
<id name="ID" column="RelationID">
<generator class="native" />
</id>
<bag name="Relationships" table="Relationship" inverse="true" cascade="none">
<key column="RelationID" />
<one-to-many class="Relationship" />
</bag>
</class>
</hibernate-mapping>
But then i remembered that i don't have only one Application reference in the join table, but two. So it is obvious, that this won't work:
Code:
<bag name="Relationships" table="Relationship" inverse="true" cascade="none">
<key column="ApplicationID" />
<one-to-many class="Relationship" />
</bag>
But if i replace ApplicationID with Application1ID or Application2ID i don't get all the relationships. For example:
I have 3 apps: ftp, mail, http and 2 relationships: ftp - mail, http - ftp. When trying to retrieve all the relationships from ftp, i get either the ftp - mail (if key column = Application1ID) or the http - ftp (if key column = Application2ID). I could solve this if i just use two collections per application, but this would be rather ugly, i think.
Does anybody know a solution for this problem?
This Problem is similar to this one:
http://forum.hibernate.org/viewtopic.php?t=982714 But i don't think that solution would work or rather i don't understand the proposed solution.
Could someone give me a hint on how to solve this?
Kind regards junsas