0

我处于这种情况:

Table A
id
name
country

和国家表

Table Country
country
name

字段国家是 2 位数的 ISO(US、PA、UY 等)。表 A 中的字段 country 引用表 Country 中的字段 iso。我在(A 类和 Country 类)nHibernate 上创建了类,因为我没有我设置的所有国家not-found="ignore"

Class A
string name;
Country country;

Class Country
string iso;
string name;

如果我想获得 ISO 代码,我将使用 y 类的 .net 属性A.country.iso。现在,如果我有一个不在表 Country 中的 ISO 代码,A.country.iso 将给出异常,因为 country 对象为空。但是表 A 的国家字段不为空,我想获取它们的值。
我怎样才能做到这一点?

4

1 回答 1

0

A只需向直接映射到 ISO 代码列的类添加一个附加字符串属性作为属性。然后,如果该country属性为空,则可以使用该属性。我还将它标记为只读(更新和插入错误),否则可能会使A类的用户混淆他应该使用哪个属性。

示例代码:

public class A
{
    private Country _country;

    public virtual string CountryIsoCode { get; private set; }

    public virtual Country Country
    {
        get
        {
            return _country;
        }
        set
        {
            _country = value;
            CountryIsoCode = value != null ? value.IsoCode : null;
        }
    }

映射:

<property name="CountryIsoCode" column="country" insert="false" update="false" />
于 2013-07-19T05:21:51.367 回答