Hi, I'm new to nHibernate although I do have several years of .Net, SQL & ORMs up my sleave. Its controlling the nHibernate mappings that I think I'm having trouble with! (Due to restrictions I can't quote actual code here.. sorry!)
I have two pretty standard tables, e.g. Parent & Child
Code:
[tParent]
ParentId int identity, pk
Name
[tChild]
ChildId int, identity, pk
ParentId int, foreign key to Parent
Name
These map to objects in C#, however note the lack of a container object in the Parent:
Code:
public class Parent
{
private int m_id;
private string m_parentName;
/* Set / Get properties Id & ParentName edited out for brevity */
}
public class Child
{
private int m_id;
private int m_parentId;
private string m_childName;
/* ... properties */
}
Now, they are associated (and a foreign key constraint appears in the db), but not in the objects themselves. The mappings I currently have are:
Code:
<class name="Parent" table="tParent>
<id name="Id" column="ParentId"> <generator class="identity"/> </id>
<property name="ParentName" column="name" />
</class>
<class name="Child" table="tChild>
<id name="Id" column="ChildId"> <generator class="identity"/> </id>
<property name="ParentId" column="ParentId" />
<property name="ChildName" column="name" />
</class>
Now, what I want to be able to do is retrieve Parent objects where the child objects meet certain critria, using the nHibernate Criteria objects (or in fact, vice versa - Child objects of a certain parent)
for e.g., retrieve all parents who have a child named 'george'
Code:
Criteria parentSearch = session.CreateCriteria(typeof(Parent));
// this won't work... but what will?
Criteria childSearch = parentSearch.CreateCriteria(typeof(Child));
childCritiera.Add(new EqExpression("ChildName", "george"));
List returnable = parentSearch.List()
The real question is, how can create the Parent.hbm.xml and the Child.hbm.xml mappings so that nHibernate can work out the relationships, but without having the container in the Parent class? All the documentation I've read and seen on the web assumes that the Parent class has an IList (or somesuch) in which to store the Child objects. Admittedly, in that case it becomes an awful lot easier. However, I'm nable to change those classes.
Any ideas?
(yes, I know it's all psuedo code, but its all I can let out, unfortunately. I'll try to make a test project to post, but it will take some time.)
Thanks to all for your help.
Phil