NHibernate复合键
我已经创建了一个复合键,它正在工作,但是我希望在row类中直接使用单独的字段 我目前的做法如下:NHibernate复合键,nhibernate,composite-key,Nhibernate,Composite Key,我已经创建了一个复合键,它正在工作,但是我希望在row类中直接使用单独的字段 我目前的做法如下: private UserPrimaryKey _compositeKey; public virtual UserPrimaryKey CompositeKey { get { if (_compositeKey == null) _compositeKey = new UserPrimaryKey();
private UserPrimaryKey _compositeKey;
public virtual UserPrimaryKey CompositeKey
{
get
{
if (_compositeKey == null) _compositeKey = new UserPrimaryKey();
return _compositeKey;
}
set {
if (_compositeKey == value) return;
_compositeKey = value;
Host = value.Host;
UserAccount = value.User;
}
}
public string Host { get; set; }
public string UserAccount { get; set; }
private UserPrimaryKey _compositeKey;
public virtual UserPrimaryKey CompositeKey
{
get
{
if (_compositeKey == null) _compositeKey = new UserPrimaryKey();
return _compositeKey;
}
set {
if (_compositeKey == value) return;
_compositeKey = value;
Host = value.Host;
UserAccount = value.User;
}
}
public string Host
{
get
{
return CompositeKey.Host;
}
set
{
CompositeKey.Host = value;
}
}
public string UserAccount
{
get
{
return CompositeKey.User;
}
set
{
CompositeKey.User = value;
}
}
我想知道有没有更好的方法?可能在NHibernate配置文件中
我当前的配置文件如下所示:
<class name="TGS.MySQL.DataBaseObjects.DataBasePrivilege,TGS.MySQL.DataBaseObjects" table="user">
<composite-id name="CompositeKey" class="TGS.MySQL.DataBaseObjects.UserPrimaryKey, TGS.MySQL.DataBaseObjects">
<key-property name="Host" column="Host" type="string" length="60" />
<key-property name="User" column="User" type="string" length="16" />
</composite-id>
</class>
我建议如下:
private UserPrimaryKey _compositeKey;
public virtual UserPrimaryKey CompositeKey
{
get
{
if (_compositeKey == null) _compositeKey = new UserPrimaryKey();
return _compositeKey;
}
set {
if (_compositeKey == value) return;
_compositeKey = value;
Host = value.Host;
UserAccount = value.User;
}
}
public string Host { get; set; }
public string UserAccount { get; set; }
private UserPrimaryKey _compositeKey;
public virtual UserPrimaryKey CompositeKey
{
get
{
if (_compositeKey == null) _compositeKey = new UserPrimaryKey();
return _compositeKey;
}
set {
if (_compositeKey == value) return;
_compositeKey = value;
Host = value.Host;
UserAccount = value.User;
}
}
public string Host
{
get
{
return CompositeKey.Host;
}
set
{
CompositeKey.Host = value;
}
}
public string UserAccount
{
get
{
return CompositeKey.User;
}
set
{
CompositeKey.User = value;
}
}
这样就不会复制数据,只返回/设置复合键内的数据。我建议如下:
private UserPrimaryKey _compositeKey;
public virtual UserPrimaryKey CompositeKey
{
get
{
if (_compositeKey == null) _compositeKey = new UserPrimaryKey();
return _compositeKey;
}
set {
if (_compositeKey == value) return;
_compositeKey = value;
Host = value.Host;
UserAccount = value.User;
}
}
public string Host { get; set; }
public string UserAccount { get; set; }
private UserPrimaryKey _compositeKey;
public virtual UserPrimaryKey CompositeKey
{
get
{
if (_compositeKey == null) _compositeKey = new UserPrimaryKey();
return _compositeKey;
}
set {
if (_compositeKey == value) return;
_compositeKey = value;
Host = value.Host;
UserAccount = value.User;
}
}
public string Host
{
get
{
return CompositeKey.Host;
}
set
{
CompositeKey.Host = value;
}
}
public string UserAccount
{
get
{
return CompositeKey.User;
}
set
{
CompositeKey.User = value;
}
}
这样,您就不会复制数据,只返回/设置复合键内的数据。我会尽量避免使用复合键。将其替换为两列上的常规唯一约束:
<class name="DataBasePrivilege" table="user">
<id name="id">
<generator class="hilo">
<param name="table">user_HiLo</param>
<param name="max_lo">100</param>
</generator>
</id>
<property name="Host" length="60" unique-key="user_host"/>
<property name="User" length="16" unique-key="user_host"/>
</class>
用户希洛
100
(顺便说一句:在常见情况下,您不需要指定类型,如果列名与属性名匹配,您也不需要指定列名。这使您可以读取xml)我会尽量避免使用复合键。将其替换为两列上的常规唯一约束:
<class name="DataBasePrivilege" table="user">
<id name="id">
<generator class="hilo">
<param name="table">user_HiLo</param>
<param name="max_lo">100</param>
</generator>
</id>
<property name="Host" length="60" unique-key="user_host"/>
<property name="User" length="16" unique-key="user_host"/>
</class>
用户希洛
100
(顺便说一句:在常见情况下,您不需要指定类型,如果列名与属性名匹配,您也不需要指定列名。这可以让您的xml可读)您可以直接在类中创建属性。。。并将其映射为:
<composite-id>
<key-property name="Host"/>
<key-property name="UserAccount"/>
</composite-id>
如果这样做,则必须在类中重写
Equals
和GetHashCode
。您可以直接在类中创建属性。。。并将其映射为:
<composite-id>
<key-property name="Host"/>
<key-property name="UserAccount"/>
</composite-id>
如果这样做,您将不得不重写类中的
Equals
和GetHashCode
。不幸的是,该表是MySQL本机表(用于保存MySQL数据库用户的记录),因此我无法更改模式。在不更改模式的情况下,这仍然有效吗?不幸的是,该表是MySQL本机表(用于保存MySQL数据库用户的记录),因此我无法更改模式。在不改变模式的情况下,这仍然有效吗?