Skip to content

Commit d7bf56d

Browse files
rwinchunknown
authored andcommitted
Remove reflection usage in orm.hibernate4.*
Previously reflection was required when interacting with Hibernate 4 in order to support both Hibernate 3 and Hibernate 4 since there were non-passive changes in the APIs. Now that the Spring build uses Gradle it is trivial to support multiple Hibernate versions. This commit removes the reflection usage in orm.hibernate4.* by creating a spring-orm-hibernate4 module that uses gradle/merge-artifacts.gradle to build a single artifact but keep distinct classpaths. Issue: SPR-10039
1 parent cf681a8 commit d7bf56d

22 files changed

+39
-63
lines changed

build.gradle

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,10 +431,9 @@ project('spring-orm') {
431431
// our respective orm.hibernate3 and orm.hibernate4 packages
432432
compile("aopalliance:aopalliance:1.0")
433433
compile("org.hibernate:com.springsource.org.hibernate:3.3.1.GA", optional)
434-
compile("org.hibernate:hibernate-core:4.1.0.Final", optional)
435434
compile("org.hibernate:hibernate-cglib-repack:2.1_3", optional)
436435
compile("org.hibernate:hibernate-annotations:3.4.0.GA", optional)
437-
compile("org.hibernate:hibernate-entitymanager:4.1.0.Final", optional)
436+
compile("org.hibernate:hibernate-entitymanager:3.4.0.GA", optional)
438437
compile("org.apache.openjpa:openjpa:1.1.0", optional)
439438
compile("org.eclipse.persistence:org.eclipse.persistence.core:1.0.1", optional)
440439
compile("org.eclipse.persistence:org.eclipse.persistence.jpa:1.0.1", optional)
@@ -459,6 +458,24 @@ project('spring-orm') {
459458
}
460459
}
461460

461+
project('spring-orm-hibernate4') {
462+
description = 'Spring Object/Relational Mapping - Hibernate 4 support'
463+
ext.mergeIntoProject = project(':spring-orm')
464+
apply from: "${gradleScriptDir}/merge-artifacts.gradle"
465+
dependencies {
466+
compile project(":spring-orm").sourceSets.main.output
467+
compile project(":spring-tx")
468+
compile project(":spring-jdbc")
469+
compile("org.hibernate:hibernate-core:4.1.0.Final", optional)
470+
compile("org.hibernate:hibernate-entitymanager:4.1.0.Final", optional)
471+
compile(project(":spring-web")) { dep ->
472+
optional dep
473+
exclude group: 'javax.persistence', module: 'persistence-api'
474+
}
475+
compile("javax.servlet:servlet-api:2.5", optional)
476+
}
477+
}
478+
462479
project('spring-webmvc') {
463480
description = 'Spring Web MVC'
464481
dependencies {

settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ include 'spring-instrument-tomcat'
1212
include 'spring-jdbc'
1313
include 'spring-jms'
1414
include 'spring-orm'
15+
include 'spring-orm-hibernate4'
1516
include 'spring-oxm'
1617
include 'spring-struts'
1718
include 'spring-test'

spring-orm/src/main/java/org/springframework/orm/hibernate4/HibernateExceptionTranslator.java renamed to spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateExceptionTranslator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

spring-orm/src/main/java/org/springframework/orm/hibernate4/HibernateJdbcException.java renamed to spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateJdbcException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

spring-orm/src/main/java/org/springframework/orm/hibernate4/HibernateObjectRetrievalFailureException.java renamed to spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateObjectRetrievalFailureException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

spring-orm/src/main/java/org/springframework/orm/hibernate4/HibernateOptimisticLockingFailureException.java renamed to spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateOptimisticLockingFailureException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

spring-orm/src/main/java/org/springframework/orm/hibernate4/HibernateQueryException.java renamed to spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateQueryException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

spring-orm/src/main/java/org/springframework/orm/hibernate4/HibernateSystemException.java renamed to spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateSystemException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

spring-orm/src/main/java/org/springframework/orm/hibernate4/HibernateTransactionManager.java renamed to spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateTransactionManager.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.orm.hibernate4;
1818

19-
import java.lang.reflect.Method;
2019
import java.sql.Connection;
2120
import javax.sql.DataSource;
2221

@@ -45,8 +44,6 @@
4544
import org.springframework.transaction.support.DefaultTransactionStatus;
4645
import org.springframework.transaction.support.ResourceTransactionManager;
4746
import org.springframework.transaction.support.TransactionSynchronizationManager;
48-
import org.springframework.util.ClassUtils;
49-
import org.springframework.util.ReflectionUtils;
5047

5148
/**
5249
* {@link org.springframework.transaction.PlatformTransactionManager}
@@ -105,15 +102,6 @@
105102
public class HibernateTransactionManager extends AbstractPlatformTransactionManager
106103
implements ResourceTransactionManager, InitializingBean {
107104

108-
/**
109-
* A Method handle for the <code>SessionFactory.getCurrentSession()</code> method.
110-
* The return value differs between Hibernate 3.x and 4.x; for cross-compilation purposes,
111-
* we have to use reflection here as long as we keep compiling against Hibernate 3.x jars.
112-
*/
113-
private static final Method getCurrentSessionMethod =
114-
ClassUtils.getMethod(SessionFactory.class, "getCurrentSession");
115-
116-
117105
private SessionFactory sessionFactory;
118106

119107
private DataSource dataSource;
@@ -293,7 +281,7 @@ protected Object doGetTransaction() {
293281
}
294282
else if (this.hibernateManagedSession) {
295283
try {
296-
Session session = (Session) ReflectionUtils.invokeMethod(getCurrentSessionMethod, this.sessionFactory);
284+
Session session = this.sessionFactory.getCurrentSession();
297285
if (logger.isDebugEnabled()) {
298286
logger.debug("Found Hibernate-managed Session [" + session + "] for Spring-managed transaction");
299287
}
@@ -337,7 +325,7 @@ protected void doBegin(Object transaction, TransactionDefinition definition) {
337325

338326
try {
339327
if (txObject.getSessionHolder() == null || txObject.getSessionHolder().isSynchronizedWithTransaction()) {
340-
Session newSession = SessionFactoryUtils.openSession(getSessionFactory());
328+
Session newSession = getSessionFactory().openSession();
341329
if (logger.isDebugEnabled()) {
342330
logger.debug("Opened new Session [" + newSession + "] for Hibernate transaction");
343331
}

0 commit comments

Comments
 (0)