Skip to content

Commit d7bf56d

Browse files
rwinchunknown
authored and
unknown
committed
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
}

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.orm.hibernate4;
1818

1919
import java.io.IOException;
20-
import java.lang.reflect.Method;
2120
import javax.persistence.Embeddable;
2221
import javax.persistence.Entity;
2322
import javax.persistence.MappedSuperclass;
@@ -46,7 +45,6 @@
4645
import org.springframework.transaction.jta.JtaTransactionManager;
4746
import org.springframework.util.Assert;
4847
import org.springframework.util.ClassUtils;
49-
import org.springframework.util.ReflectionUtils;
5048

5149
/**
5250
* A Spring-provided extension of the standard Hibernate {@link Configuration} class,
@@ -75,13 +73,6 @@ public class LocalSessionFactoryBuilder extends Configuration {
7573
new AnnotationTypeFilter(Embeddable.class, false),
7674
new AnnotationTypeFilter(MappedSuperclass.class, false)};
7775

78-
private static final Method addAnnotatedClassMethod =
79-
ClassUtils.getMethod(Configuration.class, "addAnnotatedClass", Class.class);
80-
81-
private static final Method addPackageMethod =
82-
ClassUtils.getMethod(Configuration.class, "addPackage", String.class);
83-
84-
8576
private final ResourcePatternResolver resourcePatternResolver;
8677

8778

@@ -169,7 +160,7 @@ else if (jtaTransactionManager instanceof TransactionManager) {
169160
*/
170161
public LocalSessionFactoryBuilder addAnnotatedClasses(Class<?>... annotatedClasses) {
171162
for (Class<?> annotatedClass : annotatedClasses) {
172-
ReflectionUtils.invokeMethod(addAnnotatedClassMethod, this, annotatedClass);
163+
addAnnotatedClass(annotatedClass);
173164
}
174165
return this;
175166
}
@@ -181,7 +172,7 @@ public LocalSessionFactoryBuilder addAnnotatedClasses(Class<?>... annotatedClass
181172
*/
182173
public LocalSessionFactoryBuilder addPackages(String... annotatedPackages) {
183174
for (String annotatedPackage :annotatedPackages) {
184-
ReflectionUtils.invokeMethod(addPackageMethod, this, annotatedPackage);
175+
addPackage(annotatedPackage);
185176
}
186177
return this;
187178
}

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.orm.hibernate4;
1818

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

2221
import org.apache.commons.logging.Log;
@@ -53,8 +52,6 @@
5352
import org.springframework.dao.InvalidDataAccessApiUsageException;
5453
import org.springframework.dao.InvalidDataAccessResourceUsageException;
5554
import org.springframework.jdbc.datasource.DataSourceUtils;
56-
import org.springframework.util.ClassUtils;
57-
import org.springframework.util.ReflectionUtils;
5855

5956
/**
6057
* Helper class featuring methods for Hibernate Session handling.
@@ -79,13 +76,6 @@ public abstract class SessionFactoryUtils {
7976
public static final int SESSION_SYNCHRONIZATION_ORDER =
8077
DataSourceUtils.CONNECTION_SYNCHRONIZATION_ORDER - 100;
8178

82-
/**
83-
* A Method handle for the <code>SessionFactory.openSession()</code> method.
84-
* The return value differs between Hibernate 3.x and 4.x; for cross-compilation purposes,
85-
* we have to use reflection here as long as we keep compiling against Hibernate 3.x jars.
86-
*/
87-
private static final Method openSessionMethod = ClassUtils.getMethod(SessionFactory.class, "openSession");
88-
8979
static final Log logger = LogFactory.getLog(SessionFactoryUtils.class);
9080

9181

@@ -103,17 +93,6 @@ public static DataSource getDataSource(SessionFactory sessionFactory) {
10393
return null;
10494
}
10595

106-
/**
107-
* Obtain a new Session from the given SessionFactory.
108-
* <p>Bridges between Hibernate signature differences.
109-
* @param sessionFactory the SessionFactory to use
110-
* @return the new Session
111-
* @see org.hibernate.SessionFactory#openSession()
112-
*/
113-
public static Session openSession(SessionFactory sessionFactory) {
114-
return (Session) ReflectionUtils.invokeMethod(openSessionMethod, sessionFactory);
115-
}
116-
11796
/**
11897
* Perform actual closing of the Hibernate Session,
11998
* catching and logging any cleanup exceptions thrown.

spring-orm/src/main/java/org/springframework/orm/hibernate4/SessionHolder.java renamed to spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/SessionHolder.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/SpringFlushSynchronization.java renamed to spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/SpringFlushSynchronization.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/SpringJtaSessionContext.java renamed to spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/SpringJtaSessionContext.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/SpringSessionContext.java renamed to spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/SpringSessionContext.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/SpringSessionSynchronization.java renamed to spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/SpringSessionSynchronization.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/support/OpenSessionInViewFilter.java renamed to spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/support/OpenSessionInViewFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ protected SessionFactory lookupSessionFactory() {
203203
*/
204204
protected Session openSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException {
205205
try {
206-
Session session = SessionFactoryUtils.openSession(sessionFactory);
206+
Session session = sessionFactory.openSession();
207207
session.setFlushMode(FlushMode.MANUAL);
208208
return session;
209209
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public void afterConcurrentHandlingStarted(WebRequest request) {
184184
*/
185185
protected Session openSession() throws DataAccessResourceFailureException {
186186
try {
187-
Session session = SessionFactoryUtils.openSession(getSessionFactory());
187+
Session session = getSessionFactory().openSession();
188188
session.setFlushMode(FlushMode.MANUAL);
189189
return session;
190190
}

spring-orm/src/main/java/org/springframework/orm/hibernate4/support/package-info.java renamed to spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/support/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
/*
3-
* Copyright 2002-2011 the original author or authors.
3+
* Copyright 2002-2012 the original author or authors.
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)