Author: 江南白衣
HomePage: http://www.hibernate.org
入门最佳仍然是夏昕的Hibernate开发文档(OpenDoc) ,或者买一本《深入浅出》。
Hibernate的文档以Hibernate网站上最新版的Reference为主、为准。
曹晓刚的团队永远都会贴身翻译出中文版本,致敬:http://www.redsaga.com/hibernate-ref/3.1.2/zh-cn/html/
另外<Pro Hibernate3>也是H3的参考手册,可以EMule得到。
还有一个很重要的参考例子是Hibernate中以Topic命名的UnitTest。
Hibernate的话题讲N页也讲不完,这里只顺便记录一些。
参考Product表的映射及 Author/Work Example
参考Orders表的映射及Parent/Child Example ,Customer/Order/Product Example
1. 把一个ehcache.xml 定义文件拷贝到class-path.
2. jdbc.properties加上
hibernate.cache.use_query_cache=true
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
3. applicationContext.xml的 <property name="hibernateProperties">下加上
<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
<prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
Entity二级缓存是把PO放进cache,在同一个session范围内实现缓存,不同于查询的缓存。
Entity二级缓存在BookDao.get(3),和book.getCategory()的情况下都能用到,把category这样的小表完全cache到内存挺不错的。
在hbm文件中:
<class name="Product" table="PRODUCT" dynamic-insert="true" dynamic-update="true" discriminator-value="product">
<cache usage="nonstrict-read-write"/>
<id name="id" column="ID">
<generator class="native"/>
</id>
加一个Spring带的Filter是最简单通用的方法
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
看SpringSide中的History Event Listener,
在对象的update,delete,post-update等事件中加入AOP特性.
因为它类似数据库trigger,
贴近对象的生命周期事件,而且能拿到诸如update前后的数据, 传统的,以方法执行前后为Point
Cut的AOP方案达不到相同的效果,而数据库trigger则需要每个表写一次trigger,难于管理,没有Event Listener批量处理的效果。
Event Listener在批量处理时,一般设计让处理方法相同的POJO实现同一个接口,Event Listener里就以if(pojo instance of MyEventInterface)来区分处理。
String hql = " select new map(goods.goodsNo as goodsNo,goods.code as code) from Goods goods"
即可以用Map进行访问
Map goods =(Map)list.get(0);
String code = (String)goods.get("code");
对比直接用 result[0],result[1]访问的恐怖写法,或者定义一个新pojo的繁琐,返回map比较折衷。