Hi guys,
My goal is to represent & map the following scenario using NHibernate:
public class Person {
private int _id;
private string _name;
public int ID { get {return _id;} set {_id = value;} }
public string Name { get {return _name;} set {_name = value;} }
}
A table representing Person
CREATE TABLE Person(
Id int NOT NULL,
Name nvarchar(20) default NULL ) ;
A table representing a many to many relation between one Person to another:
CREATE TABLE PersonRelations(
PersonId1 int NOT NULL,
PersonId2 int NOT NULL,
TypeOfRelation nvarchar(20) NOT NULL default 'Friend') ;
======================================
Initially I added a Collection of Person to the Person class:
private IList<Person> _relatedPersons;
public IList<Person> RelatedPersons
{
get { return _relatedPersons; }
set { _relatedPersons = value; }
}
My mapping currently looks like this:
Code:
<class name="Person" table="Persons">
<id name="Id">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="Name" />
<bag name="RelatedPersons" table="personrelations">
<key column="PersonId1" />
<many-to-many class="Person" column="PersonId2" />
</bag>
</class>
This mapping Is ok if I ignore the TypeOfRelation mentioned earlier. It saves the relation in
personrelations table and deletes it when a person is deleted.
The problem is me not being able to answer the following question:
Who are John Doe's friends?
I basically need to load a person with a name "John Doe" and in some way define an expression on the relation table, thus getting in the
RelatedPersons collection only the persons that relate to him with "Friend" as the type of relation.
There is very little / almost no documentation on this subject.
After a very long search in the forums, Google & nhibernate blogs, I found only one thread that discusses the issue of mapping many-to-many relations of the same class. (
ttp://forum.hibernate.org/viewtopic.php?t=969967&start=0&postdays=0&postorder=asc&highlight=)
The above post only scrapes the surface of this subject, but not enough to answer my question.
Is there A way to do This?
Shaul