Migrating to Spring 3.1 and Hibernate 4.1

本文介绍了如何将应用更新到Spring 3.2,并分享了从Spring 3.1到Hibernate 4.1升级的几个关键步骤。包括Maven依赖配置、Spring配置与Hibernate整合、JPA使用、CGlib依赖以及Java配置方法。重点突出了Spring和Hibernate版本更新的过程和注意事项。
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:  Spring 3.2. The old one is still available:  Spring 3.1.

As part of the Core-Spring course, 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).

<dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-orm</artifactId>
     <version>3.2.0.RELEASE</version>
     <!-- will come with all needed Spring dependencies such as spring-core and spring-beans-->
</dependency>
<dependency>
     <groupId>org.hibernate</groupId>
     <artifactId>hibernate-entitymanager</artifactId>
     <version>4.1.9.Final</version>
     <!-- will come with Hibernate core-->
</dependency>

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:

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
...
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
     <property name="sessionFactory" ref="sessionFactory"/>
</bean>

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

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
...
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
     <property name="sessionFactory" ref="sessionFactory"/>
</bean>

(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:

<jdbc:embedded-database id="dataSource">
 <jdbc:script location="classpath:jpa/config/schema.sql" />
 </jdbc:embedded-database>

 <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="persistenceUnitName" value="jpaTest" />
    <property name="packagesToScan">
    <list>
       <value>jpa/config</value>
    </list>
    </property>
    <property name="jpaVendorAdapter">
       <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
          <property name="database" value="HSQL" />
       </bean>
    </property>
 </bean>

 <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
 </bean>

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 here)
  • Adding features such as Transactional behavior, Security, Caching... to a Spring bean that does not implement an interface (more information here)
  • 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 here for more details).
  • ...

4) Java Configuration

Using Java Configuration 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.

@Bean
public 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:https://github.com/michaelisvy/hibernate-4-spring-3.1-samples

(Kriging_NSGA2)克里金模型结合多目标遗传算法求最优因变量及对应的最佳自变量组合研究(Matlab代码实现)内容概要:本文介绍了克里金模型(Kriging)与多目标遗传算法NSGA-II相结合的方法,用于求解最优因变量及其对应的最佳自变量组合,并提供了完整的Matlab代码实现。该方法首先利用克里金模型构建高精度的代理模型,逼近复杂的非线性系统响应,减少计算成本;随后结合NSGA-II算法进行多目标优化,搜索帕累托前沿解集,从而获得多个最优折衷方案。文中详细阐述了代理模型构建、算法集成流程及参数设置,适用于工程设计、参数反演等复杂优化问题。此外,文档还展示了该方法在SCI一区论文中的复现应用,体现了其科学性与实用性。; 适合人群:具备一定Matlab编程基础,熟悉优化算法和数值建模的研究生、科研人员及工程技术人员,尤其适合从事仿真优化、实验设计、代理模型研究的相关领域工作者。; 使用场景及目标:①解决高计算成本的多目标优化问题,通过代理模型降低仿真次数;②在无法解析求导或函数高度非线性的情况下寻找最优变量组合;③复现SCI高水平论文中的优化方法,提升科研可信度与效率;④应用于工程设计、能源系统调度、智能制造等需参数优化的实际场景。; 阅读建议:建议读者结合提供的Matlab代码逐段理解算法实现过程,重点关注克里金模型的构建步骤与NSGA-II的集成方式,建议自行调整测试函数或实际案例验证算法性能,并配合YALMIP等工具包扩展优化求解能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值