博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Migrating to Spring 3.1 and Hibernate 4.1
阅读量:2401 次
发布时间:2019-05-10

本文共 3637 字,大约阅读时间需要 12 分钟。

Migrating to Spring 3.1 and Hibernate 4.1

Update to Spring 3.2
The application has been updated to Spring 3.2. The new version is available here:  . The old one is still available:  .

As part of the , we have a lab application that we use to show how to integrate Spring and JPA/Hibernate together. We have just upgraded it to Spring 3.1 / Hibernate 4.1, and thought we should share a few tips.

1) Maven Dependencies

The configuration sample below is based on Maven (but you can easily convert it to Gradle if needed). Inside your POM, you should specify the latest versions of Spring and Hibernate (you might need to declare more Spring dependencies depending on which Spring components you're using).

org.springframework
spring-orm
3.2.0.RELEASE
org.hibernate
hibernate-entitymanager
4.1.9.Final

The cglib dependency is not always needed. You will find more details on that at the end of this blog entry.

2.a) Spring configuration for Hibernate

Spring provides support for several versions of Hibernate, so you need to specify explicitly which version you're using.

With Hibernate 4:

...

If you were to work with Hibernate 3, you would have instead:

...

(the package path is different according to whether you wish to use Hibernate 3 or Hibernate 4)

When working with Hibernate, there are 2 common ways of writing your mapping: XML and annotations.

Spring-Hibernate 3 integration allows you to configure 2 kinds of SessionFactoryBean:

  • LocalSessionFactoryBean for xml mapping only
  • AnnotationSessionFactoryBean for xml mapping and/or Annotation-based mapping

With Spring 3.1 and Hibernate 4, things are now simpler: there is only one SessionFactoryBean called LocalSessionFactoryBean. It works with both annotation-based mapping and xml-based mapping.

If you followed those 2 steps carefully, you should have a running application already.

2.b) Spring configuration for JPA

As an alternative of using Hibernate directly, you can also use JPA as an abstraction layer on top of Hibernate. Here is an example:

jpa/config

3) Do you need that CGLIB dependency?

The answer is likely to be a "yes". It is actually common that Spring would need it under the hood. Here are some examples:

  • Java configuration (more info )
  • Adding features such as Transactional behavior, Security, Caching... to a Spring bean that does not implement an interface (more information )
  • When the @Transactional annotation is used inside a JUnit Test case (you usually do that so the database transaction can rollback at the end of each test case, see  for more details).
  • ...

4) Java Configuration

Using  with Spring is an interesting alternative to xml-based and annotation-based configuration. So how to work with Spring and Hibernate using Java configuration? A new class called LocalSessionFactoryBuilder makes things pretty easy.

@Beanpublic SessionFactory sessionFactory() {
return new LocalSessionFactoryBuilder(dataSource()) .addAnnotatedClasses(Person.class, Account.class) .buildSessionFactory();}

LocalSessionFactoryBuilder subclasses Hibernate's own Configuration class, and as the name suggests, provides a convenient builder-style API for use within Spring @Configuration classes.

 

All the above code samples are available here:

转载地址:http://enpob.baihongyu.com/

你可能感兴趣的文章
有效库存:供应链产业切肤之痛 (zt)
查看>>
李嘉诚给年青人提出的53条人生忠告(zt)
查看>>
第23课 企业信息资源规划系列讲堂之一
查看>>
第十七章 控制工作概述
查看>>
佳句与老友一起分享
查看>>
解读BPM风潮:缘何受到企业青睐?(转载)
查看>>
成功经理人与自我管理能力(转载)
查看>>
ERP系统应用与管理咨询(转载)
查看>>
精益思想—人、过程和技术的集成(zt)
查看>>
超市食品名称中英文对照(zt)
查看>>
第六章 预测与决策
查看>>
14大管理方法工具(zt)
查看>>
职业生涯规划与管理实操(zt)
查看>>
国贸、货代常用词汇(zt)
查看>>
人力资源术语英汉对照(zt)
查看>>
传说中的100句英语可以帮你背7000单词(zt)
查看>>
网管IT服务管理五个心得 (zt)
查看>>
循序渐进学SAP系列(一):--SAP该如何入门
查看>>
PMP考试经验谈(转载)
查看>>
緣分是找到包容你的人(转载)
查看>>