首页 新闻 会员 周边

NHibernate中的枚举类型

0
悬赏园豆:10 [已解决问题] 解决于 2014-03-26 10:37

单元测试时,提示无法确定 字段 SexType

failed: NHibernate.MappingException : Could not determine type for: NHibernate.Type.EnumStringType`1[[PersonRole.Basis.Enumeration.SexType, PersonRole.Basis]], NHibernate, for columns: NHibernate.Mapping.Column([SexType])

枚举定义如下:

 public enum SexType
    {
        [EnumDescription("保密")]
        Unknow = 2,
        [EnumDescription("男性")]
        Male = 1,
        [EnumDescription("女性")]
        Female=0
    }

实体类定义如下:
    public class t_UserInfo


        private SexType _sexType= SexType.Unknow;

public virtual SexType SexType
        {
            get { return this._sexType; }
            set { this._sexType = value; }
        }

表t_UserInfo映射如下:
    <property name="SexType" column="[SexType]" type="NHibernate.Type.EnumStringType`1[[PersonRole.Basis.Enumeration.SexType, PersonRole.Basis]], NHibernate" not-null="false" />

 

其实数据库表t_UserInfo中的列名确实是 SexType

当前使用的NHibernate版本为2.1,我想这个版本肯定会支持枚举了,可是哪里写错了?

king2003的主页 king2003 | 初学一级 | 园豆:161
提问于:2014-03-25 18:57
< >
分享
最佳答案
0
<property name="SexType">
  <column name="SexType" sql-type="int"/>
</property>

最简单的数据库映射为int类型

收获园豆:10
李永京 | 老鸟四级 |园豆:3114 | 2014-03-25 19:17

嗯,按这种做法,测试通过。

king2003 | 园豆:161 (初学一级) | 2014-03-26 10:37

请问有一张类别表t_Category,假设实体类如下:

public class t_SysDepartment

{
        private Iesi.Collections.Generic.ISet<t_SysDepartment> childDepartments = null;


        public virtual t_SysDepartment ParentDepartment { get; set; }
        public virtual Iesi.Collections.Generic.ISet<t_SysDepartment> ChildDepartments
        {
            get
            {
                if (childDepartments == null)
                    childDepartments = new Iesi.Collections.Generic.HashedSet<t_SysDepartment>();

                return childDepartments;
            }
            set { childDepartments = value; }
        }

        public virtual int AddDepartments(t_SysDepartment department)
        {
            ChildDepartments.Add(department);
            department.ParentDepartment = this;

            return ChildDepartments.Count;
        }

 

}

 

XML映射如下:

<many-to-one name="ParentDepartment" column="[ParentId]" class="PersonRole.Data.DomainModel.Entities.t_SysDepartment,PersonRole.Data.DomainModel" foreign-key="PK_Department" cascade="save-update" />
    <set name="ChildDepartments" table="t_SysDepartment" generic="true" inverse="true" lazy="true">
      <key column="[ParentId]" />
      <one-to-many class="PersonRole.Data.DomainModel.Entities.t_SysDepartment,PersonRole.Data.DomainModel" />
    </set>

 

当查询某类的所有子类时,

list = s.CreateQuery("from t_SysDepartment t where t.ParentId=:parentId")
                    .SetInt32("parentId", parentId)
                    .List<t_SysDepartment>();

list = s.CreateCriteria(typeof(t_SysDepartment))
                    .CreateCriteria("ChildDepartments")
                    .Add(Restrictions.Eq("ParentId", parentId))
                    .List<t_SysDepartment>();

都是无法识别 字段ParentId

failed: NHibernate.QueryException : could not resolve property: ParentId of: PersonRole.Data.DomainModel.Entities.t_SysDepartment [from PersonRole.Data.DomainModel.Entities.t_SysDepartment t where t.ParentDepartment.ParentId=:parentId]

其实我是知道这样写会出错,因为实体类中本来就没有字段ParentId

但想查询一张表中自身下的所有子类,该如何写呢?

king2003 | 园豆:161 (初学一级) | 2014-03-26 13:26

@king2003: http://www.jtben.com/document/1250 看看这篇文章

李永京 | 园豆:3114 (老鸟四级) | 2014-03-26 13:47
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册