Wednesday, February 10, 2010

NHibernate – Define Order and Product entities and mappings (Part 10)


In the previous posts on NHibernate we have seen how to configure NHibernate and create a repository pattern implementation for accessing data using NHibernate. In this post we will see how to create different levels of mappings between our entities that participate in a customer-order application. For this we need to create two more entities (Product and Order) and define mappings between them.
The classes that are used in this sample are defined as
public class Product  : BaseEntity<long>
{
    public virtual string Name { get; set; }
    public virtual decimal Amount { get; set; }
    public virtual string ModelNumber { get; set; }       
}
public class Order : BaseEntity<long>
{
    public virtual string OrderNumber { get; set; }
    public virtual Customer Customer { get; set; }
    public virtual ISet<Product> Products { get; set; }
}
public class Customer : BaseEntity<long>
{
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual string Address { get; set; }
    public virtual ISet<Order> Orders { get; set; }
}
I have defined the mapping between entities as
Customer.hbm.xml file
xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="BlogsPrajeesh.NHibernate.Domain"
                   namespace="BlogsPrajeesh.NHibernate.Domain">
  <class name="Customer" table="Customers">
    <id name="Id" column="CustomerIntId">
      <generator class="identity" />
    id>
   
   
    <version name="Version" generated="always" unsaved-value="null" type="BinaryBlob">
      <column name="Version" not-null="false" sql-type="timestamp"/>
    version>
    <property name="CreatedDate" type="DateTime" />
    <property name="ChangedDate" type="DateTime" />
    <property name="FirstName" not-null="true" />
    <property name="LastName" not-null="true" />
    <property name="Address" />
   
    <set name="Orders" inverse="true" lazy="true" cascade="delete">
      <key column="CustomerIntId"/>
      <one-to-many class="Order"/>
    set>
  class>
hibernate-mapping>
Order.hbm.xml file
xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="BlogsPrajeesh.NHibernate.Domain"
                   namespace="BlogsPrajeesh.NHibernate.Domain">
  <class name="Order" table="Orders">
    <id name="Id" column="OrderIntId">
      <generator class="identity" />
    id>
   
   
    <version name="Version" generated="always" unsaved-value="null" type="BinaryBlob">
      <column name="Version" not-null="false" sql-type="timestamp"/>
    version>
    <property name="CreatedDate" type="DateTime" />
    <property name="ChangedDate" type="DateTime" />
    <property name="OrderNumber" not-null="true" />
   
    <many-to-one name="Customer" column="CustomerIntId"/>
    <set name="Products" table="OrderProduct">
      <key column="OrderIntId"/>
      <many-to-many class="Product" column="ProductIntId"/>
    set>
  class>
hibernate-mapping>
Product.hbm.xml file
xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="BlogsPrajeesh.NHibernate.Domain"
                   namespace="BlogsPrajeesh.NHibernate.Domain">
  <class name="Product" table="Products">
    <id name="Id" column="ProductIntId">
      <generator class="identity" />
    id>
    <version name="Version" generated="always" unsaved-value="null" type="BinaryBlob">
      <column name="Version" not-null="false" sql-type="timestamp"/>
    version>
    <property name="CreatedDate" type="DateTime" />
    <property name="ChangedDate" type="DateTime" />
    <property name="Name" not-null="true" />
    <property name="Amount" />
    <property name="ModelNumber" column="Model" not-null="true" />
  class>
hibernate-mapping>
Next part of this series we’ll see how to create repositories for the Order and Product entities and use them for accessing the data.

No comments: