首页 新闻 会员 周边

NHibernate真的好用吗

0
悬赏园豆:100 [待解决问题]

这几天一直在查博客,发现NHibernate并不好用:
1.两张表完全没有主外键关系的时候,能用NHibernate吗?
2.两张表(比如订单,里面关联一个customer Name 通过customerID 关联)要关联查询某一个字段,而在子表中(比如上面的订单表)没有明显的外键,这时候在<join>标签中指定property-ref属性是完全不起作用的.(测试了一下,这时候无论你是否指定了property-ref NH都是默认用两张表的主键做关联的)
问题:NHibernate完全不适合两张毫无关系(数据库表建立的关系,而不是逻辑关系)的查询.
3.数据表中如果有冗余字段(比如上面订单中,除了CustomerID额外冗余CustomerName),这时候getter和setter的映射关系就比较难处理.
4.NH在建立关系映射的时候,非得指定Many-to-one,many-to-many等几种,映射关系,在查询的时候,有时候并不需要将one-to-many中的many查出来,插入的时候,可能并不需要初始化many,这种笨重的写法,实在不明白是不是设计的缺陷.关于这个国外的网站会提供下面的代码.
/// <summary>
/// There are no comments for NHibernate_Console.Person, NHibernate_Console in the schema.
/// </summary>
public partial class Person {

#region Extensibility Method Definitions

/// <summary>
/// There are no comments for OnCreated in the schema.
/// </summary>
partial void OnCreated();

#endregion
/// <summary>
/// There are no comments for Person constructor in the schema.
/// </summary>
public Person()
{
OnCreated();
}


/// <summary>
/// There are no comments for Id in the schema.
/// </summary>
public virtual int Id
{
get;
set;
}


/// <summary>
/// There are no comments for Name in the schema.
/// </summary>
public virtual string Name
{
get;
set;
}


/// <summary>
/// There are no comments for Address in the schema.
/// </summary>
public virtual string Address
{
get;
set;
}
public virtual string GradeName
{
get;
set;
}

/// <summary>
/// There are no comments for GradeId in the schema.
/// </summary>
public virtual System.Nullable<int> GradeId
{
get;
set;
}


/// <summary>
/// There are no comments for ThisGrade in the schema.
/// </summary>
public virtual Grade ThisGrade
{
get;
set;
}
}

#region Extensibility Method Definitions

/// <summary>
/// There are no comments for OnCreated in the schema.
/// </summary>
partial void OnCreated();

#endregion
/// <summary>
/// There are no comments for Grade constructor in the schema.
/// </summary>
public Grade()
{
this.Persons = new List<Person>();
OnCreated();
}


/// <summary>
/// There are no comments for Id in the schema.
/// </summary>
public virtual int Id
{
get;
set;
}


/// <summary>
/// There are no comments for Name in the schema.
/// </summary>
public virtual string Name
{
get;
set;
}


/// <summary>
/// There are no comments for Note in the schema.
/// </summary>
public virtual string Note
{
get;
set;
}


/// <summary>
/// There are no comments for Persons in the schema.
/// </summary>
public virtual IList<Person> Persons
{
get;
set;
}
}


=== 执行插入的时候:

public int Add(Person person)
{
using (ISession _session = NHibernateHelper.OpenSession())
{
using (ITransaction _tran = _session.BeginTransaction())
{

var _grade = _session.Get<Grade>(22);
person.ThisGrade = _grade;
_grade.Persons = new List<Person> { person};
_session.Save(_grade); //这里差点看成无限递归了
_session.Save(person);
_tran.Commit();
return 0;
}
}
}

RemiHoston的主页 RemiHoston | 初学一级 | 园豆:65
提问于:2015-10-19 15:19
< >
分享
所有回答(3)
1

Hibernate中的mapping,和数据库中的主外键约束 没有绝对的联系。
即使数据库中不设置外键关联,同样可以在hibernate中设置一对多,或者多对一,多对多的映射关系,只要你的表符合外键关联的设计要求就可以,NH在建立关系映射的时候,是得指定Many-to-one,many-to-many等几种,你是会把many查询出来,你用一个东西当然有好处,有坏处,自己权衡吧,我觉得它一直需要写mapping我不喜欢,其实和ef的原理一样的

稳稳的河 | 园豆:4216 (老鸟四级) | 2015-10-19 15:37
0

java中的Hibernate用的比较多的,但是.NET的NHibernate用的感觉不太多,感觉不太好用

JackWang-CUMT | 园豆:2866 (老鸟四级) | 2015-10-20 08:30
0

在.NET中,重量级框架里面,NHIBERNATE目前还没有跟entity framework抗衡的能力。轻量级的你就更不应该用nh了,其实使用代码生成器就可以。或者说为了方便管理sql,使用将sql写入到xml,就使用ibatis。如果说只是为了将结果集映射到实体中,可以选用dapper。如果以上需要都没有,那就直接用dbhelper,省心省力省资源。

ensleep | 园豆:1682 (小虾三级) | 2015-10-20 13:11
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册