Reputation: 37526
Here are the mappings for my objects:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="NHibernateTest"
namespace="NHibernateTest.Database">
<class name="Employer">
<id name="Id" type="Int64" column="id" unsaved-value="0">
<generator class="identity"/>
</id>
<property name="Name"/>
</class>
</hibernate-mapping>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="NHibernateTest"
namespace="NHibernateTest.Database">
<class name="Employee">
<id name="Id" type="Int64" column="id" unsaved-value="0">
<generator class="identity"/>
</id>
<property name="FirstName"/>
<property name="LastName"/>
<property name="Email"/>
</class>
</hibernate-mapping>
Here are the objects themselves:
namespace NHibernateTest.Database {
public class Employee {
public virtual long Id { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual string Email { get; set; }
}
public class Employer {
public virtual long Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<Employee> Employees { get; set; }
}
}
I'm trying to use the SchemaExport utility to generate the schema, but cannot figure out how to write the mapping for the IList
. It's been a long time since I've touched Hibernate for Java and am finding the documentation on one-to-many for NHibernate to be a bit difficult to follow. If someone could write that mapping, I'd be much obliged.
Upvotes: 0
Views: 632
Reputation: 35117
I had to guess the name of the Employee table and the Employer.Id foreign key but here's how I would do it:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="NHibernateTest"
namespace="NHibernateTest.Database">
<class name="Employer">
<id name="Id" type="Int64" column="id" unsaved-value="0">
<generator class="identity"/>
</id>
<property name="Name"/>
<bag name="Employees" table="Employees" >
<key column="EmployerId"></key>
<one-to-many class="Employee"></one-to-many>
</bag>
</class>
</hibernate-mapping>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="NHibernateTest"
namespace="NHibernateTest.Database">
<class name="Employee">
<id name="Id" type="Int64" column="id" unsaved-value="0">
<generator class="identity"/>
</id>
<property name="EmployerId" />
<property name="FirstName"/>
<property name="LastName"/>
<property name="Email"/>
</class>
</hibernate-mapping>
Here's the .net code:
namespace NHibernateTest.Database {
public class Employee {
public Employee(Employer employer) { EmployerId = employer.Id; }
protected Employee() {} // nHibernate needs access to a parameterless constructor.
public virtual long Id { get; set; }
protected virtual long EmployerId { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual string Email { get; set; }
}
public class Employer {
public virtual long Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<Employee> Employees { get; set; }
}
}
Upvotes: 1