NHibernate使用小记

   这几天公司准备用NHibernate作为基础框架,上网搜了搜,NHibernate官网提供了几个版本,最基础的是NHibernate, 然后NHibernate.Burrow是对前者做了一些封装,NHibernate.Burrow.AppBlock则进一步进行了封装,看了下,确实很方便,于是自己动手写点玩 意。

      帮助文档都是E文版,所有又好用又不要钱的玩意好像都是外国人搞出来的,好在还不难懂。官网下了个例子,结果中了一个招。请看帮助文 档:

 

Now consider a model of the relationships between Customer, Order and LineItem and Product. There is a one-to-many association between Customer and Order, but how should we represent Order / LineItem / Product? I've chosen to map LineItem as an association class representing the many-to-many association between Order and Product. In NHibernate, this is called a composite element.


<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
    assembly
="" namespace="">

    
<class name="Customer" table="customers">
        
<id name="Id" column="id">
            
<generator class="native"/>
        
</id>
        
<property name="Name" column="name"/>
        
<set name="Orders" inverse="true" lazy="true">
            
<key column="customer_id"/>
            
<one-to-many class="Order"/>
        
</set>
    
</class>

    
<class name="Order" table="orders">
        
<id name="Id" column="id">
            
<generator class="native"/>
        
</id>
        
<property name="Date" column="date"/>
        
<many-to-one name="Customer" column="customer_id"/>
        
<list name="LineItems" table="line_items" lazy="true">
            
<key column="order_id"/>
            
<index column="line_number"/>
            
<composite-element class="LineItem">
                
<property name="Quantity" column="quantity"/>
                
<many-to-one name="Product" column="product_id"/>
            
</composite-element>
        
</list>
    
</class>

    
<class name="Product" table="products">
        
<id name="Id" column="id">
            
<generator class="native"/>
        
</id>
        
<property name="SerialNumber" column="serial_number" />
    
</class>

</hibernate-mapping>
      请看子表名:Order
官网的例子有个Create_SQL的建表语句,如果你按照这个例子做下 去,搞一对多的测试,会发现无论如何都会报错,
嘿嘿,想到原因没?老夫我找了好久才搞明白,浪费了大量脑细胞啊。
      NHibernate中用代码工具生成的实体类必须要将XML映射文件选成嵌入的资源,才可以正常使用。
posted @ 2010-06-08 10:57  小小向  阅读(432)  评论(0)    收藏  举报