Archive for the ‘hibernate’ Category

spring入门学习的例子

Saturday, May 10th, 2008




spring自带的jpetstore和petclinic都是很好的入门例子,可以学习各种配置、怎样与hibernate结合等等。

Hibernate3使用anotations的设置

Friday, May 9th, 2008




<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools.                   --> <hibernate-configuration> <session-factory> <property name="connection.datasource">java:/DefaultMySqlDS</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- --> <property name="hibernate.jdbc.batch_size">20</property> <property name="myeclipse.connection.profile">mysql.Driver</property> <property name="hibernate.cache.use_query_cache">true</property> <property name="hibernate.cache.use_second_level_cache">true</property> <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> </session-factory> </hibernate-configuration> package hibernate; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.cfg.Configuration; import test.model.*; /** * Configures and provides access to Hibernate sessions, tied to the * current thread of execution.  Follows the Thread Local Session * pattern, see {@link http://hibernate.org/42.html ...

在spring和hibernate下管理ehcache和query cache

Thursday, May 8th, 2008




1. 在Hibernate配置文件中设置: com/ouou/model/Videos.hbm.xml ...

配置Spring+hibernate使用ehcache作为second-level cache

Thursday, May 8th, 2008


大量数据流动是web应用性能问题常见的原因,而缓存被广泛的用于优化数据库应用。cache被设计为通过保存从数据库里load的数据来减少应用和数据 库之间的数据流动。数据库访问只有当检索的数据不在cache里可用时才必要。hibernate可以用两种不同的对象缓存:first-level cache 和 second-level cache。first-level cache和Session对象关联,而second-level cache是和Session Factory对象关联。 缺省地,hibernate已经使用基于每个事务的first-level cache。 Hibernate用first-level cache主要是减少在一个事务内的sql查询数量。例如,如果一个对象在同一个事务内被修改多次,hibernate将只生成一个包括所有修改的 UPDATE SQL语句。为了减少数据流动,second-level cache在Session Factory级的不同事务之间保持load的对象,这些对象对整个应用可用,不只是对当前用户正在运行的查询。这样,每次查询将返回已经load在缓存 里的对象,避免一个或更多潜在的数据库事务。 下载ehcache,hibernate3.2必须要ehcache1.2以上才能支持。可以修改log4j配置文件log4j.logger.net.sf.hibernate.cache=debug查看日志 1.在类路径上ehcache.xml: <ehcache> <!-- Sets the path to the directory where cache .data files are created. If the path is a Java System Property it is replaced by its value in the running VM. The following ...

Hibernate性能

Thursday, May 8th, 2008


1、反转控制权的设置:inverse=false(default) § 用于单向one-to-many关联 § parent.getChildren().add(child) // insert child § parent.getChildren().delete(child) // delete child inverse=true § 用于双向one-to-many关联 § child.setParent(parent); session.save(child) // insert child § session.delete(child) 在分层结构的体系中 parentDao, childDao对于CRUD的封装导致往往直接通过session接口持久化对象,而很少通过关联对象可达性 2、单向关系还是双向关系? § parent.getChildren().add(child)对集合的触及操作会导致lazy的集合初始化, 在没有对集合配置二级缓存的情况下,应避免此类操作 § select * from child where parent_id = xxx; 性能口诀: § 一般情况下避免使用单向关联,尽量使用双向关联 § 使用双向关联,inverse=“true” § 在分层结构中通过DAO接口用session直接持久化对象,避免通过关联关系进 行可达性持久化 3、单向many-to-one表达了外键存储方 灵活运用many-to-one可以避免一些不必要的性能问题 many-to-one表达的含义是:0..n : 1,many可以是0,可以是1,也 可以是n,也就是说many-to-one可以表达一对多,一对一,多对一 关系 因此可以配置双向many-to-one关系,例如: 一桌四人打麻将,麻将席位和打麻将的人是什么关系?是双向many-to-one的 关系 4、one-to-one通过主键进行关联 相当于把大表拆分为多个小表 例如把大字段单独拆分出来,以提高数据库操作的性能 Hibernate的one-to-one似乎无法lazy,必须通过bytecode enhancement 5、one-to-many VS many-to-many §  List需要维护index column,不能被用于双向关联,必须inverse=“false”,被 谨慎的使用在某些稀有的场合 §  Bag/Set语义上没有区别 §  我个人比较喜欢使用Bag many-to-many §  Bag和Set语义有区别 §  建议使用Set children = session.createFilter(parent.getChildren(), “where this.age > 5 and this.age < 10”).list() 针对一对多关联当中的集合元素非常庞大的情况,特别适合于庞大集合的分页: §  ...

SSH+ Annotations 項目需要注意的事項

Tuesday, May 6th, 2008


剛完成了這個例子: http://cwiki.apache.org/S2WIKI/struts-2-spring-2-jpa-ajax.html 各版本之間要兼容,每個jar也可能有不同的版本,版本一定要兼容,否則會出現異常。如果不清楚應該用什么版本那就都用最新版本。Eclipse的Java -> Compiler level設置要設為5.0 另外,如果是使用jboss4,需要delete hibernate-annotations.jar from the lib of jboss/default. http://forum.springframework.org/showthread.php?t=44433 开发工具使用eclipse euro winter 的时候,发布到jboss老是出现问题,用myeclipse插件好用一些。用ant来deploy很好用,用myeclipse的发布多个项目的时候会出现问题。