NHibernate常用

来源:互联网 发布:linux下usb调试工具 编辑:程序博客网 时间:2024/05/21 20:22

默认实体类的hbm文件的主键

<id name="Id" column="Id" type="int" >
      <generator class="identity" />
    </id>

是identity 如果使用Guid则需要程序中赋值  如果使用Indentity时候 是数据库给赋值

所以Guid是不一样的

generator:主键生成策略。NHibernate提供了以下几种生成策略:

 

名称

说明

是否常用

increment

用于为int类型生成 唯一标识。只有在没有其他进程往同一张表中插入数据时才能使用

N

identity

对DB2,MySQL, MS SQL Server, Sybase和HypersonicSQL的内置标识字段提供支持。数据库返回的主键值 返回的标识符是int类型的。

 

N

sequence

在DB2,PostgreSQL, Oracle, SAP DB, McKoi中使用序列(sequence), 而在Interbase中使用生成器(generator)。返回的标识符是int类型的。

Y

seqhilo

使用一个高/低位算法来高效的生成int类型的标识符,给定一个数据库序列(sequence)的名字。

N

uuid.hex

用一个System.Guid的ToString()方法法生成字符串类型的标识符, 字符串的长度由format参数定义。

N

uuid.string

用一个新的System.Guid实例的byte[]转化为字符串作为标示符。

N

guid

使用新的System.Guid实例作为标示符。

 

guid.comb

使用Jimmy Nilsson的算法生成一个新的System.Guid标示符。

 

native

根据底层数据库的能力选择identity, sequence 或者hilo中的一个。

Y

assigned

让应用程序在 Save()之前为对象分配一个标示符。

Y

foreign

使用另外一个相关联的对象的标识符。通常和<one-to-one>联合起来使用。

Y

 联合主键配置

<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Domain" namespace="Domain">
  
<class name="Product" table="T_Product" lazy="true" >
    
    
<composite-id name="ID" class="ProductID">
      
<key-property name="Name" type="string">
        
<column name="Name" length="50"/>
      
</key-property>

      
<key-property name="QuantityPerUnit" type="string">
        
<column name="QuantityPerUnit" length="50"/>
      
</key-property>

    
</composite-id>

    
<property name="Unit" type="string">
      
<column name="Unit" length="50"/>
    
</property>


    
<property name="SellPrice" type="decimal">
      
<column name="SellPrice" precision="14" scale="2"/>
    
</property>

    
<property name="BuyPrice" type="decimal">
      
<column name="BuyPrice" precision="14" scale="2"/>
    
</property>

    
<property name="Remark" type="string">
      
<column name="Remark" length="200"/>
    
</property>

  
</class>
</hibernate-mapping>


实体类的生命周期


让你的实体类继承ILifecycle

则出现了几个事件 在这些事件中 可以处理一些事情


数据正确性验证IValidatable让实体类继承这个接口

0 0