milo yu

博客园 首页 新随笔 联系 订阅 管理

看了官方的doc,如下:

<one-to-one        name="PropertyName"                                
(1)        class="ClassName"                                  
(2)        cascade="all|none|save-update|delete"              
(3)        constrained="true|false"                          
 (4)        fetch="join|select"                               
 (5)        property-ref="PropertyNameFromAssociatedClass"    
 (6)        access="field|property|nosetter|ClassName"         
(7)/>
(1)

name: The name of the property.

(2)

class (optional - defaults to the property type determined by reflection): The name of the associated class.

(3)

cascade (optional) specifies which operations should be cascaded from the parent object to the associated object.

(4)

constrained (optional) specifies that a foreign key constraint on the primary key of the mapped table references the table of the associated class. This option affects the order in which Save() and Delete() are cascaded (and is also used by the schema export tool).

(5)

fetch (optional - defaults to select): Chooses between outer-join fetching or sequential select fetching.

(6)

property-ref: (optional) The name of a property of the associated class that is joined to the primary key of this class. If not specified, the primary key of the associated class is used.

(7)

access (optional - defaults to property): The strategy NHibernate should use for accessing the property value.

看了以后 有点模糊,后来 google了一下,发现有位老兄已经给出了完整示例,在这里我就给出link了

http://www.cnblogs.com/renrenqq/archive/2006/08/16/478198.html 

 

终于找到一个完整示例,放给大家。

 two tables

Documents
-------------------
(PK) Id
(FK Unique) DocumentFileId
Name

 

DocumentFiles
-------------------
(PK) Id
Data

 

POCO类

public class Document {
    public int Id { get; set; }
    public string Name { get; set; }
    public DocumentFile DocumentFile { get; set; }   
}

 public class DocumentFile {
    public virtual int Id { get; set; }
    public virtual Document Document { get; set; }
    public virtual byte[] Data { get; set; }    
}

 

配置文件。

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="NHibernateDocumentTest.Document, NHibernateDocumentTest" table="Documents" lazy="false">
    <id name="Id" column="Id" type="integer">
      <generator class="native" />
    </id>
    <property name="Name" column="Name" type="string" />
    <many-to-one name="DocumentFile" cascade="all-delete-orphan" 
                 lazy="proxy" column="DocumentFileId" unique="true" />

  </class>
</hibernate-mapping>

 

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="NHibernateDocumentTest.DocumentFile, NHibernateDocumentTest" table="DocumentFiles" lazy="true">
    <id name="Id" column="Id" type="integer">
      <generator class="native" />
    </id>   

    <property name="Data" column="Data" type="Byte[]" />
    <one-to-one name="Document" constrained="true" property-ref="DocumentFile" />
  </class>
</hibernate-mapping>

 

终于搞定。

 

 

 

posted on 2010-09-27 22:51  milo yu  阅读(510)  评论(0编辑  收藏  举报