先简单介绍一下问题的语境。
手头有个开发了3年的Spring+iBATIS应用(经典三层架构),最近尝试用Hibernate实现原有SQLMap版的部分CRUD操作。除开混合事务和其他一些底层配置细节(如TransactionAwareDataSource、禁用lazy-load等)之外,最大的一个"pattern-mismatch"是:Model层和持久层采用了dirty flag机制,即每次INSERT和UPDATE操作,都会根据每个字段的dirty与否决定是否参加INSERT/UPDATE,而这些dirty flag可以被外部重置,所以业务层的代码,经常可以看到类似这样的pattern:
1- new一个model类并setId() (或者在已有的model上重置dirty flag)
2- set需要update的字段(通常都只是部分字段)
3- 丢给DAO层去update
最终的效果是某张表某个ID的某条记录的某些字段被更新了,而其他字段不受影响,这就是我在标题中提到的所谓"暴力"update,虽不elegant,但却也简单实用,至少很"直接"。
为了让Hibernate版的DAO(默认除Trasient之外全体字段参加INSERT和UPDATE)继续支持这样的"use-pattern",除了按照Hibernate的习惯去配置映射和SessionFactory等之外,我们需要做一些额外的工作:
1- 在BO/Entity类上追加注解
<!---->@org.hibernate.annotations.Entity(dynamicInsert
=
true
, dynamicUpdate
=
true
)
2- 实现org.hibernate.Interceptor接口的findDirty()方法,Hibernate提供了一个EmptyInterceptor可以作为起点,方法签名如下:
<!---->public
int
[] findDirty(
Object entity,
Serializable id,
Object[] currentState,
Object[] previousState,
String[] propertyNames,
Type[] types
);
返回的int[]包含所有应该被认为是dirty的字段索引,返回null表示默认处理,传入的entity是持久对象,字段列表会通过propertyNames参数传给你。
3- 注入到Spring的Application Context中,类似这样:
<!----><
bean
id
="findDirtyInterceptor"
class
="gao.sean.hybrid.interceptor.FindDirtyInterceptor"
/>
<
bean
id
="sessionFactory"
class
="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
>
<
property
name
="entityInterceptor"
><
ref
bean
="findDirtyInterceptor"
/></
property
>
</
bean
>
在这样的配置下,业务层的代码就可以继续"暴力"update了。
分享到:
相关推荐
Agile Java™ Development With Spring, Hibernate and Eclipse is a book about robust technologies and effective methods which help bring simplicity back into the world of enterprise Java development....
Agile Java™ Development With Spring, Hibernate and Eclipse is a book about robust technologies and effective methods which help bring simplicity back into the world of enterprise Java development....
Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的Rambler.ru 站点开发的,它已经在该站点运行超过四年多了。 Igor 将源代码以类BSD许可证的形式发布。自Nginx 发布四年来,Nginx 已经因为它的稳定性、丰富的功能集 ...
目 录 一、 Nginx 基础知识 二、 Nginx 安装及调试 三、 Nginx Rewrite 四、 Nginx Redirect 五、 Nginx 目录自动加斜线: 六、 Nginx Location 七、 Nginx expires 八、 Nginx 防盗链 九、 Nginx 访问控制 ...
Spring-Hibernate-示例使用 Spring、Hibernate、Gradle、Eclipse、Tomcat、Mysql 的 Java Web 应用程序框架。 ##tips 如果您正在构建一个 web 应用程序,请确保您应用 'eclipse-wtp' 插件而不是 'eclipse'。
Best+of+VIM+Tips(译注).pdf Best+of+VIM+Tips(译注).pdf
Spring State Machine上的Spring Tips Spring State Machine上的Spring提示的源代码
文件已上传到百度网盘,附件中是下载地址。... Hibernate-A.Developer's.Notebook—Hibernate程序高手秘笈 [美] james elliott(著) | o'reilly taiwan公司编译(译) | 东南大学出版社 | 9787564106379 |
abap tips abap tips abap tips abap tips abap tips
开始Hibernate之旅 1.2. 第一个可持久化类 1.3. 映射cat 1.4. 与猫同乐 1.5. 结语 2. 体系结构 2.1. 总览 2.2. JMX集成 2.3. JCA支持 3. SessionFactory配置 3.1. 可编程配置方式...
相当不多的解决方案,你可以按照书中规范完成相应的框架,理解其中的本质。
android TIPS小结android TIPS小结android TIPS小结android TIPS小结
Building RESTful Web Services with Spring 5 – Second Edition: Leverage the power of Spring 5.0, Java SE 9, and Spring Boot 2.0 Find out how to implement the REST architecture to build resilient ...
Along with detailed analysis of the code and functionality, plus the first published coverage of Spring Web Flow 2.x, this book includes numerous tips and tricks to help you get the most out of ...
tips:这是实战篇,默认各位看官具备相应的基础(文中使用了Lombok插件,如果使用源码请先安装插件) @[TOC] 项目配置 依赖 <groupId>org.springframework.boot <artifactId>spring-boot-starter-data-redis ...
swifter-tips.pdfswifter-tips.pdfswifter-tips.pdfswifter-tips.pdfswifter-tips.pdfswifter-tips.pdfswifter-tips.pdfswifter-tips.pdfswifter-tips.pdfswifter-tips.pdfswifter-tips.pdfswifter-tips.pdfswifter-...
jquery,tips,冒泡,jquery,tips,冒泡
jquery tips提示图片信息
spring-tips.github.io:Spring Tips Live系列的静态站点