NHibernate: best way to deal with staging table using Fluent NHibernate?

How would you display the following in Fluent NHibernate?

See "18.3 Customer / Order / Product"

http://www.hibernate.org/hib_docs/nhibernate/html/example-mappings.html

0


source to share


1 answer


The following solution takes the same approach as the solution in the example, and the generated XML is as good as the same. I've omitted specifying column names and things like that for brevity.

Domain:

public class Customer
{
    private ISet<Order> orders = new HashedSet<Order>();

    public long Id { get; set; }
    public string Name { get; set; }
    public ISet<Order> Orders
    {
        get { return orders; }
        private set { orders = value; }
    }
}

public class Order
{
    public long Id { get; set; }
    public DateTime Date { get; set; }
    public Customer Customer { get; set; }
    public IList<LineItem> LineItems { get; private set; }
}

public class LineItem
{
    public int Quantity { get; set; }
    public Product Product { get; set; }
}

public class Product
{
    public long Id { get; set; }
    public string SerialNumber { get; set; }
}

      

Mapping:

public class CustomerMap : ClassMap<Customer>
{
    public CustomerMap()
    {
        Id(x => x.Id)
            .GeneratedBy.Native();

        Map(x => x.Name);

        HasMany<Order>(x => x.Orders)
            .IsInverse()
            .AsSet();
    }
}

public class OrderMap : ClassMap<Order>
{
    public OrderMap()
    {
        Id(x => x.Id)
            .GeneratedBy.Native();

        Map(x => x.Date);

        References<Customer>(x => x.Customer);
        HasMany<LineItem>(x => x.LineItems)
            .Component(c =>
                {
                    c.Map(x => x.Quantity);
                    c.References<Product>(x => x.Product);
                }).AsList();
    }
}

public class ProductMap : ClassMap<Product>
{
    public ProductMap()
    {
        Id(x => x.Id)
            .GeneratedBy.Native();

        Map(x => x.SerialNumber);
    }
}

      



To see the generated XML mapping, you can use this code:

        Configuration config = new Configuration().Configure();
        PersistenceModel model = new PersistenceModel();

        model.addMappingsFromAssembly(typeof(CustomerMap).Assembly);
        model.Configure(config);

        model.WriteMappingsTo("your folder name here"); 

      

Hope this helps.

/ Erik

+6


source







All Articles