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
This commit is contained in:
Rob Winch 2012-11-26 12:57:29 -06:00 committed by unknown
parent cf681a809e
commit d7bf56df49
22 changed files with 39 additions and 63 deletions

View File

@ -431,10 +431,9 @@ project('spring-orm') {
// our respective orm.hibernate3 and orm.hibernate4 packages
compile("aopalliance:aopalliance:1.0")
compile("org.hibernate:com.springsource.org.hibernate:3.3.1.GA", optional)
compile("org.hibernate:hibernate-core:4.1.0.Final", optional)
compile("org.hibernate:hibernate-cglib-repack:2.1_3", optional)
compile("org.hibernate:hibernate-annotations:3.4.0.GA", optional)
compile("org.hibernate:hibernate-entitymanager:4.1.0.Final", optional)
compile("org.hibernate:hibernate-entitymanager:3.4.0.GA", optional)
compile("org.apache.openjpa:openjpa:1.1.0", optional)
compile("org.eclipse.persistence:org.eclipse.persistence.core:1.0.1", optional)
compile("org.eclipse.persistence:org.eclipse.persistence.jpa:1.0.1", optional)
@ -459,6 +458,24 @@ project('spring-orm') {
}
}
project('spring-orm-hibernate4') {
description = 'Spring Object/Relational Mapping - Hibernate 4 support'
ext.mergeIntoProject = project(':spring-orm')
apply from: "${gradleScriptDir}/merge-artifacts.gradle"
dependencies {
compile project(":spring-orm").sourceSets.main.output
compile project(":spring-tx")
compile project(":spring-jdbc")
compile("org.hibernate:hibernate-core:4.1.0.Final", optional)
compile("org.hibernate:hibernate-entitymanager:4.1.0.Final", optional)
compile(project(":spring-web")) { dep ->
optional dep
exclude group: 'javax.persistence', module: 'persistence-api'
}
compile("javax.servlet:servlet-api:2.5", optional)
}
}
project('spring-webmvc') {
description = 'Spring Web MVC'
dependencies {

View File

@ -12,6 +12,7 @@ include 'spring-instrument-tomcat'
include 'spring-jdbc'
include 'spring-jms'
include 'spring-orm'
include 'spring-orm-hibernate4'
include 'spring-oxm'
include 'spring-struts'
include 'spring-test'

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,7 +16,6 @@
package org.springframework.orm.hibernate4;
import java.lang.reflect.Method;
import java.sql.Connection;
import javax.sql.DataSource;
@ -45,8 +44,6 @@ import org.springframework.transaction.support.AbstractPlatformTransactionManage
import org.springframework.transaction.support.DefaultTransactionStatus;
import org.springframework.transaction.support.ResourceTransactionManager;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
/**
* {@link org.springframework.transaction.PlatformTransactionManager}
@ -105,15 +102,6 @@ import org.springframework.util.ReflectionUtils;
public class HibernateTransactionManager extends AbstractPlatformTransactionManager
implements ResourceTransactionManager, InitializingBean {
/**
* A Method handle for the <code>SessionFactory.getCurrentSession()</code> method.
* The return value differs between Hibernate 3.x and 4.x; for cross-compilation purposes,
* we have to use reflection here as long as we keep compiling against Hibernate 3.x jars.
*/
private static final Method getCurrentSessionMethod =
ClassUtils.getMethod(SessionFactory.class, "getCurrentSession");
private SessionFactory sessionFactory;
private DataSource dataSource;
@ -293,7 +281,7 @@ public class HibernateTransactionManager extends AbstractPlatformTransactionMana
}
else if (this.hibernateManagedSession) {
try {
Session session = (Session) ReflectionUtils.invokeMethod(getCurrentSessionMethod, this.sessionFactory);
Session session = this.sessionFactory.getCurrentSession();
if (logger.isDebugEnabled()) {
logger.debug("Found Hibernate-managed Session [" + session + "] for Spring-managed transaction");
}
@ -337,7 +325,7 @@ public class HibernateTransactionManager extends AbstractPlatformTransactionMana
try {
if (txObject.getSessionHolder() == null || txObject.getSessionHolder().isSynchronizedWithTransaction()) {
Session newSession = SessionFactoryUtils.openSession(getSessionFactory());
Session newSession = getSessionFactory().openSession();
if (logger.isDebugEnabled()) {
logger.debug("Opened new Session [" + newSession + "] for Hibernate transaction");
}

View File

@ -17,7 +17,6 @@
package org.springframework.orm.hibernate4;
import java.io.IOException;
import java.lang.reflect.Method;
import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.MappedSuperclass;
@ -46,7 +45,6 @@ import org.springframework.core.type.filter.TypeFilter;
import org.springframework.transaction.jta.JtaTransactionManager;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
/**
* A Spring-provided extension of the standard Hibernate {@link Configuration} class,
@ -75,13 +73,6 @@ public class LocalSessionFactoryBuilder extends Configuration {
new AnnotationTypeFilter(Embeddable.class, false),
new AnnotationTypeFilter(MappedSuperclass.class, false)};
private static final Method addAnnotatedClassMethod =
ClassUtils.getMethod(Configuration.class, "addAnnotatedClass", Class.class);
private static final Method addPackageMethod =
ClassUtils.getMethod(Configuration.class, "addPackage", String.class);
private final ResourcePatternResolver resourcePatternResolver;
@ -169,7 +160,7 @@ public class LocalSessionFactoryBuilder extends Configuration {
*/
public LocalSessionFactoryBuilder addAnnotatedClasses(Class<?>... annotatedClasses) {
for (Class<?> annotatedClass : annotatedClasses) {
ReflectionUtils.invokeMethod(addAnnotatedClassMethod, this, annotatedClass);
addAnnotatedClass(annotatedClass);
}
return this;
}
@ -181,7 +172,7 @@ public class LocalSessionFactoryBuilder extends Configuration {
*/
public LocalSessionFactoryBuilder addPackages(String... annotatedPackages) {
for (String annotatedPackage :annotatedPackages) {
ReflectionUtils.invokeMethod(addPackageMethod, this, annotatedPackage);
addPackage(annotatedPackage);
}
return this;
}

View File

@ -16,7 +16,6 @@
package org.springframework.orm.hibernate4;
import java.lang.reflect.Method;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
@ -53,8 +52,6 @@ import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.dao.InvalidDataAccessResourceUsageException;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
/**
* Helper class featuring methods for Hibernate Session handling.
@ -79,13 +76,6 @@ public abstract class SessionFactoryUtils {
public static final int SESSION_SYNCHRONIZATION_ORDER =
DataSourceUtils.CONNECTION_SYNCHRONIZATION_ORDER - 100;
/**
* A Method handle for the <code>SessionFactory.openSession()</code> method.
* The return value differs between Hibernate 3.x and 4.x; for cross-compilation purposes,
* we have to use reflection here as long as we keep compiling against Hibernate 3.x jars.
*/
private static final Method openSessionMethod = ClassUtils.getMethod(SessionFactory.class, "openSession");
static final Log logger = LogFactory.getLog(SessionFactoryUtils.class);
@ -103,17 +93,6 @@ public abstract class SessionFactoryUtils {
return null;
}
/**
* Obtain a new Session from the given SessionFactory.
* <p>Bridges between Hibernate signature differences.
* @param sessionFactory the SessionFactory to use
* @return the new Session
* @see org.hibernate.SessionFactory#openSession()
*/
public static Session openSession(SessionFactory sessionFactory) {
return (Session) ReflectionUtils.invokeMethod(openSessionMethod, sessionFactory);
}
/**
* Perform actual closing of the Hibernate Session,
* catching and logging any cleanup exceptions thrown.

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -203,7 +203,7 @@ public class OpenSessionInViewFilter extends OncePerRequestFilter {
*/
protected Session openSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException {
try {
Session session = SessionFactoryUtils.openSession(sessionFactory);
Session session = sessionFactory.openSession();
session.setFlushMode(FlushMode.MANUAL);
return session;
}

View File

@ -184,7 +184,7 @@ public class OpenSessionInViewInterceptor implements AsyncWebRequestInterceptor
*/
protected Session openSession() throws DataAccessResourceFailureException {
try {
Session session = SessionFactoryUtils.openSession(getSessionFactory());
Session session = getSessionFactory().openSession();
session.setFlushMode(FlushMode.MANUAL);
return session;
}

View File

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