使用NHibernate.Mapping.Attributes.dll,我们可以像ActiveRecord一样来Mapping 对应的Table, Column
先来个例子:
DataModel.Order
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Mapping.Attributes;
namespace DataModel
{
/// <summary>
/// Order data model class
/// </summary>
[Class(Table = "Orders",Name = "Order", Lazy = false, NameType = typeof(DataModel.Order))]
public class Order
{
private int _orderId;
private DateTime _orderDate;
private int _customer;
[Id(1, Column="OrderId", Name="OrderId", TypeType=typeof(Int32))]
[Generator(2, Class=HibernateStrings.Generator.Native)]
public int OrderId
{
get { return _orderId; }
set { _orderId = value; }
}
[Property(Column="OrderDate")]
public DateTime OrderDate
{
get { return _orderDate; }
set { _orderDate = value; }
}
//[Property(Column="Customer")]
//public int CustomerId
//{
// get { return _customer; }
// set { _customer = value; }
//}
[ManyToOne(Name="Customer", Cascade=CascadeStyle.None, Column="Customer",ClassType=typeof(DataModel.Customer))]
public Customer Customer
{ get; set; }
[Bag(Name = "Products", Lazy = false, Generic=true,
Table="OrderProduct", Cascade = CascadeStyle.AllDeleteOrphan)]
[Key(1, Column = "[Order]")]
[ManyToMany(2, Column="Product", ClassType = typeof(DataModel.Product))]
private IList<Product> _products = new List<Product>();
public IList<Product> Products
{
get { return _products; }
set { _products = value; }
}
public override string ToString()
{
return String.Format("[Order] OrderId={0}, OrderDate={1}, CustomerId={2}, CustomerName={3}",
OrderId, OrderDate, Customer.CustomerId, Customer.FullName);
}
}
}
不用再写烦人的hbm.xml文件了.
不过要注意以下几点:
(1)ID必须要写Name, TypeType
(2)Class也必须要写Name, NameType等Attribute
(3)Session.Flush()根据ID策略不同,而不同. Commit时自动Flush(), 以发送SQL到DB. 有时不Flush,不会真正更新DB.
Load VS. Get
(1)当记录不存在时候,get方法返回null,load方法产生异常
(2)load方法可以返回实体的代理类,get方法则返回真是的实体类
(3)load方法可以充分利用hibernate的内部缓存和二级缓存中的现有数据,而get方法仅仅在内部缓存中进行数据查找,如果没有发现数据則将越过二级缓存,直接调用SQL查询数据库。
(4) 也许别人把数据库中的数据修改了,load如何在缓存中找到了数据,则不会再访问数据库,而get则会返回最新数据。