一、典型设置
cascade:(默认为none)级联。指明哪些操作会从对象级联到关联的对象。
inverse: (默认为false) 标记这个集合作为双向关联关系中的方向一端。在双向关联时才需要设置。在设为false的一端对cascade进行维护。处于性能的考虑,一般在数据少的一端或者被依赖端设置inverse="true",而让数据多的一段维护cascade。
1.one-to-one
1.1 数据库表结构
其中T_Person为主表,T_Employee为子表。T_Employee的PersonId参照T_Peson的PersonId。
1.2 示例映射类文件
/**//*
/*作者:DDL
/*联系:http://renrenqq.cnblogs.com/
*/
using System;
namespace DDLLY.TestNHibernate.TestAssociation.OneToOne
{
/**//// <summary>
///
/// </summary>
[Serializable]
public class Person
{
Private Members#region Private Members
private bool _isChanged;
private bool _isDeleted;
private int _personid;
private string _name;
private Employee _employee;
#endregion
Default ( Empty ) Class Constuctor#region Default ( Empty ) Class Constuctor
/**//// <summary>
/// default constructor
/// </summary>
public Person()
{
_personid = 0;
_name = null;
_employee = null;
}
#endregion // End of Default ( Empty ) Class Constuctor
Public Properties#region Public Properties
/**//// <summary>
///
/// </summary>
public virtual int PersonId
{
get { return _personid; }
set
{
_isChanged |= (_personid != value);
_personid = value;
}
}
/**//// <summary>
///
/// </summary>
public virtual string Name
{
get { return _name; }
set
{
if (value != null)
if (value.Length > 64)
throw new ArgumentOutOfRangeException("Invalid value for Name", value, value.ToString());
_isChanged |= (_name != value);
_name = value;
}
}
/**//// <summary>
///
/// </summary>
public Employee Employee
{
get { return _employee; }
set { _employee = value; }
}
/**//// <summary>
/// Returns whether or not the object has changed it's values.
/// </summary>
public bool IsChanged
{
get { return _isChanged; }
}
/**//// <summary>
/// Returns whether or not the object has changed it's values.
/// </summary>
public bool IsDeleted
{
get { return _isDeleted; }
}
#endregion
Public Functions#region Public Functions
/**//// <summary>
/// mark the item as deleted
/// </summary>
public void MarkAsDeleted()
{
_isDeleted = true;
_isChanged = true;
}
#endregion
Equals And HashCode Overrides#region Equals And HashCode Overrides
/**//// <summary>
/// local implementation of Equals based on unique value members
/// </summary>
public override bool Equals(object obj)
{
if (this == obj) return true;
if ((obj == null) || (obj.GetType() != GetType())) return false;
Person castObj = (Person) obj;
return (castObj != null) &&
(_personid == castObj.PersonId);
}
/**//// <summary>
/// local implementation of GetHashCode based on unique value members
/// </summary>
public override int GetHashCode()
/*作者:DDL
/*联系:http://renrenqq.cnblogs.com/
*/
using System;
namespace DDLLY.TestNHibernate.TestAssociation.OneToOne
{
/**//// <summary>
///
/// </summary>
[Serializable]
public class Person
{
Private Members#region Private Members
private bool _isChanged;
private bool _isDeleted;
private int _personid;
private string _name;
private Employee _employee;
#endregion
Default ( Empty ) Class Constuctor#region Default ( Empty ) Class Constuctor
/**//// <summary>
/// default constructor
/// </summary>
public Person()
{
_personid = 0;
_name = null;
_employee = null;
}
#endregion // End of Default ( Empty ) Class Constuctor
Public Properties#region Public Properties
/**//// <summary>
///
/// </summary>
public virtual int PersonId
{
get { return _personid; }
set
{
_isChanged |= (_personid != value);
_personid = value;
}
}
/**//// <summary>
///
/// </summary>
public virtual string Name
{
get { return _name; }
set
{
if (value != null)
if (value.Length > 64)
throw new ArgumentOutOfRangeException("Invalid value for Name", value, value.ToString());
_isChanged |= (_name != value);
_name = value;
}
}
/**//// <summary>
///
/// </summary>
public Employee Employee
{
get { return _employee; }
set { _employee = value; }
}
/**//// <summary>
/// Returns whether or not the object has changed it's values.
/// </summary>
public bool IsChanged
{
get { return _isChanged; }
}
/**//// <summary>
/// Returns whether or not the object has changed it's values.
/// </summary>
public bool IsDeleted
{
get { return _isDeleted; }
}
#endregion
Public Functions#region Public Functions
/**//// <summary>
/// mark the item as deleted
/// </summary>
public void MarkAsDeleted()
{
_isDeleted = true;
_isChanged = true;
}
#endregion
Equals And HashCode Overrides#region Equals And HashCode Overrides
/**//// <summary>
/// local implementation of Equals based on unique value members
/// </summary>
public override bool Equals(object obj)
{
if (this == obj) return true;
if ((obj == null) || (obj.GetType() != GetType())) return false;
Person castObj = (Person) obj;
return (castObj != null) &&
(_personid == castObj.PersonId);
}
/**//// <summary>
/// local implementation of GetHashCode based on unique value members
/// </summary>
public override int GetHashCode()