abluedog

闲来无胜情,沽酒买一醉;闻歌知雅意,笑看是与非。

统计

常用链接

留言簿(9)

阅读排行榜

评论排行榜

我的评论

@zhuwei
模板方法的使用在hibernate里面很普遍,您的方法确实可行。

spring已经内建了对hibernate的支持,已经包含了getHibernateDao等一系列模板方法,您可以去参考一下。不过遗憾的是spring.net还没有实现对nhibernate的支持。

而session您大可不必每次都用sessionfactory来获得,可以采用open session in view的模式来实现。

如果想变得真正的优雅,那需要结合IoC及AOP,以声明性的事务来代替到处的事务编码,您可以参考一下castle的容器实现或者直接去参考spring的实现(spring.net目前还不支持)
@pwei
Example.create:Version properties, identifiers and associations are ignored. By default, null valued properties are excluded.
@pwei
session.Delete(hql)和query.executeUpdate的实现是不一样的。
如果你使用session.Delete("from person"),那么实际的执行代码如下:
IList list = session.CreateQuery("from person").List();
for(int i=0;i<list.Count;i++)
{
session.Delete(list[i]);
}

当你删除单个或者少量的对象时,这样还是可以的,但是如果你要delete 10000条记录,
那么NHibernate首先会加载10000个对象到内存中,然后循环一个一个删除!这样你的内存
嗖地就上去了。
所以如果你要批量删除大量数据,那么还是直接获取connection,自己写sql语句,而不要指望NHibernate来做。而Hibernate3.0实现的bulk update/delete即query.executeupdate,实际上也是直接调用sql,由于实现的原因,当使用这种方式删除掉对象后,缓存中的对象还没有被删除,这就造成了数据的不一致。所以,我在上面提到过,即使是在hibernate里也是建议慎重考虑使用的。
如果一定要使用这种方式,或者直接调用sql来进行批量delte/update,那么最好在调用完毕后clear一下缓存,让过期的对象不再保存。

re: NHibernate Step by Step(二) 单表操作 abluedog 2006-04-22 20:10  
@SOSOS's BLog
@mzl
目前还是以文章为主,测试程序都是说明性的,等这个系列完了后,我会再重新整理出来一份文档及示例代码,抱歉了。

@和地球人一起
抱歉让您理解不清楚了。
我在前面已经说明是使用的vs2005演示的,可能我忘了一点,我使用的是team suite版,如果您使用的是professional版或者更低的版本,可能不会包含完整的测试框架。
至于mstest,我想您看一下sdk就应该很清楚了,我在这里就不费笔墨在nhibernate外的地方了。
@INRIE
nhibernate未来会实现这样的功能。
但是需要注意的是,这种方法会引起缓存的不同步,即使是在hibernate里也是建议慎重考虑使用的。在后面的高级话题里我们会谈到这个问题。
如果您非常需要这个功能,建议:
IDbConnection conn = session.Connection;
IDbCommand command = conn.CreateCommand(...);
这样您就可以使用任何sql语句了,当然您必须清楚自己在干什么,所以您需要自己维护缓存的有效性以及保持同步的问题。
@卡卡.net
在下面几篇我还会持续深入讲解hql的。
@Terrylee
那下次就来个长的:)
@pwei
目前的版本还不支持直接调用sp。

@Tutuya
不用这么麻烦啊,直接通过ISession.Connection就可以获得当前session使用的DbConnection,也就可以做任何事情了,而且每次都buildfactory,代价太大了啊。

可以这样封装一下:
ublic IList ExecuteStoredProc( DbConnection conn,string spName, ICollection paramInfos ) { ...}

Invoke:
ExecuteStoredProc(session.Connection,spName,...)
re: NHibernate Step by Step:序篇 abluedog 2006-04-16 15:48  
以下关于性能的QA来自Hibernate官方网站:
Many people try to benchmark Hibernate. All public benchmarks we have seen so far had (and most still have) serious flaws.

The first category of benchmarks are trivial micro benchmarks. Hibernate of course will have an overhead in simple scenarios (loading 50.000 objects and doing nothing else is considered trivial) compared to JDBC. See this page for a critique of a trivial benchmark. If you'd like to avoid writing your own trivial and not very conclusive tests, have a look at the perftest target in Hibernate's build file. We use this target to check if a trivial performance bug slipped into the Hibernate code. You can use it to verify the JDBC overhead of Hibernate in trivial situations. But, as should be clear now, these numbers are meaningless for real application performance and scalability.

In a fair benchmark with complex data associations/joins, highly concurrent access, random updates of data in the application, real-world data set size, and utilizing other Hibernate features, you will find Hibernate perform very well. Why is there no such benchmark provided by the Hibernate developers? The first reason is trust. Why would you believe that the numbers shown by the vendor of a product, in a comparative benchmark, are true? The second reason is applicability. Certainly, a fair benchmark would show the benefits of Hibernate in a typical complete application with realistic concurrent access. However, the number of variables in any decent benchmark make it almost impossible to transfer these results into reasonable conclusions about the performance of your own application. Your application is different. (If you came here from a forum thread, think about it: instead of arguing with you about your trivial micro benchmark, we would be arguing why you don't see the same results in your application...) For these reasons we always recommend that you benchmark your application yourself. We encourage you to see performance and load testing as a natural stage in the life of your application - don't go into production with only micro benchmarks. Setting up benchmarks for your application and scenario, and helping you in this stage is in fact one of our usual support and consulting services.
Conclusion?

It turns out that Hibernate is very fast if used properly, in highly concurrent multi-user applications with significant data sets.

原文在http://www.hibernate.org/15.html
re: NHibernate Step by Step(二) 单表操作 abluedog 2006-04-16 15:37  
@javac
你可以使用nunit,基本上差别不大。之所以使用MS Test,是因为ms既然在ide里包含了测试框架,那么不管我们喜欢还是不喜欢,它肯定会获得比较广泛的应用——这也是没办法的事。
除了Code Smith外,还有My Generation等代码生成工具,都支持NHibernate文件的生成。
My Generation:http://www.mygenerationsoftware.com/portal/default.aspx
NHibernate Template:
http://www.mygenerationsoftware.com/phpBB2/viewtopic.php?t=1505
re: NHibernate Step by Step:序篇 abluedog 2006-04-15 14:55  
@Michael.zh
“世上没有救世主,也没有灵丹妙药”

当然,也没有银弹。:)解决了持久层,还有业务层,表现层,都少不了写代码:)
@福娃
实际上,在我上面的演示代码里其实也应该添加System.Collections引用的,因为目前nhibernate官方还不支持generic,一个query对象返回的还是一个普通的IList。
第3方的nhibernate generic方案请参考:
http://www.ayende.com/projects/nhibernate-query-analyzer/generics.aspx
re: NHibernate Step by Step:序篇 abluedog 2006-04-15 10:39  
@Michael.zh
理论上讲,o/r mapping由于进行了反射、生成sql及框架运行的维护等等,性能是要比直接的数据库操作稍微差一点,但是你要考虑团队开发中出现的蹩脚的sql语句,没有考虑优化的架构等等可以直接影响ado.net性能的问题,而使用o/r mapping工具,基础的操作都有框架来完成,一个设计优良的框架足以弥补以上不足,况且使用o/r mapping后而产生的生产力的大幅提高,更快的开发效率,足以让人心动。
o/r mapping框架的性能问题,你可以去搜索一下hibernate与jdbc性能比较的文章,基本上都可以满足一般的企业开发。
至于你所说的“关键领域”,我不知道你是不是指一些对性能或者实时性要求非常高的环境,这样的项目,一般都不大会使用java/.net这样的基于虚拟机的语言来开发,比如电信方面项目等等的核心部分,毕竟,都不能与c/c++来比较。


@侵晨
我会尽量保持每周2到3篇的速度。