您的位置:首页 > 编程语言 > Java开发

Hibernate最佳实践(Best Practices)

2007-06-04 11:28 369 查看
1、 使用Configuration装载映射文件时,不要使用绝对路径装载。最好的方式是通过getResourceAsStream()装载映射文件,这样Hibernate会从classpath中寻找已配置的映射文件。
2、 SessionFactory的创建非常消耗资源,整个应用一般只要一个SessionFactory就够了,只有多个数据库的时候才会使用多个SessionFactory。
3、 在整个应用中,Session和事务应该能够统一管理。(Spring为Hibernate提供了非常好的支持)
4、 将所有的集合属性配置设置为懒加载(lazy="true")。在hibernate2.x版本中,lazy默认值是"false",但hibernate3.x已经将lazy的默认改为"true"了。
5、 在定义关联关系时,集合首选Set,如果集合中的实体存在重复,则选择List(在定义配置文件时,可以将List定义为bag),数组的性能最差。
6、 HQL子句本身大小写无关,但是其中出现的类名和属性名必须注意大小写区分。
7、 在一对多的双向关联中,一般将集合的inverse属性设为true,让集合的对方维护关联关系。例如:Group-User,由User来维护Group和User的关联关系。
8、 在非分布式架构中,不需要使用DTO来向上层传输数据。直接使用POJO的Entity就可以了。
在<many-to-one>中,将outer-join 设置为false。If the SQL statements use join operations that are too complex and
slow, set outer-join to false for <many-to-one> associations (this is
enabled by default). Also try to tune with the global hibernate.
max_fetch_depth configuration option, but keep in mind that this
is best left at a value between 1 and 4.
9、 如果要精通Hibernate,熟练掌握关系数据库理论和SQL是前提。
10、 
If the SQL statements use join operations that are too complex and
slow, set outer-join to false for <many-to-one> associations (this is
enabled by default). Also try to tune with the global hibernate.
max_fetch_depth configuration option, but keep in mind that this
is best left at a value between 1 and 4.

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息