diff --git a/spring-context/src/main/java/org/springframework/validation/beanvalidation/LocalValidatorFactoryBean.java b/spring-context/src/main/java/org/springframework/validation/beanvalidation/LocalValidatorFactoryBean.java
index 33d53bf922..9db0fec7dc 100644
--- a/spring-context/src/main/java/org/springframework/validation/beanvalidation/LocalValidatorFactoryBean.java
+++ b/spring-context/src/main/java/org/springframework/validation/beanvalidation/LocalValidatorFactoryBean.java
@@ -24,11 +24,13 @@ import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.validation.Configuration;
import javax.validation.ConstraintValidatorFactory;
import javax.validation.MessageInterpolator;
+import javax.validation.ParameterNameProvider;
import javax.validation.TraversableResolver;
import javax.validation.Validation;
import javax.validation.ValidationProviderResolver;
@@ -66,14 +68,8 @@ import org.springframework.util.ReflectionUtils;
* you will almost always use the default Validator anyway. This can also be injected directly
* into any target dependency of type {@link org.springframework.validation.Validator}!
*
- *
As of Spring 4.0, this class supports Bean Validation 1.0 and 1.1, with special support
- * for Hibernate Validator 4.3 and 5.x (see {@link #setValidationMessageSource}).
- *
- *
Note that Bean Validation 1.1's {@code #forExecutables} method isn't supported: We do not
- * expect that method to be called by application code; consider {@link MethodValidationInterceptor}
- * instead. If you really need programmatic {@code #forExecutables} access, inject this class as
- * a {@link ValidatorFactory} and call {@link #getValidator()} on it, then {@code #forExecutables}
- * on the returned native {@link Validator} reference instead of directly on this class.
+ *
As of Spring 5.0, this class requires Bean Validation 1.1, with special support
+ * for Hibernate Validator 5.x (see {@link #setValidationMessageSource}).
*
*
This class is also being used by Spring's MVC configuration namespace, in case of the
* {@code javax.validation} API being present but no explicit Validator having been configured.
@@ -88,10 +84,6 @@ import org.springframework.util.ReflectionUtils;
public class LocalValidatorFactoryBean extends SpringValidatorAdapter
implements ValidatorFactory, ApplicationContextAware, InitializingBean, DisposableBean {
- // Bean Validation 1.1 close() method available?
- private static final Method closeMethod = ClassUtils.getMethodIfAvailable(ValidatorFactory.class, "close");
-
-
@SuppressWarnings("rawtypes")
private Class providerClass;
@@ -305,55 +297,23 @@ public class LocalValidatorFactoryBean extends SpringValidatorAdapter
}
private void configureParameterNameProviderIfPossible(Configuration> configuration) {
- try {
- Class> parameterNameProviderClass =
- ClassUtils.forName("javax.validation.ParameterNameProvider", getClass().getClassLoader());
- Method parameterNameProviderMethod =
- Configuration.class.getMethod("parameterNameProvider", parameterNameProviderClass);
- final Object defaultProvider = ReflectionUtils.invokeMethod(
- Configuration.class.getMethod("getDefaultParameterNameProvider"), configuration);
- final ParameterNameDiscoverer discoverer = this.parameterNameDiscoverer;
- Object parameterNameProvider = Proxy.newProxyInstance(getClass().getClassLoader(),
- new Class>[] {parameterNameProviderClass}, new InvocationHandler() {
- @Override
- public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
- if (method.getName().equals("getParameterNames")) {
- String[] result = null;
- if (args[0] instanceof Constructor) {
- result = discoverer.getParameterNames((Constructor>) args[0]);
- }
- else if (args[0] instanceof Method) {
- result = discoverer.getParameterNames((Method) args[0]);
- }
- if (result != null) {
- return Arrays.asList(result);
- }
- else {
- try {
- return method.invoke(defaultProvider, args);
- }
- catch (InvocationTargetException ex) {
- throw ex.getTargetException();
- }
- }
- }
- else {
- // toString, equals, hashCode
- try {
- return method.invoke(this, args);
- }
- catch (InvocationTargetException ex) {
- throw ex.getTargetException();
- }
- }
- }
- });
- ReflectionUtils.invokeMethod(parameterNameProviderMethod, configuration, parameterNameProvider);
-
- }
- catch (Exception ex) {
- // Bean Validation 1.1 API not available - simply not applying the ParameterNameDiscoverer
- }
+ // TODO: inner class
+ final ParameterNameDiscoverer discoverer = this.parameterNameDiscoverer;
+ final ParameterNameProvider defaultProvider = configuration.getDefaultParameterNameProvider();
+ configuration.parameterNameProvider(new ParameterNameProvider() {
+ @Override
+ public List getParameterNames(Constructor> constructor) {
+ String[] paramNames = discoverer.getParameterNames(constructor);
+ return (paramNames != null ? Arrays.asList(paramNames) :
+ defaultProvider.getParameterNames(constructor));
+ }
+ @Override
+ public List getParameterNames(Method method) {
+ String[] paramNames = discoverer.getParameterNames(method);
+ return (paramNames != null ? Arrays.asList(paramNames) :
+ defaultProvider.getParameterNames(method));
+ }
+ });
}
/**
@@ -397,9 +357,14 @@ public class LocalValidatorFactoryBean extends SpringValidatorAdapter
return this.validatorFactory.getConstraintValidatorFactory();
}
+ @Override
+ public ParameterNameProvider getParameterNameProvider() {
+ return this.validatorFactory.getParameterNameProvider();
+ }
+
public void close() {
- if (closeMethod != null && this.validatorFactory != null) {
- ReflectionUtils.invokeMethod(closeMethod, this.validatorFactory);
+ if (this.validatorFactory != null) {
+ this.validatorFactory.close();
}
}
diff --git a/spring-context/src/main/java/org/springframework/validation/beanvalidation/MethodValidationInterceptor.java b/spring-context/src/main/java/org/springframework/validation/beanvalidation/MethodValidationInterceptor.java
index 697944e69a..7edbacc1b5 100644
--- a/spring-context/src/main/java/org/springframework/validation/beanvalidation/MethodValidationInterceptor.java
+++ b/spring-context/src/main/java/org/springframework/validation/beanvalidation/MethodValidationInterceptor.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2015 the original author or authors.
+ * Copyright 2002-2016 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.
@@ -26,7 +26,6 @@ import javax.validation.ValidatorFactory;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
-import org.hibernate.validator.HibernateValidator;
import org.springframework.core.BridgeMethodResolver;
import org.springframework.core.annotation.AnnotationUtils;
@@ -48,9 +47,8 @@ import org.springframework.validation.annotation.Validated;
* at the type level of the containing target class, applying to all public service methods
* of that class. By default, JSR-303 will validate against its default group only.
*
- * As of Spring 4.0, this functionality requires either a Bean Validation 1.1 provider
- * (such as Hibernate Validator 5.x) or the Bean Validation 1.0 API with Hibernate Validator
- * 4.3. The actual provider will be autodetected and automatically adapted.
+ *
As of Spring 5.0, this functionality requires a Bean Validation 1.1 provider
+ * (such as Hibernate Validator 5.x).
*
* @author Juergen Hoeller
* @since 3.1
@@ -88,8 +86,7 @@ public class MethodValidationInterceptor implements MethodInterceptor {
* Create a new MethodValidationInterceptor using a default JSR-303 validator underneath.
*/
public MethodValidationInterceptor() {
- this(forExecutablesMethod != null ? Validation.buildDefaultValidatorFactory() :
- HibernateValidatorDelegate.buildValidatorFactory());
+ this(Validation.buildDefaultValidatorFactory());
}
/**
@@ -114,43 +111,36 @@ public class MethodValidationInterceptor implements MethodInterceptor {
public Object invoke(MethodInvocation invocation) throws Throwable {
Class>[] groups = determineValidationGroups(invocation);
- if (forExecutablesMethod != null) {
- // Standard Bean Validation 1.1 API
- Object execVal = ReflectionUtils.invokeMethod(forExecutablesMethod, this.validator);
- Method methodToValidate = invocation.getMethod();
- Set> result;
+ // Standard Bean Validation 1.1 API
+ Object execVal = ReflectionUtils.invokeMethod(forExecutablesMethod, this.validator);
+ Method methodToValidate = invocation.getMethod();
+ Set> result;
- try {
- result = (Set>) ReflectionUtils.invokeMethod(validateParametersMethod,
- execVal, invocation.getThis(), methodToValidate, invocation.getArguments(), groups);
- }
- catch (IllegalArgumentException ex) {
- // Probably a generic type mismatch between interface and impl as reported in SPR-12237 / HV-1011
- // Let's try to find the bridged method on the implementation class...
- methodToValidate = BridgeMethodResolver.findBridgedMethod(
- ClassUtils.getMostSpecificMethod(invocation.getMethod(), invocation.getThis().getClass()));
- result = (Set>) ReflectionUtils.invokeMethod(validateParametersMethod,
- execVal, invocation.getThis(), methodToValidate, invocation.getArguments(), groups);
- }
- if (!result.isEmpty()) {
- throw new ConstraintViolationException(result);
- }
-
- Object returnValue = invocation.proceed();
-
- result = (Set>) ReflectionUtils.invokeMethod(validateReturnValueMethod,
- execVal, invocation.getThis(), methodToValidate, returnValue, groups);
- if (!result.isEmpty()) {
- throw new ConstraintViolationException(result);
- }
-
- return returnValue;
+ try {
+ result = (Set>) ReflectionUtils.invokeMethod(validateParametersMethod,
+ execVal, invocation.getThis(), methodToValidate, invocation.getArguments(), groups);
+ }
+ catch (IllegalArgumentException ex) {
+ // Probably a generic type mismatch between interface and impl as reported in SPR-12237 / HV-1011
+ // Let's try to find the bridged method on the implementation class...
+ methodToValidate = BridgeMethodResolver.findBridgedMethod(
+ ClassUtils.getMostSpecificMethod(invocation.getMethod(), invocation.getThis().getClass()));
+ result = (Set>) ReflectionUtils.invokeMethod(validateParametersMethod,
+ execVal, invocation.getThis(), methodToValidate, invocation.getArguments(), groups);
+ }
+ if (!result.isEmpty()) {
+ throw new ConstraintViolationException(result);
}
- else {
- // Hibernate Validator 4.3's native API
- return HibernateValidatorDelegate.invokeWithinValidation(invocation, this.validator, groups);
+ Object returnValue = invocation.proceed();
+
+ result = (Set>) ReflectionUtils.invokeMethod(validateReturnValueMethod,
+ execVal, invocation.getThis(), methodToValidate, returnValue, groups);
+ if (!result.isEmpty()) {
+ throw new ConstraintViolationException(result);
}
+
+ return returnValue;
}
/**
@@ -168,36 +158,4 @@ public class MethodValidationInterceptor implements MethodInterceptor {
return (validatedAnn != null ? validatedAnn.value() : new Class>[0]);
}
-
- /**
- * Inner class to avoid a hard-coded Hibernate Validator 4.3 dependency.
- */
- private static class HibernateValidatorDelegate {
-
- public static ValidatorFactory buildValidatorFactory() {
- return Validation.byProvider(HibernateValidator.class).configure().buildValidatorFactory();
- }
-
- @SuppressWarnings("deprecation")
- public static Object invokeWithinValidation(MethodInvocation invocation, Validator validator, Class>[] groups)
- throws Throwable {
-
- org.hibernate.validator.method.MethodValidator methodValidator =
- validator.unwrap(org.hibernate.validator.method.MethodValidator.class);
- Set> result =
- methodValidator.validateAllParameters(
- invocation.getThis(), invocation.getMethod(), invocation.getArguments(), groups);
- if (!result.isEmpty()) {
- throw new org.hibernate.validator.method.MethodConstraintViolationException(result);
- }
- Object returnValue = invocation.proceed();
- result = methodValidator.validateReturnValue(
- invocation.getThis(), invocation.getMethod(), returnValue, groups);
- if (!result.isEmpty()) {
- throw new org.hibernate.validator.method.MethodConstraintViolationException(result);
- }
- return returnValue;
- }
- }
-
}
diff --git a/spring-context/src/main/java/org/springframework/validation/beanvalidation/SpringValidatorAdapter.java b/spring-context/src/main/java/org/springframework/validation/beanvalidation/SpringValidatorAdapter.java
index 6d46113d83..7640c52c63 100644
--- a/spring-context/src/main/java/org/springframework/validation/beanvalidation/SpringValidatorAdapter.java
+++ b/spring-context/src/main/java/org/springframework/validation/beanvalidation/SpringValidatorAdapter.java
@@ -24,6 +24,7 @@ import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import javax.validation.ConstraintViolation;
+import javax.validation.executable.ExecutableValidator;
import javax.validation.metadata.BeanDescriptor;
import javax.validation.metadata.ConstraintDescriptor;
@@ -299,6 +300,11 @@ public class SpringValidatorAdapter implements SmartValidator, javax.validation.
return (type != null ? this.targetValidator.unwrap(type) : (T) this.targetValidator);
}
+ @Override
+ public ExecutableValidator forExecutables() {
+ return this.targetValidator.forExecutables();
+ }
+
/**
* Wrapper for a String attribute which can be resolved via a {@code MessageSource},
diff --git a/spring-context/src/test/java/org/springframework/validation/beanvalidation/BeanValidationPostProcessorTests.java b/spring-context/src/test/java/org/springframework/validation/beanvalidation/BeanValidationPostProcessorTests.java
index bfe908f88d..6ebc2a0474 100644
--- a/spring-context/src/test/java/org/springframework/validation/beanvalidation/BeanValidationPostProcessorTests.java
+++ b/spring-context/src/test/java/org/springframework/validation/beanvalidation/BeanValidationPostProcessorTests.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2014 the original author or authors.
+ * Copyright 2002-2015 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.
@@ -31,10 +31,9 @@ import org.springframework.tests.sample.beans.TestBean;
import static org.junit.Assert.*;
/**
- * Tested against Hibernate Validator 4.3, as of Spring 4.0.
+ * Tested against Hibernate Validator 5.x.
*
* @author Juergen Hoeller
- * @since 3.0
*/
public class BeanValidationPostProcessorTests {
@@ -52,6 +51,7 @@ public class BeanValidationPostProcessorTests {
assertTrue(ex.getRootCause().getMessage().contains("testBean"));
assertTrue(ex.getRootCause().getMessage().contains("invalid"));
}
+ ac.close();
}
@Test
@@ -63,6 +63,7 @@ public class BeanValidationPostProcessorTests {
bd.getPropertyValues().add("testBean", new TestBean());
ac.registerBeanDefinition("bean", bd);
ac.refresh();
+ ac.close();
}
@Test
@@ -74,6 +75,7 @@ public class BeanValidationPostProcessorTests {
ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
ac.registerBeanDefinition("bean", new RootBeanDefinition(AfterInitConstraintBean.class));
ac.refresh();
+ ac.close();
}
@Test
@@ -92,6 +94,7 @@ public class BeanValidationPostProcessorTests {
assertTrue(ex.getRootCause().getMessage().contains("stringValue"));
assertTrue(ex.getRootCause().getMessage().contains("invalid"));
}
+ ac.close();
}
@Test
@@ -103,6 +106,7 @@ public class BeanValidationPostProcessorTests {
bd.getPropertyValues().add("stringValue", "ss");
ac.registerBeanDefinition("bean", bd);
ac.refresh();
+ ac.close();
}
diff --git a/spring-context/src/test/java/org/springframework/validation/beanvalidation/MethodValidationTests.java b/spring-context/src/test/java/org/springframework/validation/beanvalidation/MethodValidationTests.java
index 812b1d9ae1..a383d81b16 100644
--- a/spring-context/src/test/java/org/springframework/validation/beanvalidation/MethodValidationTests.java
+++ b/spring-context/src/test/java/org/springframework/validation/beanvalidation/MethodValidationTests.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2015 the original author or authors.
+ * Copyright 2002-2016 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.
@@ -35,11 +35,11 @@ import org.springframework.validation.annotation.Validated;
import static org.junit.Assert.*;
/**
- * Tested against Hibernate Validator 4.3, as of Spring 4.0.
+ * Tests against Hibernate Validator 5.x.
*
* @author Juergen Hoeller
- * @since 3.1
*/
+@SuppressWarnings("rawtypes")
public class MethodValidationTests {
@Test
@@ -65,6 +65,7 @@ public class MethodValidationTests {
}
+ @SuppressWarnings("unchecked")
private void doTestProxyValidation(MyValidInterface proxy) {
assertNotNull(proxy.myValidMethod("value", 5));
try {
diff --git a/spring-context/src/test/java/org/springframework/validation/beanvalidation/ValidatorFactoryTests.java b/spring-context/src/test/java/org/springframework/validation/beanvalidation/ValidatorFactoryTests.java
index 26138af537..144c6fd98f 100644
--- a/spring-context/src/test/java/org/springframework/validation/beanvalidation/ValidatorFactoryTests.java
+++ b/spring-context/src/test/java/org/springframework/validation/beanvalidation/ValidatorFactoryTests.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2014 the original author or authors.
+ * Copyright 2002-2015 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.
@@ -38,6 +38,10 @@ import javax.validation.constraints.NotNull;
import org.hibernate.validator.HibernateValidator;
import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.ConfigurableApplicationContext;
+import org.springframework.context.annotation.AnnotationConfigApplicationContext;
+import org.springframework.core.env.Environment;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
@@ -47,10 +51,9 @@ import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
/**
- * Tested against Hibernate Validator 4.3, as of Spring 4.0.
+ * Tests against Hibernate Validator 5.x.
*
* @author Juergen Hoeller
- * @since 3.0
*/
public class ValidatorFactoryTests {
@@ -58,6 +61,7 @@ public class ValidatorFactoryTests {
public void testSimpleValidation() throws Exception {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.afterPropertiesSet();
+
ValidPerson person = new ValidPerson();
Set> result = validator.validate(person);
assertEquals(2, result.size());
@@ -78,6 +82,7 @@ public class ValidatorFactoryTests {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.setProviderClass(HibernateValidator.class);
validator.afterPropertiesSet();
+
ValidPerson person = new ValidPerson();
Set> result = validator.validate(person);
assertEquals(2, result.size());
@@ -112,6 +117,7 @@ public class ValidatorFactoryTests {
public void testSpringValidationFieldType() throws Exception {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.afterPropertiesSet();
+
ValidPerson person = new ValidPerson();
person.setName("Phil");
person.getAddress().setStreet("Phil's Street");
@@ -126,6 +132,7 @@ public class ValidatorFactoryTests {
public void testSpringValidation() throws Exception {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.afterPropertiesSet();
+
ValidPerson person = new ValidPerson();
BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
validator.validate(person, result);
@@ -153,6 +160,7 @@ public class ValidatorFactoryTests {
public void testSpringValidationWithClassLevel() throws Exception {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.afterPropertiesSet();
+
ValidPerson person = new ValidPerson();
person.setName("Juergen");
person.getAddress().setStreet("Juergen's Street");
@@ -166,10 +174,32 @@ public class ValidatorFactoryTests {
assertTrue(errorCodes.contains("NameAddressValid"));
}
+ @Test
+ public void testSpringValidationWithAutowiredValidator() throws Exception {
+ ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(
+ LocalValidatorFactoryBean.class);
+ LocalValidatorFactoryBean validator = ctx.getBean(LocalValidatorFactoryBean.class);
+
+ ValidPerson person = new ValidPerson();
+ person.expectsAutowiredValidator = true;
+ person.setName("Juergen");
+ person.getAddress().setStreet("Juergen's Street");
+ BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
+ validator.validate(person, result);
+ assertEquals(1, result.getErrorCount());
+ ObjectError globalError = result.getGlobalError();
+ List errorCodes = Arrays.asList(globalError.getCodes());
+ assertEquals(2, errorCodes.size());
+ assertTrue(errorCodes.contains("NameAddressValid.person"));
+ assertTrue(errorCodes.contains("NameAddressValid"));
+ ctx.close();
+ }
+
@Test
public void testSpringValidationWithErrorInListElement() throws Exception {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.afterPropertiesSet();
+
ValidPerson person = new ValidPerson();
person.getAddressList().add(new ValidAddress());
BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
@@ -187,6 +217,7 @@ public class ValidatorFactoryTests {
public void testSpringValidationWithErrorInSetElement() throws Exception {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.afterPropertiesSet();
+
ValidPerson person = new ValidPerson();
person.getAddressSet().add(new ValidAddress());
BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
@@ -240,6 +271,8 @@ public class ValidatorFactoryTests {
@Valid
private Set addressSet = new LinkedHashSet();
+ public boolean expectsAutowiredValidator = false;
+
public String getName() {
return name;
}
@@ -304,16 +337,22 @@ public class ValidatorFactoryTests {
public static class NameAddressValidator implements ConstraintValidator {
+ @Autowired
+ private Environment environment;
+
@Override
public void initialize(NameAddressValid constraintAnnotation) {
}
@Override
public boolean isValid(ValidPerson value, ConstraintValidatorContext context) {
+ if (value.expectsAutowiredValidator) {
+ assertNotNull(this.environment);
+ }
boolean valid = (value.name == null || !value.address.street.contains(value.name));
if (!valid && "Phil".equals(value.name)) {
context.buildConstraintViolationWithTemplate(
- context.getDefaultConstraintMessageTemplate()).addNode("address").addConstraintViolation().disableDefaultConstraintViolation();
+ context.getDefaultConstraintMessageTemplate()).addPropertyNode("address").addConstraintViolation().disableDefaultConstraintViolation();
}
return valid;
}
@@ -378,7 +417,7 @@ public class ValidatorFactoryTests {
public boolean isValid(InnerBean bean, ConstraintValidatorContext context) {
context.disableDefaultConstraintViolation();
if (bean.getValue() == null) {
- context.buildConstraintViolationWithTemplate("NULL"). addNode("value").addConstraintViolation();
+ context.buildConstraintViolationWithTemplate("NULL").addPropertyNode("value").addConstraintViolation();
return false;
}
return true;
diff --git a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/ConfigurableJtaPlatform.java b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/ConfigurableJtaPlatform.java
deleted file mode 100644
index 7942e65a3f..0000000000
--- a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/ConfigurableJtaPlatform.java
+++ /dev/null
@@ -1,161 +0,0 @@
-/*
- * Copyright 2002-2014 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate4;
-
-import java.lang.reflect.InvocationHandler;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.lang.reflect.Proxy;
-import javax.transaction.Status;
-import javax.transaction.Synchronization;
-import javax.transaction.SystemException;
-import javax.transaction.Transaction;
-import javax.transaction.TransactionManager;
-import javax.transaction.TransactionSynchronizationRegistry;
-import javax.transaction.UserTransaction;
-
-import org.hibernate.TransactionException;
-import org.hibernate.service.Service;
-
-import org.springframework.transaction.jta.UserTransactionAdapter;
-import org.springframework.util.Assert;
-import org.springframework.util.ClassUtils;
-
-/**
- * Implementation of Hibernate 4's JtaPlatform SPI (which has a different package
- * location in Hibernate 4.0-4.2 vs 4.3), exposing passed-in {@link TransactionManager},
- * {@link UserTransaction} and {@link TransactionSynchronizationRegistry} references.
- *
- * @author Juergen Hoeller
- * @since 3.1.2
- */
-@SuppressWarnings({"serial", "unchecked"})
-class ConfigurableJtaPlatform implements InvocationHandler {
-
- static final Class extends Service> jtaPlatformClass;
-
- static {
- Class> jpClass;
- try {
- // Try Hibernate 4.0-4.2 JtaPlatform variant
- jpClass = ClassUtils.forName("org.hibernate.service.jta.platform.spi.JtaPlatform",
- ConfigurableJtaPlatform.class.getClassLoader());
- }
- catch (ClassNotFoundException ex) {
- try {
- // Try Hibernate 4.3 JtaPlatform variant
- jpClass = ClassUtils.forName("org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform",
- ConfigurableJtaPlatform.class.getClassLoader());
- }
- catch (ClassNotFoundException ex2) {
- throw new IllegalStateException("Neither Hibernate 4.0-4.2 nor 4.3 variant of JtaPlatform found");
- }
- }
- jtaPlatformClass = (Class extends Service>) jpClass;
- }
-
- static String getJtaPlatformBasePackage() {
- String className = jtaPlatformClass.getName();
- return className.substring(0, className.length() - "spi.JtaPlatform".length());
- }
-
-
- private final TransactionManager transactionManager;
-
- private final UserTransaction userTransaction;
-
- private final TransactionSynchronizationRegistry transactionSynchronizationRegistry;
-
-
- /**
- * Create a new ConfigurableJtaPlatform instance with the given
- * JTA TransactionManager and optionally a given UserTransaction.
- * @param tm the JTA TransactionManager reference (required)
- * @param ut the JTA UserTransaction reference (optional)
- * @param tsr the JTA 1.1 TransactionSynchronizationRegistry (optional)
- */
- public ConfigurableJtaPlatform(TransactionManager tm, UserTransaction ut, TransactionSynchronizationRegistry tsr) {
- Assert.notNull(tm, "TransactionManager reference must not be null");
- this.transactionManager = tm;
- this.userTransaction = (ut != null ? ut : new UserTransactionAdapter(tm));
- this.transactionSynchronizationRegistry = tsr;
- }
-
-
- public TransactionManager retrieveTransactionManager() {
- return this.transactionManager;
- }
-
- public UserTransaction retrieveUserTransaction() {
- return this.userTransaction;
- }
-
- public Object getTransactionIdentifier(Transaction transaction) {
- return transaction;
- }
-
- public boolean canRegisterSynchronization() {
- try {
- return (this.transactionManager.getStatus() == Status.STATUS_ACTIVE);
- }
- catch (SystemException ex) {
- throw new TransactionException("Could not determine JTA transaction status", ex);
- }
- }
-
- public void registerSynchronization(Synchronization synchronization) {
- if (this.transactionSynchronizationRegistry != null) {
- this.transactionSynchronizationRegistry.registerInterposedSynchronization(synchronization);
- }
- else {
- try {
- this.transactionManager.getTransaction().registerSynchronization(synchronization);
- }
- catch (Exception ex) {
- throw new TransactionException("Could not access JTA Transaction to register synchronization", ex);
- }
- }
- }
-
- public int getCurrentStatus() throws SystemException {
- return this.transactionManager.getStatus();
- }
-
-
- @Override
- public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
- try {
- return getClass().getMethod(method.getName(), method.getParameterTypes()).invoke(this, args);
- }
- catch (InvocationTargetException ex) {
- throw ex.getTargetException();
- }
- catch (Throwable ex) {
- throw new IllegalStateException("Failed to delegate to corresponding implementation method", ex);
- }
- }
-
- /**
- * Obtain a proxy that implements the current Hibernate version's JtaPlatform interface
- * in the right package location, delegating all invocations to the same-named methods
- * on this ConfigurableJtaPlatform class itself.
- */
- public Object getJtaPlatformProxy() {
- return Proxy.newProxyInstance(getClass().getClassLoader(), new Class>[] {jtaPlatformClass}, this);
- }
-
-}
diff --git a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateCallback.java b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateCallback.java
deleted file mode 100644
index 66f78d6678..0000000000
--- a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateCallback.java
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * Copyright 2002-2016 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate4;
-
-import org.hibernate.HibernateException;
-import org.hibernate.Session;
-
-/**
- * Callback interface for Hibernate code. To be used with {@link HibernateTemplate}'s
- * execution methods, often as anonymous classes within a method implementation.
- * A typical implementation will call {@code Session.load/find/update} to perform
- * some operations on persistent objects.
- *
- * @author Juergen Hoeller
- * @since 4.0.1
- * @see HibernateTemplate
- * @see HibernateTransactionManager
- */
-public interface HibernateCallback {
-
- /**
- * Gets called by {@code HibernateTemplate.execute} with an active
- * Hibernate {@code Session}. Does not need to care about activating
- * or closing the {@code Session}, or handling transactions.
- * Allows for returning a result object created within the callback,
- * i.e. a domain object or a collection of domain objects.
- * A thrown custom RuntimeException is treated as an application exception:
- * It gets propagated to the caller of the template.
- * @param session active Hibernate session
- * @return a result object, or {@code null} if none
- * @throws HibernateException if thrown by the Hibernate API
- * @see HibernateTemplate#execute
- */
- T doInHibernate(Session session) throws HibernateException;
-
-}
diff --git a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateExceptionTranslator.java b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateExceptionTranslator.java
deleted file mode 100644
index 7223e8d7da..0000000000
--- a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateExceptionTranslator.java
+++ /dev/null
@@ -1,60 +0,0 @@
-/*
- * 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate4;
-
-import org.hibernate.HibernateException;
-
-import org.springframework.dao.DataAccessException;
-import org.springframework.dao.support.PersistenceExceptionTranslator;
-
-/**
- * {@link PersistenceExceptionTranslator} capable of translating {@link HibernateException}
- * instances to Spring's {@link DataAccessException} hierarchy.
- *
- *
Extended by {@link LocalSessionFactoryBean}, so there is no need to declare this
- * translator in addition to a {@code LocalSessionFactoryBean}.
- *
- *
When configuring the container with {@code @Configuration} classes, a {@code @Bean}
- * of this type must be registered manually.
- *
- * @author Juergen Hoeller
- * @since 3.1
- * @see org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor
- * @see SessionFactoryUtils#convertHibernateAccessException(HibernateException)
- */
-public class HibernateExceptionTranslator implements PersistenceExceptionTranslator {
-
- @Override
- public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
- if (ex instanceof HibernateException) {
- return convertHibernateAccessException((HibernateException) ex);
- }
- return null;
- }
-
- /**
- * Convert the given HibernateException to an appropriate exception from the
- * {@code org.springframework.dao} hierarchy.
- * @param ex HibernateException that occured
- * @return a corresponding DataAccessException
- * @see SessionFactoryUtils#convertHibernateAccessException
- */
- protected DataAccessException convertHibernateAccessException(HibernateException ex) {
- return SessionFactoryUtils.convertHibernateAccessException(ex);
- }
-
-}
diff --git a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateJdbcException.java b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateJdbcException.java
deleted file mode 100644
index 96c1a5f4b2..0000000000
--- a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateJdbcException.java
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate4;
-
-import java.sql.SQLException;
-
-import org.hibernate.JDBCException;
-
-import org.springframework.dao.UncategorizedDataAccessException;
-
-/**
- * Hibernate-specific subclass of UncategorizedDataAccessException,
- * for JDBC exceptions that Hibernate wrapped.
- *
- * @author Juergen Hoeller
- * @since 3.1
- * @see SessionFactoryUtils#convertHibernateAccessException
- */
-@SuppressWarnings("serial")
-public class HibernateJdbcException extends UncategorizedDataAccessException {
-
- public HibernateJdbcException(JDBCException ex) {
- super("JDBC exception on Hibernate data access: SQLException for SQL [" + ex.getSQL() + "]; SQL state [" +
- ex.getSQLState() + "]; error code [" + ex.getErrorCode() + "]; " + ex.getMessage(), ex);
- }
-
- /**
- * Return the underlying SQLException.
- */
- public SQLException getSQLException() {
- return ((JDBCException) getCause()).getSQLException();
- }
-
- /**
- * Return the SQL that led to the problem.
- */
- public String getSql() {
- return ((JDBCException) getCause()).getSQL();
- }
-
-}
diff --git a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateObjectRetrievalFailureException.java b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateObjectRetrievalFailureException.java
deleted file mode 100644
index ce496de76d..0000000000
--- a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateObjectRetrievalFailureException.java
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate4;
-
-import org.hibernate.UnresolvableObjectException;
-import org.hibernate.WrongClassException;
-
-import org.springframework.orm.ObjectRetrievalFailureException;
-
-/**
- * Hibernate-specific subclass of ObjectRetrievalFailureException.
- * Converts Hibernate's UnresolvableObjectException and WrongClassException.
- *
- * @author Juergen Hoeller
- * @since 3.1
- * @see SessionFactoryUtils#convertHibernateAccessException
- */
-@SuppressWarnings("serial")
-public class HibernateObjectRetrievalFailureException extends ObjectRetrievalFailureException {
-
- public HibernateObjectRetrievalFailureException(UnresolvableObjectException ex) {
- super(ex.getEntityName(), ex.getIdentifier(), ex.getMessage(), ex);
- }
-
- public HibernateObjectRetrievalFailureException(WrongClassException ex) {
- super(ex.getEntityName(), ex.getIdentifier(), ex.getMessage(), ex);
- }
-
-}
diff --git a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateOperations.java b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateOperations.java
deleted file mode 100644
index a4858909a8..0000000000
--- a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateOperations.java
+++ /dev/null
@@ -1,795 +0,0 @@
-/*
- * Copyright 2002-2014 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate4;
-
-import java.io.Serializable;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.List;
-
-import org.hibernate.Filter;
-import org.hibernate.LockMode;
-import org.hibernate.ReplicationMode;
-import org.hibernate.criterion.DetachedCriteria;
-
-import org.springframework.dao.DataAccessException;
-
-/**
- * Interface that specifies a basic set of Hibernate operations,
- * implemented by {@link HibernateTemplate}. Not often used, but a useful
- * option to enhance testability, as it can easily be mocked or stubbed.
- *
- *
Defines {@code HibernateTemplate}'s data access methods that
- * mirror various {@link org.hibernate.Session} methods. Users are
- * strongly encouraged to read the Hibernate {@code Session} javadocs
- * for details on the semantics of those methods.
- *
- * @author Juergen Hoeller
- * @since 4.0.1
- * @see HibernateTemplate
- * @see org.hibernate.Session
- * @see HibernateTransactionManager
- */
-public interface HibernateOperations {
-
- /**
- * Execute the action specified by the given action object within a
- * {@link org.hibernate.Session}.
- *
Application exceptions thrown by the action object get propagated
- * to the caller (can only be unchecked). Hibernate exceptions are
- * transformed into appropriate DAO ones. Allows for returning a result
- * object, that is a domain object or a collection of domain objects.
- *
Note: Callback code is not supposed to handle transactions itself!
- * Use an appropriate transaction manager like
- * {@link HibernateTransactionManager}. Generally, callback code must not
- * touch any {@code Session} lifecycle methods, like close,
- * disconnect, or reconnect, to let the template do its work.
- * @param action callback object that specifies the Hibernate action
- * @return a result object returned by the action, or {@code null}
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see HibernateTransactionManager
- * @see org.hibernate.Session
- */
- T execute(HibernateCallback action) throws DataAccessException;
-
-
- //-------------------------------------------------------------------------
- // Convenience methods for loading individual objects
- //-------------------------------------------------------------------------
-
- /**
- * Return the persistent instance of the given entity class
- * with the given identifier, or {@code null} if not found.
- * This method is a thin wrapper around
- * {@link org.hibernate.Session#get(Class, java.io.Serializable)} for convenience.
- * For an explanation of the exact semantics of this method, please do refer to
- * the Hibernate API documentation in the first instance.
- * @param entityClass a persistent class
- * @param id the identifier of the persistent instance
- * @return the persistent instance, or {@code null} if not found
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#get(Class, java.io.Serializable)
- */
- T get(Class entityClass, Serializable id) throws DataAccessException;
-
- /**
- * Return the persistent instance of the given entity class
- * with the given identifier, or {@code null} if not found.
- * Obtains the specified lock mode if the instance exists.
- *
This method is a thin wrapper around
- * {@link org.hibernate.Session#get(Class, java.io.Serializable, LockMode)} for convenience.
- * For an explanation of the exact semantics of this method, please do refer to
- * the Hibernate API documentation in the first instance.
- * @param entityClass a persistent class
- * @param id the identifier of the persistent instance
- * @param lockMode the lock mode to obtain
- * @return the persistent instance, or {@code null} if not found
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#get(Class, java.io.Serializable, org.hibernate.LockMode)
- */
- T get(Class entityClass, Serializable id, LockMode lockMode) throws DataAccessException;
-
- /**
- * Return the persistent instance of the given entity class
- * with the given identifier, or {@code null} if not found.
- * This method is a thin wrapper around
- * {@link org.hibernate.Session#get(String, java.io.Serializable)} for convenience.
- * For an explanation of the exact semantics of this method, please do refer to
- * the Hibernate API documentation in the first instance.
- * @param entityName the name of the persistent entity
- * @param id the identifier of the persistent instance
- * @return the persistent instance, or {@code null} if not found
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#get(Class, java.io.Serializable)
- */
- Object get(String entityName, Serializable id) throws DataAccessException;
-
- /**
- * Return the persistent instance of the given entity class
- * with the given identifier, or {@code null} if not found.
- * Obtains the specified lock mode if the instance exists.
- *
This method is a thin wrapper around
- * {@link org.hibernate.Session#get(String, java.io.Serializable, LockMode)} for convenience.
- * For an explanation of the exact semantics of this method, please do refer to
- * the Hibernate API documentation in the first instance.
- * @param entityName the name of the persistent entity
- * @param id the identifier of the persistent instance
- * @param lockMode the lock mode to obtain
- * @return the persistent instance, or {@code null} if not found
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#get(Class, java.io.Serializable, org.hibernate.LockMode)
- */
- Object get(String entityName, Serializable id, LockMode lockMode) throws DataAccessException;
-
- /**
- * Return the persistent instance of the given entity class
- * with the given identifier, throwing an exception if not found.
- *
This method is a thin wrapper around
- * {@link org.hibernate.Session#load(Class, java.io.Serializable)} for convenience.
- * For an explanation of the exact semantics of this method, please do refer to
- * the Hibernate API documentation in the first instance.
- * @param entityClass a persistent class
- * @param id the identifier of the persistent instance
- * @return the persistent instance
- * @throws org.springframework.orm.ObjectRetrievalFailureException if not found
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#load(Class, java.io.Serializable)
- */
- T load(Class entityClass, Serializable id) throws DataAccessException;
-
- /**
- * Return the persistent instance of the given entity class
- * with the given identifier, throwing an exception if not found.
- * Obtains the specified lock mode if the instance exists.
- * This method is a thin wrapper around
- * {@link org.hibernate.Session#load(Class, java.io.Serializable, LockMode)} for convenience.
- * For an explanation of the exact semantics of this method, please do refer to
- * the Hibernate API documentation in the first instance.
- * @param entityClass a persistent class
- * @param id the identifier of the persistent instance
- * @param lockMode the lock mode to obtain
- * @return the persistent instance
- * @throws org.springframework.orm.ObjectRetrievalFailureException if not found
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#load(Class, java.io.Serializable)
- */
- T load(Class entityClass, Serializable id, LockMode lockMode) throws DataAccessException;
-
- /**
- * Return the persistent instance of the given entity class
- * with the given identifier, throwing an exception if not found.
- * This method is a thin wrapper around
- * {@link org.hibernate.Session#load(String, java.io.Serializable)} for convenience.
- * For an explanation of the exact semantics of this method, please do refer to
- * the Hibernate API documentation in the first instance.
- * @param entityName the name of the persistent entity
- * @param id the identifier of the persistent instance
- * @return the persistent instance
- * @throws org.springframework.orm.ObjectRetrievalFailureException if not found
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#load(Class, java.io.Serializable)
- */
- Object load(String entityName, Serializable id) throws DataAccessException;
-
- /**
- * Return the persistent instance of the given entity class
- * with the given identifier, throwing an exception if not found.
- *
Obtains the specified lock mode if the instance exists.
- *
This method is a thin wrapper around
- * {@link org.hibernate.Session#load(String, java.io.Serializable, LockMode)} for convenience.
- * For an explanation of the exact semantics of this method, please do refer to
- * the Hibernate API documentation in the first instance.
- * @param entityName the name of the persistent entity
- * @param id the identifier of the persistent instance
- * @param lockMode the lock mode to obtain
- * @return the persistent instance
- * @throws org.springframework.orm.ObjectRetrievalFailureException if not found
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#load(Class, java.io.Serializable)
- */
- Object load(String entityName, Serializable id, LockMode lockMode) throws DataAccessException;
-
- /**
- * Return all persistent instances of the given entity class.
- * Note: Use queries or criteria for retrieving a specific subset.
- * @param entityClass a persistent class
- * @return a {@link List} containing 0 or more persistent instances
- * @throws org.springframework.dao.DataAccessException if there is a Hibernate error
- * @see org.hibernate.Session#createCriteria
- */
- List loadAll(Class entityClass) throws DataAccessException;
-
- /**
- * Load the persistent instance with the given identifier
- * into the given object, throwing an exception if not found.
- * This method is a thin wrapper around
- * {@link org.hibernate.Session#load(Object, java.io.Serializable)} for convenience.
- * For an explanation of the exact semantics of this method, please do refer to
- * the Hibernate API documentation in the first instance.
- * @param entity the object (of the target class) to load into
- * @param id the identifier of the persistent instance
- * @throws org.springframework.orm.ObjectRetrievalFailureException if not found
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#load(Object, java.io.Serializable)
- */
- void load(Object entity, Serializable id) throws DataAccessException;
-
- /**
- * Re-read the state of the given persistent instance.
- * @param entity the persistent instance to re-read
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#refresh(Object)
- */
- void refresh(Object entity) throws DataAccessException;
-
- /**
- * Re-read the state of the given persistent instance.
- * Obtains the specified lock mode for the instance.
- * @param entity the persistent instance to re-read
- * @param lockMode the lock mode to obtain
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#refresh(Object, org.hibernate.LockMode)
- */
- void refresh(Object entity, LockMode lockMode) throws DataAccessException;
-
- /**
- * Check whether the given object is in the Session cache.
- * @param entity the persistence instance to check
- * @return whether the given object is in the Session cache
- * @throws org.springframework.dao.DataAccessException if there is a Hibernate error
- * @see org.hibernate.Session#contains
- */
- boolean contains(Object entity) throws DataAccessException;
-
- /**
- * Remove the given object from the {@link org.hibernate.Session} cache.
- * @param entity the persistent instance to evict
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#evict
- */
- void evict(Object entity) throws DataAccessException;
-
- /**
- * Force initialization of a Hibernate proxy or persistent collection.
- * @param proxy a proxy for a persistent object or a persistent collection
- * @throws DataAccessException if we can't initialize the proxy, for example
- * because it is not associated with an active Session
- * @see org.hibernate.Hibernate#initialize
- */
- void initialize(Object proxy) throws DataAccessException;
-
- /**
- * Return an enabled Hibernate {@link Filter} for the given filter name.
- * The returned {@code Filter} instance can be used to set filter parameters.
- * @param filterName the name of the filter
- * @return the enabled Hibernate {@code Filter} (either already
- * enabled or enabled on the fly by this operation)
- * @throws IllegalStateException if we are not running within a
- * transactional Session (in which case this operation does not make sense)
- */
- Filter enableFilter(String filterName) throws IllegalStateException;
-
-
- //-------------------------------------------------------------------------
- // Convenience methods for storing individual objects
- //-------------------------------------------------------------------------
-
- /**
- * Obtain the specified lock level upon the given object, implicitly
- * checking whether the corresponding database entry still exists.
- * @param entity the persistent instance to lock
- * @param lockMode the lock mode to obtain
- * @throws org.springframework.orm.ObjectOptimisticLockingFailureException if not found
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#lock(Object, org.hibernate.LockMode)
- */
- void lock(Object entity, LockMode lockMode) throws DataAccessException;
-
- /**
- * Obtain the specified lock level upon the given object, implicitly
- * checking whether the corresponding database entry still exists.
- * @param entityName the name of the persistent entity
- * @param entity the persistent instance to lock
- * @param lockMode the lock mode to obtain
- * @throws org.springframework.orm.ObjectOptimisticLockingFailureException if not found
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#lock(String, Object, org.hibernate.LockMode)
- */
- void lock(String entityName, Object entity, LockMode lockMode) throws DataAccessException;
-
- /**
- * Persist the given transient instance.
- * @param entity the transient instance to persist
- * @return the generated identifier
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#save(Object)
- */
- Serializable save(Object entity) throws DataAccessException;
-
- /**
- * Persist the given transient instance.
- * @param entityName the name of the persistent entity
- * @param entity the transient instance to persist
- * @return the generated identifier
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#save(String, Object)
- */
- Serializable save(String entityName, Object entity) throws DataAccessException;
-
- /**
- * Update the given persistent instance,
- * associating it with the current Hibernate {@link org.hibernate.Session}.
- * @param entity the persistent instance to update
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#update(Object)
- */
- void update(Object entity) throws DataAccessException;
-
- /**
- * Update the given persistent instance,
- * associating it with the current Hibernate {@link org.hibernate.Session}.
- *
Obtains the specified lock mode if the instance exists, implicitly
- * checking whether the corresponding database entry still exists.
- * @param entity the persistent instance to update
- * @param lockMode the lock mode to obtain
- * @throws org.springframework.orm.ObjectOptimisticLockingFailureException if not found
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#update(Object)
- */
- void update(Object entity, LockMode lockMode) throws DataAccessException;
-
- /**
- * Update the given persistent instance,
- * associating it with the current Hibernate {@link org.hibernate.Session}.
- * @param entityName the name of the persistent entity
- * @param entity the persistent instance to update
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#update(String, Object)
- */
- void update(String entityName, Object entity) throws DataAccessException;
-
- /**
- * Update the given persistent instance,
- * associating it with the current Hibernate {@link org.hibernate.Session}.
- *
Obtains the specified lock mode if the instance exists, implicitly
- * checking whether the corresponding database entry still exists.
- * @param entityName the name of the persistent entity
- * @param entity the persistent instance to update
- * @param lockMode the lock mode to obtain
- * @throws org.springframework.orm.ObjectOptimisticLockingFailureException if not found
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#update(String, Object)
- */
- void update(String entityName, Object entity, LockMode lockMode) throws DataAccessException;
-
- /**
- * Save or update the given persistent instance,
- * according to its id (matching the configured "unsaved-value"?).
- * Associates the instance with the current Hibernate {@link org.hibernate.Session}.
- * @param entity the persistent instance to save or update
- * (to be associated with the Hibernate {@code Session})
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#saveOrUpdate(Object)
- */
- void saveOrUpdate(Object entity) throws DataAccessException;
-
- /**
- * Save or update the given persistent instance,
- * according to its id (matching the configured "unsaved-value"?).
- * Associates the instance with the current Hibernate {@code Session}.
- * @param entityName the name of the persistent entity
- * @param entity the persistent instance to save or update
- * (to be associated with the Hibernate {@code Session})
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#saveOrUpdate(String, Object)
- */
- void saveOrUpdate(String entityName, Object entity) throws DataAccessException;
-
- /**
- * Persist the state of the given detached instance according to the
- * given replication mode, reusing the current identifier value.
- * @param entity the persistent object to replicate
- * @param replicationMode the Hibernate ReplicationMode
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#replicate(Object, org.hibernate.ReplicationMode)
- */
- void replicate(Object entity, ReplicationMode replicationMode) throws DataAccessException;
-
- /**
- * Persist the state of the given detached instance according to the
- * given replication mode, reusing the current identifier value.
- * @param entityName the name of the persistent entity
- * @param entity the persistent object to replicate
- * @param replicationMode the Hibernate ReplicationMode
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#replicate(String, Object, org.hibernate.ReplicationMode)
- */
- void replicate(String entityName, Object entity, ReplicationMode replicationMode) throws DataAccessException;
-
- /**
- * Persist the given transient instance. Follows JSR-220 semantics.
- *
Similar to {@code save}, associating the given object
- * with the current Hibernate {@link org.hibernate.Session}.
- * @param entity the persistent instance to persist
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#persist(Object)
- * @see #save
- */
- void persist(Object entity) throws DataAccessException;
-
- /**
- * Persist the given transient instance. Follows JSR-220 semantics.
- *
Similar to {@code save}, associating the given object
- * with the current Hibernate {@link org.hibernate.Session}.
- * @param entityName the name of the persistent entity
- * @param entity the persistent instance to persist
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#persist(String, Object)
- * @see #save
- */
- void persist(String entityName, Object entity) throws DataAccessException;
-
- /**
- * Copy the state of the given object onto the persistent object
- * with the same identifier. Follows JSR-220 semantics.
- *
Similar to {@code saveOrUpdate}, but never associates the given
- * object with the current Hibernate Session. In case of a new entity,
- * the state will be copied over as well.
- *
Note that {@code merge} will not update the identifiers
- * in the passed-in object graph (in contrast to TopLink)! Consider
- * registering Spring's {@code IdTransferringMergeEventListener} if
- * you would like to have newly assigned ids transferred to the original
- * object graph too.
- * @param entity the object to merge with the corresponding persistence instance
- * @return the updated, registered persistent instance
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#merge(Object)
- * @see #saveOrUpdate
- */
- T merge(T entity) throws DataAccessException;
-
- /**
- * Copy the state of the given object onto the persistent object
- * with the same identifier. Follows JSR-220 semantics.
- * Similar to {@code saveOrUpdate}, but never associates the given
- * object with the current Hibernate {@link org.hibernate.Session}. In
- * the case of a new entity, the state will be copied over as well.
- *
Note that {@code merge} will not update the identifiers
- * in the passed-in object graph (in contrast to TopLink)! Consider
- * registering Spring's {@code IdTransferringMergeEventListener}
- * if you would like to have newly assigned ids transferred to the
- * original object graph too.
- * @param entityName the name of the persistent entity
- * @param entity the object to merge with the corresponding persistence instance
- * @return the updated, registered persistent instance
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#merge(String, Object)
- * @see #saveOrUpdate
- */
- T merge(String entityName, T entity) throws DataAccessException;
-
- /**
- * Delete the given persistent instance.
- * @param entity the persistent instance to delete
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#delete(Object)
- */
- void delete(Object entity) throws DataAccessException;
-
- /**
- * Delete the given persistent instance.
- * Obtains the specified lock mode if the instance exists, implicitly
- * checking whether the corresponding database entry still exists.
- * @param entity the persistent instance to delete
- * @param lockMode the lock mode to obtain
- * @throws org.springframework.orm.ObjectOptimisticLockingFailureException if not found
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#delete(Object)
- */
- void delete(Object entity, LockMode lockMode) throws DataAccessException;
-
- /**
- * Delete the given persistent instance.
- * @param entityName the name of the persistent entity
- * @param entity the persistent instance to delete
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#delete(Object)
- */
- void delete(String entityName, Object entity) throws DataAccessException;
-
- /**
- * Delete the given persistent instance.
- *
Obtains the specified lock mode if the instance exists, implicitly
- * checking whether the corresponding database entry still exists.
- * @param entityName the name of the persistent entity
- * @param entity the persistent instance to delete
- * @param lockMode the lock mode to obtain
- * @throws org.springframework.orm.ObjectOptimisticLockingFailureException if not found
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#delete(Object)
- */
- void delete(String entityName, Object entity, LockMode lockMode) throws DataAccessException;
-
- /**
- * Delete all given persistent instances.
- *
This can be combined with any of the find methods to delete by query
- * in two lines of code.
- * @param entities the persistent instances to delete
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#delete(Object)
- */
- void deleteAll(Collection> entities) throws DataAccessException;
-
- /**
- * Flush all pending saves, updates and deletes to the database.
- *
Only invoke this for selective eager flushing, for example when
- * JDBC code needs to see certain changes within the same transaction.
- * Else, it is preferable to rely on auto-flushing at transaction
- * completion.
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#flush
- */
- void flush() throws DataAccessException;
-
- /**
- * Remove all objects from the {@link org.hibernate.Session} cache, and
- * cancel all pending saves, updates and deletes.
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#clear
- */
- void clear() throws DataAccessException;
-
-
- //-------------------------------------------------------------------------
- // Convenience finder methods for HQL strings
- //-------------------------------------------------------------------------
-
- /**
- * Execute an HQL query, binding a number of values to "?" parameters
- * in the query string.
- * @param queryString a query expressed in Hibernate's query language
- * @param values the values of the parameters
- * @return a {@link List} containing the results of the query execution
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#createQuery
- */
- List> find(String queryString, Object... values) throws DataAccessException;
-
- /**
- * Execute an HQL query, binding one value to a ":" named parameter
- * in the query string.
- * @param queryString a query expressed in Hibernate's query language
- * @param paramName the name of the parameter
- * @param value the value of the parameter
- * @return a {@link List} containing the results of the query execution
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#getNamedQuery(String)
- */
- List> findByNamedParam(String queryString, String paramName, Object value) throws DataAccessException;
-
- /**
- * Execute an HQL query, binding a number of values to ":" named
- * parameters in the query string.
- * @param queryString a query expressed in Hibernate's query language
- * @param paramNames the names of the parameters
- * @param values the values of the parameters
- * @return a {@link List} containing the results of the query execution
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#getNamedQuery(String)
- */
- List> findByNamedParam(String queryString, String[] paramNames, Object[] values) throws DataAccessException;
-
- /**
- * Execute an HQL query, binding the properties of the given bean to
- * named parameters in the query string.
- * @param queryString a query expressed in Hibernate's query language
- * @param valueBean the values of the parameters
- * @return a {@link List} containing the results of the query execution
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Query#setProperties
- * @see org.hibernate.Session#createQuery
- */
- List> findByValueBean(String queryString, Object valueBean) throws DataAccessException;
-
-
- //-------------------------------------------------------------------------
- // Convenience finder methods for named queries
- //-------------------------------------------------------------------------
-
- /**
- * Execute a named query binding a number of values to "?" parameters
- * in the query string.
- *
A named query is defined in a Hibernate mapping file.
- * @param queryName the name of a Hibernate query in a mapping file
- * @param values the values of the parameters
- * @return a {@link List} containing the results of the query execution
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#getNamedQuery(String)
- */
- List> findByNamedQuery(String queryName, Object... values) throws DataAccessException;
-
- /**
- * Execute a named query, binding one value to a ":" named parameter
- * in the query string.
- *
A named query is defined in a Hibernate mapping file.
- * @param queryName the name of a Hibernate query in a mapping file
- * @param paramName the name of parameter
- * @param value the value of the parameter
- * @return a {@link List} containing the results of the query execution
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#getNamedQuery(String)
- */
- List> findByNamedQueryAndNamedParam(String queryName, String paramName, Object value)
- throws DataAccessException;
-
- /**
- * Execute a named query, binding a number of values to ":" named
- * parameters in the query string.
- *
A named query is defined in a Hibernate mapping file.
- * @param queryName the name of a Hibernate query in a mapping file
- * @param paramNames the names of the parameters
- * @param values the values of the parameters
- * @return a {@link List} containing the results of the query execution
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#getNamedQuery(String)
- */
- List> findByNamedQueryAndNamedParam(String queryName, String[] paramNames, Object[] values)
- throws DataAccessException;
-
- /**
- * Execute a named query, binding the properties of the given bean to
- * ":" named parameters in the query string.
- *
A named query is defined in a Hibernate mapping file.
- * @param queryName the name of a Hibernate query in a mapping file
- * @param valueBean the values of the parameters
- * @return a {@link List} containing the results of the query execution
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Query#setProperties
- * @see org.hibernate.Session#getNamedQuery(String)
- */
- List> findByNamedQueryAndValueBean(String queryName, Object valueBean) throws DataAccessException;
-
-
- //-------------------------------------------------------------------------
- // Convenience finder methods for detached criteria
- //-------------------------------------------------------------------------
-
- /**
- * Execute a query based on a given Hibernate criteria object.
- * @param criteria the detached Hibernate criteria object.
- * Note: Do not reuse criteria objects! They need to recreated per execution,
- * due to the suboptimal design of Hibernate's criteria facility.
- * @return a {@link List} containing 0 or more persistent instances
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.criterion.DetachedCriteria#getExecutableCriteria(org.hibernate.Session)
- */
- List> findByCriteria(DetachedCriteria criteria) throws DataAccessException;
-
- /**
- * Execute a query based on the given Hibernate criteria object.
- * @param criteria the detached Hibernate criteria object.
- * Note: Do not reuse criteria objects! They need to recreated per execution,
- * due to the suboptimal design of Hibernate's criteria facility.
- * @param firstResult the index of the first result object to be retrieved
- * (numbered from 0)
- * @param maxResults the maximum number of result objects to retrieve
- * (or <=0 for no limit)
- * @return a {@link List} containing 0 or more persistent instances
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.criterion.DetachedCriteria#getExecutableCriteria(org.hibernate.Session)
- * @see org.hibernate.Criteria#setFirstResult(int)
- * @see org.hibernate.Criteria#setMaxResults(int)
- */
- List> findByCriteria(DetachedCriteria criteria, int firstResult, int maxResults) throws DataAccessException;
-
- /**
- * Execute a query based on the given example entity object.
- * @param exampleEntity an instance of the desired entity,
- * serving as example for "query-by-example"
- * @return a {@link List} containing 0 or more persistent instances
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.criterion.Example#create(Object)
- */
- List findByExample(T exampleEntity) throws DataAccessException;
-
- /**
- * Execute a query based on the given example entity object.
- * @param entityName the name of the persistent entity
- * @param exampleEntity an instance of the desired entity,
- * serving as example for "query-by-example"
- * @return a {@link List} containing 0 or more persistent instances
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.criterion.Example#create(Object)
- */
- List findByExample(String entityName, T exampleEntity) throws DataAccessException;
-
- /**
- * Execute a query based on a given example entity object.
- * @param exampleEntity an instance of the desired entity,
- * serving as example for "query-by-example"
- * @param firstResult the index of the first result object to be retrieved
- * (numbered from 0)
- * @param maxResults the maximum number of result objects to retrieve
- * (or <=0 for no limit)
- * @return a {@link List} containing 0 or more persistent instances
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.criterion.Example#create(Object)
- * @see org.hibernate.Criteria#setFirstResult(int)
- * @see org.hibernate.Criteria#setMaxResults(int)
- */
- List findByExample(T exampleEntity, int firstResult, int maxResults) throws DataAccessException;
-
- /**
- * Execute a query based on a given example entity object.
- * @param entityName the name of the persistent entity
- * @param exampleEntity an instance of the desired entity,
- * serving as example for "query-by-example"
- * @param firstResult the index of the first result object to be retrieved
- * (numbered from 0)
- * @param maxResults the maximum number of result objects to retrieve
- * (or <=0 for no limit)
- * @return a {@link List} containing 0 or more persistent instances
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.criterion.Example#create(Object)
- * @see org.hibernate.Criteria#setFirstResult(int)
- * @see org.hibernate.Criteria#setMaxResults(int)
- */
- List findByExample(String entityName, T exampleEntity, int firstResult, int maxResults)
- throws DataAccessException;
-
-
- //-------------------------------------------------------------------------
- // Convenience query methods for iteration and bulk updates/deletes
- //-------------------------------------------------------------------------
-
- /**
- * Execute a query for persistent instances, binding a number of
- * values to "?" parameters in the query string.
- * Returns the results as an {@link Iterator}. Entities returned are
- * initialized on demand. See the Hibernate API documentation for details.
- * @param queryString a query expressed in Hibernate's query language
- * @param values the values of the parameters
- * @return an {@link Iterator} containing 0 or more persistent instances
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#createQuery
- * @see org.hibernate.Query#iterate
- */
- Iterator> iterate(String queryString, Object... values) throws DataAccessException;
-
- /**
- * Immediately close an {@link Iterator} created by any of the various
- * {@code iterate(..)} operations, instead of waiting until the
- * session is closed or disconnected.
- * @param it the {@code Iterator} to close
- * @throws DataAccessException if the {@code Iterator} could not be closed
- * @see org.hibernate.Hibernate#close
- */
- void closeIterator(Iterator> it) throws DataAccessException;
-
- /**
- * Update/delete all objects according to the given query, binding a number of
- * values to "?" parameters in the query string.
- * @param queryString an update/delete query expressed in Hibernate's query language
- * @param values the values of the parameters
- * @return the number of instances updated/deleted
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#createQuery
- * @see org.hibernate.Query#executeUpdate
- */
- int bulkUpdate(String queryString, Object... values) throws DataAccessException;
-
-}
diff --git a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateOptimisticLockingFailureException.java b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateOptimisticLockingFailureException.java
deleted file mode 100644
index 4447aa9644..0000000000
--- a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateOptimisticLockingFailureException.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * Copyright 2002-2013 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate4;
-
-import org.hibernate.StaleObjectStateException;
-import org.hibernate.StaleStateException;
-import org.hibernate.dialect.lock.OptimisticEntityLockException;
-
-import org.springframework.orm.ObjectOptimisticLockingFailureException;
-
-/**
- * Hibernate-specific subclass of ObjectOptimisticLockingFailureException.
- * Converts Hibernate's StaleObjectStateException, StaleStateException
- * and OptimisticEntityLockException.
- *
- * @author Juergen Hoeller
- * @since 3.1
- * @see SessionFactoryUtils#convertHibernateAccessException
- */
-@SuppressWarnings("serial")
-public class HibernateOptimisticLockingFailureException extends ObjectOptimisticLockingFailureException {
-
- public HibernateOptimisticLockingFailureException(StaleObjectStateException ex) {
- super(ex.getEntityName(), ex.getIdentifier(), ex);
- }
-
- public HibernateOptimisticLockingFailureException(StaleStateException ex) {
- super(ex.getMessage(), ex);
- }
-
- public HibernateOptimisticLockingFailureException(OptimisticEntityLockException ex) {
- super(ex.getMessage(), ex);
- }
-
-}
diff --git a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateQueryException.java b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateQueryException.java
deleted file mode 100644
index b9d6516127..0000000000
--- a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateQueryException.java
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- * 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate4;
-
-import org.hibernate.QueryException;
-
-import org.springframework.dao.InvalidDataAccessResourceUsageException;
-
-/**
- * Hibernate-specific subclass of InvalidDataAccessResourceUsageException,
- * thrown on invalid HQL query syntax.
- *
- * @author Juergen Hoeller
- * @since 3.1
- * @see SessionFactoryUtils#convertHibernateAccessException
- */
-@SuppressWarnings("serial")
-public class HibernateQueryException extends InvalidDataAccessResourceUsageException {
-
- public HibernateQueryException(QueryException ex) {
- super(ex.getMessage(), ex);
- }
-
- /**
- * Return the HQL query string that was invalid.
- */
- public String getQueryString() {
- return ((QueryException) getCause()).getQueryString();
- }
-
-}
diff --git a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateSystemException.java b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateSystemException.java
deleted file mode 100644
index 358aa72a2c..0000000000
--- a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateSystemException.java
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate4;
-
-import org.hibernate.HibernateException;
-
-import org.springframework.dao.UncategorizedDataAccessException;
-
-/**
- * Hibernate-specific subclass of UncategorizedDataAccessException,
- * for Hibernate system errors that do not match any concrete
- * {@code org.springframework.dao} exceptions.
- *
- * @author Juergen Hoeller
- * @since 3.1
- * @see SessionFactoryUtils#convertHibernateAccessException
- */
-@SuppressWarnings("serial")
-public class HibernateSystemException extends UncategorizedDataAccessException {
-
- /**
- * Create a new HibernateSystemException,
- * wrapping an arbitrary HibernateException.
- * @param cause the HibernateException thrown
- */
- public HibernateSystemException(HibernateException cause) {
- super(cause != null ? cause.getMessage() : null, cause);
- }
-
-}
diff --git a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateTemplate.java b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateTemplate.java
deleted file mode 100644
index f05bd73276..0000000000
--- a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateTemplate.java
+++ /dev/null
@@ -1,1263 +0,0 @@
-/*
- * Copyright 2002-2015 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate4;
-
-import java.io.Serializable;
-import java.lang.reflect.InvocationHandler;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.lang.reflect.Proxy;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.List;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
-import org.hibernate.Criteria;
-import org.hibernate.Filter;
-import org.hibernate.FlushMode;
-import org.hibernate.Hibernate;
-import org.hibernate.HibernateException;
-import org.hibernate.LockMode;
-import org.hibernate.LockOptions;
-import org.hibernate.Query;
-import org.hibernate.ReplicationMode;
-import org.hibernate.Session;
-import org.hibernate.SessionFactory;
-import org.hibernate.criterion.DetachedCriteria;
-import org.hibernate.criterion.Example;
-
-import org.springframework.beans.factory.InitializingBean;
-import org.springframework.dao.DataAccessException;
-import org.springframework.dao.InvalidDataAccessApiUsageException;
-import org.springframework.transaction.support.TransactionSynchronizationManager;
-import org.springframework.util.Assert;
-
-/**
- * Helper class that simplifies Hibernate data access code. Automatically
- * converts HibernateExceptions into DataAccessExceptions, following the
- * {@code org.springframework.dao} exception hierarchy.
- *
- *
The central method is {@code execute}, supporting Hibernate access code
- * implementing the {@link HibernateCallback} interface. It provides Hibernate Session
- * handling such that neither the HibernateCallback implementation nor the calling
- * code needs to explicitly care about retrieving/closing Hibernate Sessions,
- * or handling Session lifecycle exceptions. For typical single step actions,
- * there are various convenience methods (find, load, saveOrUpdate, delete).
- *
- *
Can be used within a service implementation via direct instantiation
- * with a SessionFactory reference, or get prepared in an application context
- * and given to services as bean reference. Note: The SessionFactory should
- * always be configured as bean in the application context, in the first case
- * given to the service directly, in the second case to the prepared template.
- *
- *
NOTE: Hibernate access code can also be coded in plain Hibernate style.
- * Hence, for newly started projects, consider adopting the standard Hibernate
- * style of coding data access objects instead, based on
- * {@link org.hibernate.SessionFactory#getCurrentSession()}.
- * This HibernateTemplate primarily exists as a migration helper for Hibernate 3
- * based data access code, to benefit from bug fixes in Hibernate 4.x.
- *
- * @author Juergen Hoeller
- * @since 4.0.1
- * @see #setSessionFactory
- * @see HibernateCallback
- * @see org.hibernate.Session
- * @see LocalSessionFactoryBean
- * @see HibernateTransactionManager
- * @see org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
- * @see org.springframework.orm.hibernate4.support.OpenSessionInViewInterceptor
- */
-public class HibernateTemplate implements HibernateOperations, InitializingBean {
-
- protected final Log logger = LogFactory.getLog(getClass());
-
- private SessionFactory sessionFactory;
-
- private String[] filterNames;
-
- private boolean exposeNativeSession = false;
-
- private boolean checkWriteOperations = true;
-
- private boolean cacheQueries = false;
-
- private String queryCacheRegion;
-
- private int fetchSize = 0;
-
- private int maxResults = 0;
-
-
- /**
- * Create a new HibernateTemplate instance.
- */
- public HibernateTemplate() {
- }
-
- /**
- * Create a new HibernateTemplate instance.
- * @param sessionFactory the SessionFactory to create Sessions with
- */
- public HibernateTemplate(SessionFactory sessionFactory) {
- setSessionFactory(sessionFactory);
- afterPropertiesSet();
- }
-
-
- /**
- * Set the Hibernate SessionFactory that should be used to create
- * Hibernate Sessions.
- */
- public void setSessionFactory(SessionFactory sessionFactory) {
- this.sessionFactory = sessionFactory;
- }
-
- /**
- * Return the Hibernate SessionFactory that should be used to create
- * Hibernate Sessions.
- */
- public SessionFactory getSessionFactory() {
- return this.sessionFactory;
- }
-
- /**
- * Set one or more names of Hibernate filters to be activated for all
- * Sessions that this accessor works with.
- *
Each of those filters will be enabled at the beginning of each
- * operation and correspondingly disabled at the end of the operation.
- * This will work for newly opened Sessions as well as for existing
- * Sessions (for example, within a transaction).
- * @see #enableFilters(org.hibernate.Session)
- * @see org.hibernate.Session#enableFilter(String)
- */
- public void setFilterNames(String... filterNames) {
- this.filterNames = filterNames;
- }
-
- /**
- * Return the names of Hibernate filters to be activated, if any.
- */
- public String[] getFilterNames() {
- return this.filterNames;
- }
-
- /**
- * Set whether to expose the native Hibernate Session to
- * HibernateCallback code.
- *
Default is "false": a Session proxy will be returned, suppressing
- * {@code close} calls and automatically applying query cache
- * settings and transaction timeouts.
- * @see HibernateCallback
- * @see org.hibernate.Session
- * @see #setCacheQueries
- * @see #setQueryCacheRegion
- * @see #prepareQuery
- * @see #prepareCriteria
- */
- public void setExposeNativeSession(boolean exposeNativeSession) {
- this.exposeNativeSession = exposeNativeSession;
- }
-
- /**
- * Return whether to expose the native Hibernate Session to
- * HibernateCallback code, or rather a Session proxy.
- */
- public boolean isExposeNativeSession() {
- return this.exposeNativeSession;
- }
-
- /**
- * Set whether to check that the Hibernate Session is not in read-only mode
- * in case of write operations (save/update/delete).
- *
Default is "true", for fail-fast behavior when attempting write operations
- * within a read-only transaction. Turn this off to allow save/update/delete
- * on a Session with flush mode MANUAL.
- * @see #checkWriteOperationAllowed
- * @see org.springframework.transaction.TransactionDefinition#isReadOnly
- */
- public void setCheckWriteOperations(boolean checkWriteOperations) {
- this.checkWriteOperations = checkWriteOperations;
- }
-
- /**
- * Return whether to check that the Hibernate Session is not in read-only
- * mode in case of write operations (save/update/delete).
- */
- public boolean isCheckWriteOperations() {
- return this.checkWriteOperations;
- }
-
- /**
- * Set whether to cache all queries executed by this template.
- *
If this is "true", all Query and Criteria objects created by
- * this template will be marked as cacheable (including all
- * queries through find methods).
- *
To specify the query region to be used for queries cached
- * by this template, set the "queryCacheRegion" property.
- * @see #setQueryCacheRegion
- * @see org.hibernate.Query#setCacheable
- * @see org.hibernate.Criteria#setCacheable
- */
- public void setCacheQueries(boolean cacheQueries) {
- this.cacheQueries = cacheQueries;
- }
-
- /**
- * Return whether to cache all queries executed by this template.
- */
- public boolean isCacheQueries() {
- return this.cacheQueries;
- }
-
- /**
- * Set the name of the cache region for queries executed by this template.
- *
If this is specified, it will be applied to all Query and Criteria objects
- * created by this template (including all queries through find methods).
- *
The cache region will not take effect unless queries created by this
- * template are configured to be cached via the "cacheQueries" property.
- * @see #setCacheQueries
- * @see org.hibernate.Query#setCacheRegion
- * @see org.hibernate.Criteria#setCacheRegion
- */
- public void setQueryCacheRegion(String queryCacheRegion) {
- this.queryCacheRegion = queryCacheRegion;
- }
-
- /**
- * Return the name of the cache region for queries executed by this template.
- */
- public String getQueryCacheRegion() {
- return this.queryCacheRegion;
- }
-
- /**
- * Set the fetch size for this HibernateTemplate. This is important for processing
- * large result sets: Setting this higher than the default value will increase
- * processing speed at the cost of memory consumption; setting this lower can
- * avoid transferring row data that will never be read by the application.
- *
Default is 0, indicating to use the JDBC driver's default.
- */
- public void setFetchSize(int fetchSize) {
- this.fetchSize = fetchSize;
- }
-
- /**
- * Return the fetch size specified for this HibernateTemplate.
- */
- public int getFetchSize() {
- return this.fetchSize;
- }
-
- /**
- * Set the maximum number of rows for this HibernateTemplate. This is important
- * for processing subsets of large result sets, avoiding to read and hold
- * the entire result set in the database or in the JDBC driver if we're
- * never interested in the entire result in the first place (for example,
- * when performing searches that might return a large number of matches).
- *
Default is 0, indicating to use the JDBC driver's default.
- */
- public void setMaxResults(int maxResults) {
- this.maxResults = maxResults;
- }
-
- /**
- * Return the maximum number of rows specified for this HibernateTemplate.
- */
- public int getMaxResults() {
- return this.maxResults;
- }
-
- @Override
- public void afterPropertiesSet() {
- if (getSessionFactory() == null) {
- throw new IllegalArgumentException("Property 'sessionFactory' is required");
- }
- }
-
-
- @Override
- public T execute(HibernateCallback action) throws DataAccessException {
- return doExecute(action, false);
- }
-
- /**
- * Execute the action specified by the given action object within a
- * native {@link org.hibernate.Session}.
- * This execute variant overrides the template-wide
- * {@link #isExposeNativeSession() "exposeNativeSession"} setting.
- * @param action callback object that specifies the Hibernate action
- * @return a result object returned by the action, or {@code null}
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- */
- public T executeWithNativeSession(HibernateCallback action) {
- return doExecute(action, true);
- }
-
- /**
- * Execute the action specified by the given action object within a Session.
- * @param action callback object that specifies the Hibernate action
- * @param enforceNativeSession whether to enforce exposure of the native
- * Hibernate Session to callback code
- * @return a result object returned by the action, or {@code null}
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- */
- protected T doExecute(HibernateCallback action, boolean enforceNativeSession) throws DataAccessException {
- Assert.notNull(action, "Callback object must not be null");
-
- Session session = null;
- boolean isNew = false;
- try {
- session = getSessionFactory().getCurrentSession();
- }
- catch (HibernateException ex) {
- logger.debug("Could not retrieve pre-bound Hibernate session", ex);
- }
- if (session == null) {
- session = getSessionFactory().openSession();
- session.setFlushMode(FlushMode.MANUAL);
- isNew = true;
- }
-
- try {
- enableFilters(session);
- Session sessionToExpose =
- (enforceNativeSession || isExposeNativeSession() ? session : createSessionProxy(session));
- return action.doInHibernate(sessionToExpose);
- }
- catch (HibernateException ex) {
- throw SessionFactoryUtils.convertHibernateAccessException(ex);
- }
- catch (RuntimeException ex) {
- // Callback code threw application exception...
- throw ex;
- }
- finally {
- if (isNew) {
- SessionFactoryUtils.closeSession(session);
- }
- else {
- disableFilters(session);
- }
- }
- }
-
- /**
- * Create a close-suppressing proxy for the given Hibernate Session.
- * The proxy also prepares returned Query and Criteria objects.
- * @param session the Hibernate Session to create a proxy for
- * @return the Session proxy
- * @see org.hibernate.Session#close()
- * @see #prepareQuery
- * @see #prepareCriteria
- */
- protected Session createSessionProxy(Session session) {
- return (Session) Proxy.newProxyInstance(
- session.getClass().getClassLoader(), new Class>[] {Session.class},
- new CloseSuppressingInvocationHandler(session));
- }
-
- /**
- * Enable the specified filters on the given Session.
- * @param session the current Hibernate Session
- * @see #setFilterNames
- * @see org.hibernate.Session#enableFilter(String)
- */
- protected void enableFilters(Session session) {
- String[] filterNames = getFilterNames();
- if (filterNames != null) {
- for (String filterName : filterNames) {
- session.enableFilter(filterName);
- }
- }
- }
-
- /**
- * Disable the specified filters on the given Session.
- * @param session the current Hibernate Session
- * @see #setFilterNames
- * @see org.hibernate.Session#disableFilter(String)
- */
- protected void disableFilters(Session session) {
- String[] filterNames = getFilterNames();
- if (filterNames != null) {
- for (String filterName : filterNames) {
- session.disableFilter(filterName);
- }
- }
- }
-
-
- //-------------------------------------------------------------------------
- // Convenience methods for loading individual objects
- //-------------------------------------------------------------------------
-
- @Override
- public T get(Class entityClass, Serializable id) throws DataAccessException {
- return get(entityClass, id, null);
- }
-
- @Override
- public T get(final Class entityClass, final Serializable id, final LockMode lockMode)
- throws DataAccessException {
-
- return executeWithNativeSession(new HibernateCallback() {
- @Override
- @SuppressWarnings("unchecked")
- public T doInHibernate(Session session) throws HibernateException {
- if (lockMode != null) {
- return (T) session.get(entityClass, id, new LockOptions(lockMode));
- }
- else {
- return (T) session.get(entityClass, id);
- }
- }
- });
- }
-
- @Override
- public Object get(String entityName, Serializable id) throws DataAccessException {
- return get(entityName, id, null);
- }
-
- @Override
- public Object get(final String entityName, final Serializable id, final LockMode lockMode)
- throws DataAccessException {
-
- return executeWithNativeSession(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) throws HibernateException {
- if (lockMode != null) {
- return session.get(entityName, id, new LockOptions(lockMode));
- }
- else {
- return session.get(entityName, id);
- }
- }
- });
- }
-
- @Override
- public T load(Class entityClass, Serializable id) throws DataAccessException {
- return load(entityClass, id, null);
- }
-
- @Override
- public T load(final Class entityClass, final Serializable id, final LockMode lockMode)
- throws DataAccessException {
-
- return executeWithNativeSession(new HibernateCallback() {
- @Override
- @SuppressWarnings("unchecked")
- public T doInHibernate(Session session) throws HibernateException {
- if (lockMode != null) {
- return (T) session.load(entityClass, id, new LockOptions(lockMode));
- }
- else {
- return (T) session.load(entityClass, id);
- }
- }
- });
- }
-
- @Override
- public Object load(String entityName, Serializable id) throws DataAccessException {
- return load(entityName, id, null);
- }
-
- @Override
- public Object load(final String entityName, final Serializable id, final LockMode lockMode)
- throws DataAccessException {
-
- return executeWithNativeSession(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) throws HibernateException {
- if (lockMode != null) {
- return session.load(entityName, id, new LockOptions(lockMode));
- }
- else {
- return session.load(entityName, id);
- }
- }
- });
- }
-
- @Override
- public List loadAll(final Class entityClass) throws DataAccessException {
- return executeWithNativeSession(new HibernateCallback>() {
- @Override
- @SuppressWarnings("unchecked")
- public List doInHibernate(Session session) throws HibernateException {
- Criteria criteria = session.createCriteria(entityClass);
- criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
- prepareCriteria(criteria);
- return criteria.list();
- }
- });
- }
-
- @Override
- public void load(final Object entity, final Serializable id) throws DataAccessException {
- executeWithNativeSession(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) throws HibernateException {
- session.load(entity, id);
- return null;
- }
- });
- }
-
- @Override
- public void refresh(final Object entity) throws DataAccessException {
- refresh(entity, null);
- }
-
- @Override
- public void refresh(final Object entity, final LockMode lockMode) throws DataAccessException {
- executeWithNativeSession(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) throws HibernateException {
- if (lockMode != null) {
- session.refresh(entity, new LockOptions(lockMode));
- }
- else {
- session.refresh(entity);
- }
- return null;
- }
- });
- }
-
- @Override
- public boolean contains(final Object entity) throws DataAccessException {
- return executeWithNativeSession(new HibernateCallback() {
- @Override
- public Boolean doInHibernate(Session session) {
- return session.contains(entity);
- }
- });
- }
-
- @Override
- public void evict(final Object entity) throws DataAccessException {
- executeWithNativeSession(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) throws HibernateException {
- session.evict(entity);
- return null;
- }
- });
- }
-
- @Override
- public void initialize(Object proxy) throws DataAccessException {
- try {
- Hibernate.initialize(proxy);
- }
- catch (HibernateException ex) {
- throw SessionFactoryUtils.convertHibernateAccessException(ex);
- }
- }
-
- @Override
- public Filter enableFilter(String filterName) throws IllegalStateException {
- Session session = getSessionFactory().getCurrentSession();
- Filter filter = session.getEnabledFilter(filterName);
- if (filter == null) {
- filter = session.enableFilter(filterName);
- }
- return filter;
- }
-
-
- //-------------------------------------------------------------------------
- // Convenience methods for storing individual objects
- //-------------------------------------------------------------------------
-
- @Override
- public void lock(final Object entity, final LockMode lockMode) throws DataAccessException {
- executeWithNativeSession(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) throws HibernateException {
- session.buildLockRequest(new LockOptions(lockMode)).lock(entity);
- return null;
- }
- });
- }
-
- @Override
- public void lock(final String entityName, final Object entity, final LockMode lockMode)
- throws DataAccessException {
-
- executeWithNativeSession(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) throws HibernateException {
- session.buildLockRequest(new LockOptions(lockMode)).lock(entityName, entity);
- return null;
- }
- });
- }
-
- @Override
- public Serializable save(final Object entity) throws DataAccessException {
- return executeWithNativeSession(new HibernateCallback() {
- @Override
- public Serializable doInHibernate(Session session) throws HibernateException {
- checkWriteOperationAllowed(session);
- return session.save(entity);
- }
- });
- }
-
- @Override
- public Serializable save(final String entityName, final Object entity) throws DataAccessException {
- return executeWithNativeSession(new HibernateCallback() {
- @Override
- public Serializable doInHibernate(Session session) throws HibernateException {
- checkWriteOperationAllowed(session);
- return session.save(entityName, entity);
- }
- });
- }
-
- @Override
- public void update(Object entity) throws DataAccessException {
- update(entity, null);
- }
-
- @Override
- public void update(final Object entity, final LockMode lockMode) throws DataAccessException {
- executeWithNativeSession(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) throws HibernateException {
- checkWriteOperationAllowed(session);
- session.update(entity);
- if (lockMode != null) {
- session.buildLockRequest(new LockOptions(lockMode)).lock(entity);
- }
- return null;
- }
- });
- }
-
- @Override
- public void update(String entityName, Object entity) throws DataAccessException {
- update(entityName, entity, null);
- }
-
- @Override
- public void update(final String entityName, final Object entity, final LockMode lockMode)
- throws DataAccessException {
-
- executeWithNativeSession(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) throws HibernateException {
- checkWriteOperationAllowed(session);
- session.update(entityName, entity);
- if (lockMode != null) {
- session.buildLockRequest(new LockOptions(lockMode)).lock(entityName, entity);
- }
- return null;
- }
- });
- }
-
- @Override
- public void saveOrUpdate(final Object entity) throws DataAccessException {
- executeWithNativeSession(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) throws HibernateException {
- checkWriteOperationAllowed(session);
- session.saveOrUpdate(entity);
- return null;
- }
- });
- }
-
- @Override
- public void saveOrUpdate(final String entityName, final Object entity) throws DataAccessException {
- executeWithNativeSession(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) throws HibernateException {
- checkWriteOperationAllowed(session);
- session.saveOrUpdate(entityName, entity);
- return null;
- }
- });
- }
-
- @Override
- public void replicate(final Object entity, final ReplicationMode replicationMode)
- throws DataAccessException {
-
- executeWithNativeSession(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) throws HibernateException {
- checkWriteOperationAllowed(session);
- session.replicate(entity, replicationMode);
- return null;
- }
- });
- }
-
- @Override
- public void replicate(final String entityName, final Object entity, final ReplicationMode replicationMode)
- throws DataAccessException {
-
- executeWithNativeSession(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) throws HibernateException {
- checkWriteOperationAllowed(session);
- session.replicate(entityName, entity, replicationMode);
- return null;
- }
- });
- }
-
- @Override
- public void persist(final Object entity) throws DataAccessException {
- executeWithNativeSession(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) throws HibernateException {
- checkWriteOperationAllowed(session);
- session.persist(entity);
- return null;
- }
- });
- }
-
- @Override
- public void persist(final String entityName, final Object entity) throws DataAccessException {
- executeWithNativeSession(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) throws HibernateException {
- checkWriteOperationAllowed(session);
- session.persist(entityName, entity);
- return null;
- }
- });
- }
-
- @Override
- public T merge(final T entity) throws DataAccessException {
- return executeWithNativeSession(new HibernateCallback() {
- @Override
- @SuppressWarnings("unchecked")
- public T doInHibernate(Session session) throws HibernateException {
- checkWriteOperationAllowed(session);
- return (T) session.merge(entity);
- }
- });
- }
-
- @Override
- public T merge(final String entityName, final T entity) throws DataAccessException {
- return executeWithNativeSession(new HibernateCallback() {
- @Override
- @SuppressWarnings("unchecked")
- public T doInHibernate(Session session) throws HibernateException {
- checkWriteOperationAllowed(session);
- return (T) session.merge(entityName, entity);
- }
- });
- }
-
- @Override
- public void delete(Object entity) throws DataAccessException {
- delete(entity, null);
- }
-
- @Override
- public void delete(final Object entity, final LockMode lockMode) throws DataAccessException {
- executeWithNativeSession(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) throws HibernateException {
- checkWriteOperationAllowed(session);
- if (lockMode != null) {
- session.buildLockRequest(new LockOptions(lockMode)).lock(entity);
- }
- session.delete(entity);
- return null;
- }
- });
- }
-
- @Override
- public void delete(String entityName, Object entity) throws DataAccessException {
- delete(entityName, entity, null);
- }
-
- @Override
- public void delete(final String entityName, final Object entity, final LockMode lockMode)
- throws DataAccessException {
-
- executeWithNativeSession(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) throws HibernateException {
- checkWriteOperationAllowed(session);
- if (lockMode != null) {
- session.buildLockRequest(new LockOptions(lockMode)).lock(entityName, entity);
- }
- session.delete(entityName, entity);
- return null;
- }
- });
- }
-
- @Override
- public void deleteAll(final Collection> entities) throws DataAccessException {
- executeWithNativeSession(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) throws HibernateException {
- checkWriteOperationAllowed(session);
- for (Object entity : entities) {
- session.delete(entity);
- }
- return null;
- }
- });
- }
-
- @Override
- public void flush() throws DataAccessException {
- executeWithNativeSession(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) throws HibernateException {
- session.flush();
- return null;
- }
- });
- }
-
- @Override
- public void clear() throws DataAccessException {
- executeWithNativeSession(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) {
- session.clear();
- return null;
- }
- });
- }
-
-
- //-------------------------------------------------------------------------
- // Convenience finder methods for HQL strings
- //-------------------------------------------------------------------------
-
- @Override
- public List> find(final String queryString, final Object... values) throws DataAccessException {
- return executeWithNativeSession(new HibernateCallback>() {
- @Override
- public List> doInHibernate(Session session) throws HibernateException {
- Query queryObject = session.createQuery(queryString);
- prepareQuery(queryObject);
- if (values != null) {
- for (int i = 0; i < values.length; i++) {
- queryObject.setParameter(i, values[i]);
- }
- }
- return queryObject.list();
- }
- });
- }
-
- @Override
- public List> findByNamedParam(String queryString, String paramName, Object value)
- throws DataAccessException {
-
- return findByNamedParam(queryString, new String[] {paramName}, new Object[] {value});
- }
-
- @Override
- public List> findByNamedParam(final String queryString, final String[] paramNames, final Object[] values)
- throws DataAccessException {
-
- if (paramNames.length != values.length) {
- throw new IllegalArgumentException("Length of paramNames array must match length of values array");
- }
- return executeWithNativeSession(new HibernateCallback>() {
- @Override
- public List> doInHibernate(Session session) throws HibernateException {
- Query queryObject = session.createQuery(queryString);
- prepareQuery(queryObject);
- if (values != null) {
- for (int i = 0; i < values.length; i++) {
- applyNamedParameterToQuery(queryObject, paramNames[i], values[i]);
- }
- }
- return queryObject.list();
- }
- });
- }
-
- @Override
- public List> findByValueBean(final String queryString, final Object valueBean)
- throws DataAccessException {
-
- return executeWithNativeSession(new HibernateCallback>() {
- @Override
- public List> doInHibernate(Session session) throws HibernateException {
- Query queryObject = session.createQuery(queryString);
- prepareQuery(queryObject);
- queryObject.setProperties(valueBean);
- return queryObject.list();
- }
- });
- }
-
-
- //-------------------------------------------------------------------------
- // Convenience finder methods for named queries
- //-------------------------------------------------------------------------
-
- @Override
- public List> findByNamedQuery(final String queryName, final Object... values) throws DataAccessException {
- return executeWithNativeSession(new HibernateCallback>() {
- @Override
- public List> doInHibernate(Session session) throws HibernateException {
- Query queryObject = session.getNamedQuery(queryName);
- prepareQuery(queryObject);
- if (values != null) {
- for (int i = 0; i < values.length; i++) {
- queryObject.setParameter(i, values[i]);
- }
- }
- return queryObject.list();
- }
- });
- }
-
- @Override
- public List> findByNamedQueryAndNamedParam(String queryName, String paramName, Object value)
- throws DataAccessException {
-
- return findByNamedQueryAndNamedParam(queryName, new String[] {paramName}, new Object[] {value});
- }
-
- @Override
- public List> findByNamedQueryAndNamedParam(
- final String queryName, final String[] paramNames, final Object[] values)
- throws DataAccessException {
-
- if (values != null && (paramNames == null || paramNames.length != values.length)) {
- throw new IllegalArgumentException("Length of paramNames array must match length of values array");
- }
- return executeWithNativeSession(new HibernateCallback>() {
- @Override
- public List> doInHibernate(Session session) throws HibernateException {
- Query queryObject = session.getNamedQuery(queryName);
- prepareQuery(queryObject);
- if (values != null) {
- for (int i = 0; i < values.length; i++) {
- applyNamedParameterToQuery(queryObject, paramNames[i], values[i]);
- }
- }
- return queryObject.list();
- }
- });
- }
-
- @Override
- public List> findByNamedQueryAndValueBean(final String queryName, final Object valueBean)
- throws DataAccessException {
-
- return executeWithNativeSession(new HibernateCallback>() {
- @Override
- public List> doInHibernate(Session session) throws HibernateException {
- Query queryObject = session.getNamedQuery(queryName);
- prepareQuery(queryObject);
- queryObject.setProperties(valueBean);
- return queryObject.list();
- }
- });
- }
-
-
- //-------------------------------------------------------------------------
- // Convenience finder methods for detached criteria
- //-------------------------------------------------------------------------
-
- @Override
- public List> findByCriteria(DetachedCriteria criteria) throws DataAccessException {
- return findByCriteria(criteria, -1, -1);
- }
-
- @Override
- public List> findByCriteria(final DetachedCriteria criteria, final int firstResult, final int maxResults)
- throws DataAccessException {
-
- Assert.notNull(criteria, "DetachedCriteria must not be null");
- return executeWithNativeSession(new HibernateCallback>() {
- @Override
- public List> doInHibernate(Session session) throws HibernateException {
- Criteria executableCriteria = criteria.getExecutableCriteria(session);
- prepareCriteria(executableCriteria);
- if (firstResult >= 0) {
- executableCriteria.setFirstResult(firstResult);
- }
- if (maxResults > 0) {
- executableCriteria.setMaxResults(maxResults);
- }
- return executableCriteria.list();
- }
- });
- }
-
- @Override
- public List findByExample(T exampleEntity) throws DataAccessException {
- return findByExample(null, exampleEntity, -1, -1);
- }
-
- @Override
- public List findByExample(String entityName, T exampleEntity) throws DataAccessException {
- return findByExample(entityName, exampleEntity, -1, -1);
- }
-
- @Override
- public List findByExample(T exampleEntity, int firstResult, int maxResults) throws DataAccessException {
- return findByExample(null, exampleEntity, firstResult, maxResults);
- }
-
- @Override
- public List findByExample(
- final String entityName, final T exampleEntity, final int firstResult, final int maxResults)
- throws DataAccessException {
-
- Assert.notNull(exampleEntity, "Example entity must not be null");
- return executeWithNativeSession(new HibernateCallback>() {
- @Override
- @SuppressWarnings("unchecked")
- public List doInHibernate(Session session) throws HibernateException {
- Criteria executableCriteria = (entityName != null ?
- session.createCriteria(entityName) : session.createCriteria(exampleEntity.getClass()));
- executableCriteria.add(Example.create(exampleEntity));
- prepareCriteria(executableCriteria);
- if (firstResult >= 0) {
- executableCriteria.setFirstResult(firstResult);
- }
- if (maxResults > 0) {
- executableCriteria.setMaxResults(maxResults);
- }
- return executableCriteria.list();
- }
- });
- }
-
-
- //-------------------------------------------------------------------------
- // Convenience query methods for iteration and bulk updates/deletes
- //-------------------------------------------------------------------------
-
- @Override
- public Iterator> iterate(final String queryString, final Object... values) throws DataAccessException {
- return executeWithNativeSession(new HibernateCallback>() {
- @Override
- public Iterator> doInHibernate(Session session) throws HibernateException {
- Query queryObject = session.createQuery(queryString);
- prepareQuery(queryObject);
- if (values != null) {
- for (int i = 0; i < values.length; i++) {
- queryObject.setParameter(i, values[i]);
- }
- }
- return queryObject.iterate();
- }
- });
- }
-
- @Override
- public void closeIterator(Iterator> it) throws DataAccessException {
- try {
- Hibernate.close(it);
- }
- catch (HibernateException ex) {
- throw SessionFactoryUtils.convertHibernateAccessException(ex);
- }
- }
-
- @Override
- public int bulkUpdate(final String queryString, final Object... values) throws DataAccessException {
- return executeWithNativeSession(new HibernateCallback() {
- @Override
- public Integer doInHibernate(Session session) throws HibernateException {
- Query queryObject = session.createQuery(queryString);
- prepareQuery(queryObject);
- if (values != null) {
- for (int i = 0; i < values.length; i++) {
- queryObject.setParameter(i, values[i]);
- }
- }
- return queryObject.executeUpdate();
- }
- });
- }
-
-
- //-------------------------------------------------------------------------
- // Helper methods used by the operations above
- //-------------------------------------------------------------------------
-
- /**
- * Check whether write operations are allowed on the given Session.
- * Default implementation throws an InvalidDataAccessApiUsageException in
- * case of {@code FlushMode.MANUAL}. Can be overridden in subclasses.
- * @param session current Hibernate Session
- * @throws InvalidDataAccessApiUsageException if write operations are not allowed
- * @see #setCheckWriteOperations
- * @see org.hibernate.Session#getFlushMode()
- * @see org.hibernate.FlushMode#MANUAL
- */
- protected void checkWriteOperationAllowed(Session session) throws InvalidDataAccessApiUsageException {
- if (isCheckWriteOperations() && session.getFlushMode().lessThan(FlushMode.COMMIT)) {
- throw new InvalidDataAccessApiUsageException(
- "Write operations are not allowed in read-only mode (FlushMode.MANUAL): "+
- "Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.");
- }
- }
-
- /**
- * Prepare the given Query object, applying cache settings and/or
- * a transaction timeout.
- * @param queryObject the Query object to prepare
- * @see #setCacheQueries
- * @see #setQueryCacheRegion
- */
- protected void prepareQuery(Query queryObject) {
- if (isCacheQueries()) {
- queryObject.setCacheable(true);
- if (getQueryCacheRegion() != null) {
- queryObject.setCacheRegion(getQueryCacheRegion());
- }
- }
- if (getFetchSize() > 0) {
- queryObject.setFetchSize(getFetchSize());
- }
- if (getMaxResults() > 0) {
- queryObject.setMaxResults(getMaxResults());
- }
-
- SessionHolder sessionHolder =
- (SessionHolder) TransactionSynchronizationManager.getResource(getSessionFactory());
- if (sessionHolder != null && sessionHolder.hasTimeout()) {
- queryObject.setTimeout(sessionHolder.getTimeToLiveInSeconds());
- }
- }
-
- /**
- * Prepare the given Criteria object, applying cache settings and/or
- * a transaction timeout.
- * @param criteria the Criteria object to prepare
- * @see #setCacheQueries
- * @see #setQueryCacheRegion
- */
- protected void prepareCriteria(Criteria criteria) {
- if (isCacheQueries()) {
- criteria.setCacheable(true);
- if (getQueryCacheRegion() != null) {
- criteria.setCacheRegion(getQueryCacheRegion());
- }
- }
- if (getFetchSize() > 0) {
- criteria.setFetchSize(getFetchSize());
- }
- if (getMaxResults() > 0) {
- criteria.setMaxResults(getMaxResults());
- }
-
- SessionHolder sessionHolder =
- (SessionHolder) TransactionSynchronizationManager.getResource(getSessionFactory());
- if (sessionHolder != null && sessionHolder.hasTimeout()) {
- criteria.setTimeout(sessionHolder.getTimeToLiveInSeconds());
- }
- }
-
- /**
- * Apply the given name parameter to the given Query object.
- * @param queryObject the Query object
- * @param paramName the name of the parameter
- * @param value the value of the parameter
- * @throws HibernateException if thrown by the Query object
- */
- protected void applyNamedParameterToQuery(Query queryObject, String paramName, Object value)
- throws HibernateException {
-
- if (value instanceof Collection) {
- queryObject.setParameterList(paramName, (Collection>) value);
- }
- else if (value instanceof Object[]) {
- queryObject.setParameterList(paramName, (Object[]) value);
- }
- else {
- queryObject.setParameter(paramName, value);
- }
- }
-
-
- /**
- * Invocation handler that suppresses close calls on Hibernate Sessions.
- * Also prepares returned Query and Criteria objects.
- * @see org.hibernate.Session#close
- */
- private class CloseSuppressingInvocationHandler implements InvocationHandler {
-
- private final Session target;
-
- public CloseSuppressingInvocationHandler(Session target) {
- this.target = target;
- }
-
- @Override
- public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
- // Invocation on Session interface coming in...
-
- if (method.getName().equals("equals")) {
- // Only consider equal when proxies are identical.
- return (proxy == args[0]);
- }
- else if (method.getName().equals("hashCode")) {
- // Use hashCode of Session proxy.
- return System.identityHashCode(proxy);
- }
- else if (method.getName().equals("close")) {
- // Handle close method: suppress, not valid.
- return null;
- }
-
- // Invoke method on target Session.
- try {
- Object retVal = method.invoke(this.target, args);
-
- // If return value is a Query or Criteria, apply transaction timeout.
- // Applies to createQuery, getNamedQuery, createCriteria.
- if (retVal instanceof Query) {
- prepareQuery(((Query) retVal));
- }
- if (retVal instanceof Criteria) {
- prepareCriteria(((Criteria) retVal));
- }
-
- return retVal;
- }
- catch (InvocationTargetException ex) {
- throw ex.getTargetException();
- }
- }
- }
-
-}
diff --git a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateTransactionManager.java b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateTransactionManager.java
deleted file mode 100644
index c9ac33332c..0000000000
--- a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateTransactionManager.java
+++ /dev/null
@@ -1,843 +0,0 @@
-/*
- * Copyright 2002-2016 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate4;
-
-import java.sql.Connection;
-import java.sql.ResultSet;
-import javax.sql.DataSource;
-
-import org.hibernate.ConnectionReleaseMode;
-import org.hibernate.FlushMode;
-import org.hibernate.HibernateException;
-import org.hibernate.Interceptor;
-import org.hibernate.Session;
-import org.hibernate.SessionFactory;
-import org.hibernate.Transaction;
-import org.hibernate.engine.spi.SessionImplementor;
-import org.hibernate.engine.transaction.spi.TransactionContext;
-
-import org.springframework.beans.BeansException;
-import org.springframework.beans.factory.BeanFactory;
-import org.springframework.beans.factory.BeanFactoryAware;
-import org.springframework.beans.factory.InitializingBean;
-import org.springframework.dao.DataAccessException;
-import org.springframework.dao.DataAccessResourceFailureException;
-import org.springframework.jdbc.datasource.ConnectionHolder;
-import org.springframework.jdbc.datasource.DataSourceUtils;
-import org.springframework.jdbc.datasource.JdbcTransactionObjectSupport;
-import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
-import org.springframework.transaction.CannotCreateTransactionException;
-import org.springframework.transaction.IllegalTransactionStateException;
-import org.springframework.transaction.InvalidIsolationLevelException;
-import org.springframework.transaction.TransactionDefinition;
-import org.springframework.transaction.TransactionSystemException;
-import org.springframework.transaction.support.AbstractPlatformTransactionManager;
-import org.springframework.transaction.support.DefaultTransactionStatus;
-import org.springframework.transaction.support.ResourceTransactionManager;
-import org.springframework.transaction.support.TransactionSynchronizationManager;
-
-/**
- * {@link org.springframework.transaction.PlatformTransactionManager}
- * implementation for a single Hibernate {@link org.hibernate.SessionFactory}.
- * Binds a Hibernate Session from the specified factory to the thread,
- * potentially allowing for one thread-bound Session per factory.
- * {@code SessionFactory.getCurrentSession()} is required for Hibernate
- * access code that needs to support this transaction handling mechanism,
- * with the SessionFactory being configured with {@link SpringSessionContext}.
- *
- *
Supports custom isolation levels, and timeouts that get applied as
- * Hibernate transaction timeouts.
- *
- *
This transaction manager is appropriate for applications that use a single
- * Hibernate SessionFactory for transactional data access, but it also supports
- * direct DataSource access within a transaction (i.e. plain JDBC code working
- * with the same DataSource). This allows for mixing services which access Hibernate
- * and services which use plain JDBC (without being aware of Hibernate)!
- * Application code needs to stick to the same simple Connection lookup pattern as
- * with {@link org.springframework.jdbc.datasource.DataSourceTransactionManager}
- * (i.e. {@link org.springframework.jdbc.datasource.DataSourceUtils#getConnection}
- * or going through a
- * {@link org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy}).
- *
- *
Note: To be able to register a DataSource's Connection for plain JDBC code,
- * this instance needs to be aware of the DataSource ({@link #setDataSource}).
- * The given DataSource should obviously match the one used by the given SessionFactory.
- *
- *
JTA (usually through {@link org.springframework.transaction.jta.JtaTransactionManager})
- * is necessary for accessing multiple transactional resources within the same
- * transaction. The DataSource that Hibernate uses needs to be JTA-enabled in
- * such a scenario (see container setup).
- *
- *
This transaction manager supports nested transactions via JDBC 3.0 Savepoints.
- * The {@link #setNestedTransactionAllowed} "nestedTransactionAllowed"} flag defaults
- * to "false", though, as nested transactions will just apply to the JDBC Connection,
- * not to the Hibernate Session and its cached entity objects and related context.
- * You can manually set the flag to "true" if you want to use nested transactions
- * for JDBC access code which participates in Hibernate transactions (provided that
- * your JDBC driver supports Savepoints). Note that Hibernate itself does not
- * support nested transactions! Hence, do not expect Hibernate access code to
- * semantically participate in a nested transaction.
- *
- *
NOTE: Hibernate 4.2+ is strongly recommended for efficient transaction
- * management with Spring, in particular for transactional Spring JDBC access.
- *
- * @author Juergen Hoeller
- * @since 3.1
- * @see #setSessionFactory
- * @see #setDataSource
- * @see org.hibernate.SessionFactory#getCurrentSession()
- * @see org.springframework.jdbc.datasource.DataSourceUtils#getConnection
- * @see org.springframework.jdbc.datasource.DataSourceUtils#releaseConnection
- * @see org.springframework.jdbc.core.JdbcTemplate
- * @see org.springframework.jdbc.datasource.DataSourceTransactionManager
- * @see org.springframework.transaction.jta.JtaTransactionManager
- */
-@SuppressWarnings("serial")
-public class HibernateTransactionManager extends AbstractPlatformTransactionManager
- implements ResourceTransactionManager, BeanFactoryAware, InitializingBean {
-
- private SessionFactory sessionFactory;
-
- private DataSource dataSource;
-
- private boolean autodetectDataSource = true;
-
- private boolean prepareConnection = true;
-
- private boolean allowResultAccessAfterCompletion = false;
-
- private boolean hibernateManagedSession = false;
-
- private Object entityInterceptor;
-
- /**
- * Just needed for entityInterceptorBeanName.
- * @see #setEntityInterceptorBeanName
- */
- private BeanFactory beanFactory;
-
-
- /**
- * Create a new HibernateTransactionManager instance.
- * A SessionFactory has to be set to be able to use it.
- * @see #setSessionFactory
- */
- public HibernateTransactionManager() {
- }
-
- /**
- * Create a new HibernateTransactionManager instance.
- * @param sessionFactory SessionFactory to manage transactions for
- */
- public HibernateTransactionManager(SessionFactory sessionFactory) {
- this.sessionFactory = sessionFactory;
- afterPropertiesSet();
- }
-
-
- /**
- * Set the SessionFactory that this instance should manage transactions for.
- */
- public void setSessionFactory(SessionFactory sessionFactory) {
- this.sessionFactory = sessionFactory;
- }
-
- /**
- * Return the SessionFactory that this instance should manage transactions for.
- */
- public SessionFactory getSessionFactory() {
- return this.sessionFactory;
- }
-
- /**
- * Set the JDBC DataSource that this instance should manage transactions for.
- * The DataSource should match the one used by the Hibernate SessionFactory:
- * for example, you could specify the same JNDI DataSource for both.
- *
If the SessionFactory was configured with LocalDataSourceConnectionProvider,
- * i.e. by Spring's LocalSessionFactoryBean with a specified "dataSource",
- * the DataSource will be auto-detected: You can still explicitly specify the
- * DataSource, but you don't need to in this case.
- *
A transactional JDBC Connection for this DataSource will be provided to
- * application code accessing this DataSource directly via DataSourceUtils
- * or JdbcTemplate. The Connection will be taken from the Hibernate Session.
- *
The DataSource specified here should be the target DataSource to manage
- * transactions for, not a TransactionAwareDataSourceProxy. Only data access
- * code may work with TransactionAwareDataSourceProxy, while the transaction
- * manager needs to work on the underlying target DataSource. If there's
- * nevertheless a TransactionAwareDataSourceProxy passed in, it will be
- * unwrapped to extract its target DataSource.
- * @see #setAutodetectDataSource
- * @see org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy
- * @see org.springframework.jdbc.datasource.DataSourceUtils
- * @see org.springframework.jdbc.core.JdbcTemplate
- */
- public void setDataSource(DataSource dataSource) {
- if (dataSource instanceof TransactionAwareDataSourceProxy) {
- // If we got a TransactionAwareDataSourceProxy, we need to perform transactions
- // for its underlying target DataSource, else data access code won't see
- // properly exposed transactions (i.e. transactions for the target DataSource).
- this.dataSource = ((TransactionAwareDataSourceProxy) dataSource).getTargetDataSource();
- }
- else {
- this.dataSource = dataSource;
- }
- }
-
- /**
- * Return the JDBC DataSource that this instance manages transactions for.
- */
- public DataSource getDataSource() {
- return this.dataSource;
- }
-
- /**
- * Set whether to autodetect a JDBC DataSource used by the Hibernate SessionFactory,
- * if set via LocalSessionFactoryBean's {@code setDataSource}. Default is "true".
- *
Can be turned off to deliberately ignore an available DataSource, in order
- * to not expose Hibernate transactions as JDBC transactions for that DataSource.
- * @see #setDataSource
- */
- public void setAutodetectDataSource(boolean autodetectDataSource) {
- this.autodetectDataSource = autodetectDataSource;
- }
-
- /**
- * Set whether to prepare the underlying JDBC Connection of a transactional
- * Hibernate Session, that is, whether to apply a transaction-specific
- * isolation level and/or the transaction's read-only flag to the underlying
- * JDBC Connection.
- *
Default is "true". If you turn this flag off, the transaction manager
- * will not support per-transaction isolation levels anymore. It will not
- * call {@code Connection.setReadOnly(true)} for read-only transactions
- * anymore either. If this flag is turned off, no cleanup of a JDBC Connection
- * is required after a transaction, since no Connection settings will get modified.
- * @see java.sql.Connection#setTransactionIsolation
- * @see java.sql.Connection#setReadOnly
- */
- public void setPrepareConnection(boolean prepareConnection) {
- this.prepareConnection = prepareConnection;
- }
-
- /**
- * Set whether to allow result access after completion, typically via Hibernate's
- * ScrollableResults mechanism.
- *
Default is "false". Turning this flag on enforces over-commit holdability on the
- * underlying JDBC Connection (if {@link #prepareConnection "prepareConnection"} is on)
- * and skips the disconnect-on-completion step.
- * @since 4.1.2
- * @see java.sql.Connection#setHoldability
- * @see ResultSet#HOLD_CURSORS_OVER_COMMIT
- * @see #disconnectOnCompletion(Session)
- */
- public void setAllowResultAccessAfterCompletion(boolean allowResultAccessAfterCompletion) {
- this.allowResultAccessAfterCompletion = allowResultAccessAfterCompletion;
- }
-
- /**
- * Set whether to operate on a Hibernate-managed Session instead of a
- * Spring-managed Session, that is, whether to obtain the Session through
- * Hibernate's {@link org.hibernate.SessionFactory#getCurrentSession()}
- * instead of {@link org.hibernate.SessionFactory#openSession()} (with a Spring
- * {@link org.springframework.transaction.support.TransactionSynchronizationManager}
- * check preceding it).
- *
Default is "false", i.e. using a Spring-managed Session: taking the current
- * thread-bound Session if available (e.g. in an Open-Session-in-View scenario),
- * creating a new Session for the current transaction otherwise.
- *
Switch this flag to "true" in order to enforce use of a Hibernate-managed Session.
- * Note that this requires {@link org.hibernate.SessionFactory#getCurrentSession()}
- * to always return a proper Session when called for a Spring-managed transaction;
- * transaction begin will fail if the {@code getCurrentSession()} call fails.
- *
This mode will typically be used in combination with a custom Hibernate
- * {@link org.hibernate.context.spi.CurrentSessionContext} implementation that stores
- * Sessions in a place other than Spring's TransactionSynchronizationManager.
- * It may also be used in combination with Spring's Open-Session-in-View support
- * (using Spring's default {@link SpringSessionContext}), in which case it subtly
- * differs from the Spring-managed Session mode: The pre-bound Session will not
- * receive a {@code clear()} call (on rollback) or a {@code disconnect()}
- * call (on transaction completion) in such a scenario; this is rather left up
- * to a custom CurrentSessionContext implementation (if desired).
- */
- public void setHibernateManagedSession(boolean hibernateManagedSession) {
- this.hibernateManagedSession = hibernateManagedSession;
- }
-
- /**
- * Set the bean name of a Hibernate entity interceptor that allows to inspect
- * and change property values before writing to and reading from the database.
- * Will get applied to any new Session created by this transaction manager.
- *
Requires the bean factory to be known, to be able to resolve the bean
- * name to an interceptor instance on session creation. Typically used for
- * prototype interceptors, i.e. a new interceptor instance per session.
- *
Can also be used for shared interceptor instances, but it is recommended
- * to set the interceptor reference directly in such a scenario.
- * @param entityInterceptorBeanName the name of the entity interceptor in
- * the bean factory
- * @see #setBeanFactory
- * @see #setEntityInterceptor
- */
- public void setEntityInterceptorBeanName(String entityInterceptorBeanName) {
- this.entityInterceptor = entityInterceptorBeanName;
- }
-
- /**
- * Set a Hibernate entity interceptor that allows to inspect and change
- * property values before writing to and reading from the database.
- * Will get applied to any new Session created by this transaction manager.
- *
Such an interceptor can either be set at the SessionFactory level,
- * i.e. on LocalSessionFactoryBean, or at the Session level, i.e. on
- * HibernateTransactionManager.
- * @see LocalSessionFactoryBean#setEntityInterceptor
- */
- public void setEntityInterceptor(Interceptor entityInterceptor) {
- this.entityInterceptor = entityInterceptor;
- }
-
- /**
- * Return the current Hibernate entity interceptor, or {@code null} if none.
- * Resolves an entity interceptor bean name via the bean factory,
- * if necessary.
- * @throws IllegalStateException if bean name specified but no bean factory set
- * @throws BeansException if bean name resolution via the bean factory failed
- * @see #setEntityInterceptor
- * @see #setEntityInterceptorBeanName
- * @see #setBeanFactory
- */
- public Interceptor getEntityInterceptor() throws IllegalStateException, BeansException {
- if (this.entityInterceptor instanceof Interceptor) {
- return (Interceptor) entityInterceptor;
- }
- else if (this.entityInterceptor instanceof String) {
- if (this.beanFactory == null) {
- throw new IllegalStateException("Cannot get entity interceptor via bean name if no bean factory set");
- }
- String beanName = (String) this.entityInterceptor;
- return this.beanFactory.getBean(beanName, Interceptor.class);
- }
- else {
- return null;
- }
- }
-
- /**
- * The bean factory just needs to be known for resolving entity interceptor
- * bean names. It does not need to be set for any other mode of operation.
- * @see #setEntityInterceptorBeanName
- */
- @Override
- public void setBeanFactory(BeanFactory beanFactory) {
- this.beanFactory = beanFactory;
- }
-
- @Override
- public void afterPropertiesSet() {
- if (getSessionFactory() == null) {
- throw new IllegalArgumentException("Property 'sessionFactory' is required");
- }
- if (this.entityInterceptor instanceof String && this.beanFactory == null) {
- throw new IllegalArgumentException("Property 'beanFactory' is required for 'entityInterceptorBeanName'");
- }
-
- // Check for SessionFactory's DataSource.
- if (this.autodetectDataSource && getDataSource() == null) {
- DataSource sfds = SessionFactoryUtils.getDataSource(getSessionFactory());
- if (sfds != null) {
- // Use the SessionFactory's DataSource for exposing transactions to JDBC code.
- if (logger.isInfoEnabled()) {
- logger.info("Using DataSource [" + sfds +
- "] of Hibernate SessionFactory for HibernateTransactionManager");
- }
- setDataSource(sfds);
- }
- }
- }
-
-
- @Override
- public Object getResourceFactory() {
- return getSessionFactory();
- }
-
- @Override
- protected Object doGetTransaction() {
- HibernateTransactionObject txObject = new HibernateTransactionObject();
- txObject.setSavepointAllowed(isNestedTransactionAllowed());
-
- SessionHolder sessionHolder =
- (SessionHolder) TransactionSynchronizationManager.getResource(getSessionFactory());
- if (sessionHolder != null) {
- if (logger.isDebugEnabled()) {
- logger.debug("Found thread-bound Session [" + sessionHolder.getSession() + "] for Hibernate transaction");
- }
- txObject.setSessionHolder(sessionHolder);
- }
- else if (this.hibernateManagedSession) {
- try {
- Session session = this.sessionFactory.getCurrentSession();
- if (logger.isDebugEnabled()) {
- logger.debug("Found Hibernate-managed Session [" + session + "] for Spring-managed transaction");
- }
- txObject.setExistingSession(session);
- }
- catch (HibernateException ex) {
- throw new DataAccessResourceFailureException(
- "Could not obtain Hibernate-managed Session for Spring-managed transaction", ex);
- }
- }
-
- if (getDataSource() != null) {
- ConnectionHolder conHolder = (ConnectionHolder)
- TransactionSynchronizationManager.getResource(getDataSource());
- txObject.setConnectionHolder(conHolder);
- }
-
- return txObject;
- }
-
- @Override
- protected boolean isExistingTransaction(Object transaction) {
- HibernateTransactionObject txObject = (HibernateTransactionObject) transaction;
- return (txObject.hasSpringManagedTransaction() ||
- (this.hibernateManagedSession && txObject.hasHibernateManagedTransaction()));
- }
-
- @Override
- protected void doBegin(Object transaction, TransactionDefinition definition) {
- HibernateTransactionObject txObject = (HibernateTransactionObject) transaction;
-
- if (txObject.hasConnectionHolder() && !txObject.getConnectionHolder().isSynchronizedWithTransaction()) {
- throw new IllegalTransactionStateException(
- "Pre-bound JDBC Connection found! HibernateTransactionManager does not support " +
- "running within DataSourceTransactionManager if told to manage the DataSource itself. " +
- "It is recommended to use a single HibernateTransactionManager for all transactions " +
- "on a single DataSource, no matter whether Hibernate or JDBC access.");
- }
-
- Session session = null;
-
- try {
- if (txObject.getSessionHolder() == null || txObject.getSessionHolder().isSynchronizedWithTransaction()) {
- Interceptor entityInterceptor = getEntityInterceptor();
- Session newSession = (entityInterceptor != null ?
- getSessionFactory().withOptions().interceptor(entityInterceptor).openSession() :
- getSessionFactory().openSession());
- if (logger.isDebugEnabled()) {
- logger.debug("Opened new Session [" + newSession + "] for Hibernate transaction");
- }
- txObject.setSession(newSession);
- }
-
- session = txObject.getSessionHolder().getSession();
-
- if (this.prepareConnection && isSameConnectionForEntireSession(session)) {
- // We're allowed to change the transaction settings of the JDBC Connection.
- if (logger.isDebugEnabled()) {
- logger.debug("Preparing JDBC Connection of Hibernate Session [" + session + "]");
- }
- Connection con = ((SessionImplementor) session).connection();
- Integer previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(con, definition);
- txObject.setPreviousIsolationLevel(previousIsolationLevel);
- if (this.allowResultAccessAfterCompletion && !txObject.isNewSession()) {
- int currentHoldability = con.getHoldability();
- if (currentHoldability != ResultSet.HOLD_CURSORS_OVER_COMMIT) {
- txObject.setPreviousHoldability(currentHoldability);
- con.setHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT);
- }
- }
- }
- else {
- // Not allowed to change the transaction settings of the JDBC Connection.
- if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
- // We should set a specific isolation level but are not allowed to...
- throw new InvalidIsolationLevelException(
- "HibernateTransactionManager is not allowed to support custom isolation levels: " +
- "make sure that its 'prepareConnection' flag is on (the default) and that the " +
- "Hibernate connection release mode is set to 'on_close' (the default for JDBC).");
- }
- if (logger.isDebugEnabled()) {
- logger.debug("Not preparing JDBC Connection of Hibernate Session [" + session + "]");
- }
- }
-
- if (definition.isReadOnly() && txObject.isNewSession()) {
- // Just set to MANUAL in case of a new Session for this transaction.
- session.setFlushMode(FlushMode.MANUAL);
- }
-
- if (!definition.isReadOnly() && !txObject.isNewSession()) {
- // We need AUTO or COMMIT for a non-read-only transaction.
- FlushMode flushMode = session.getFlushMode();
- if (FlushMode.MANUAL.equals(flushMode)) {
- session.setFlushMode(FlushMode.AUTO);
- txObject.getSessionHolder().setPreviousFlushMode(flushMode);
- }
- }
-
- Transaction hibTx;
-
- // Register transaction timeout.
- int timeout = determineTimeout(definition);
- if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) {
- // Use Hibernate's own transaction timeout mechanism on Hibernate 3.1+
- // Applies to all statements, also to inserts, updates and deletes!
- hibTx = session.getTransaction();
- hibTx.setTimeout(timeout);
- hibTx.begin();
- }
- else {
- // Open a plain Hibernate transaction without specified timeout.
- hibTx = session.beginTransaction();
- }
-
- // Add the Hibernate transaction to the session holder.
- txObject.getSessionHolder().setTransaction(hibTx);
-
- // Register the Hibernate Session's JDBC Connection for the DataSource, if set.
- if (getDataSource() != null) {
- Connection con = ((SessionImplementor) session).connection();
- ConnectionHolder conHolder = new ConnectionHolder(con);
- if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) {
- conHolder.setTimeoutInSeconds(timeout);
- }
- if (logger.isDebugEnabled()) {
- logger.debug("Exposing Hibernate transaction as JDBC transaction [" + con + "]");
- }
- TransactionSynchronizationManager.bindResource(getDataSource(), conHolder);
- txObject.setConnectionHolder(conHolder);
- }
-
- // Bind the session holder to the thread.
- if (txObject.isNewSessionHolder()) {
- TransactionSynchronizationManager.bindResource(getSessionFactory(), txObject.getSessionHolder());
- }
- txObject.getSessionHolder().setSynchronizedWithTransaction(true);
- }
-
- catch (Throwable ex) {
- if (txObject.isNewSession()) {
- try {
- if (session.getTransaction().isActive()) {
- session.getTransaction().rollback();
- }
- }
- catch (Throwable ex2) {
- logger.debug("Could not rollback Session after failed transaction begin", ex);
- }
- finally {
- SessionFactoryUtils.closeSession(session);
- txObject.setSessionHolder(null);
- }
- }
- throw new CannotCreateTransactionException("Could not open Hibernate Session for transaction", ex);
- }
- }
-
- @Override
- protected Object doSuspend(Object transaction) {
- HibernateTransactionObject txObject = (HibernateTransactionObject) transaction;
- txObject.setSessionHolder(null);
- SessionHolder sessionHolder =
- (SessionHolder) TransactionSynchronizationManager.unbindResource(getSessionFactory());
- txObject.setConnectionHolder(null);
- ConnectionHolder connectionHolder = null;
- if (getDataSource() != null) {
- connectionHolder = (ConnectionHolder) TransactionSynchronizationManager.unbindResource(getDataSource());
- }
- return new SuspendedResourcesHolder(sessionHolder, connectionHolder);
- }
-
- @Override
- protected void doResume(Object transaction, Object suspendedResources) {
- SuspendedResourcesHolder resourcesHolder = (SuspendedResourcesHolder) suspendedResources;
- if (TransactionSynchronizationManager.hasResource(getSessionFactory())) {
- // From non-transactional code running in active transaction synchronization
- // -> can be safely removed, will be closed on transaction completion.
- TransactionSynchronizationManager.unbindResource(getSessionFactory());
- }
- TransactionSynchronizationManager.bindResource(getSessionFactory(), resourcesHolder.getSessionHolder());
- if (getDataSource() != null) {
- TransactionSynchronizationManager.bindResource(getDataSource(), resourcesHolder.getConnectionHolder());
- }
- }
-
- @Override
- protected void doCommit(DefaultTransactionStatus status) {
- HibernateTransactionObject txObject = (HibernateTransactionObject) status.getTransaction();
- if (status.isDebug()) {
- logger.debug("Committing Hibernate transaction on Session [" +
- txObject.getSessionHolder().getSession() + "]");
- }
- try {
- txObject.getSessionHolder().getTransaction().commit();
- }
- catch (org.hibernate.TransactionException ex) {
- // assumably from commit call to the underlying JDBC connection
- throw new TransactionSystemException("Could not commit Hibernate transaction", ex);
- }
- catch (HibernateException ex) {
- // assumably failed to flush changes to database
- throw convertHibernateAccessException(ex);
- }
- }
-
- @Override
- protected void doRollback(DefaultTransactionStatus status) {
- HibernateTransactionObject txObject = (HibernateTransactionObject) status.getTransaction();
- if (status.isDebug()) {
- logger.debug("Rolling back Hibernate transaction on Session [" +
- txObject.getSessionHolder().getSession() + "]");
- }
- try {
- txObject.getSessionHolder().getTransaction().rollback();
- }
- catch (org.hibernate.TransactionException ex) {
- throw new TransactionSystemException("Could not roll back Hibernate transaction", ex);
- }
- catch (HibernateException ex) {
- // Shouldn't really happen, as a rollback doesn't cause a flush.
- throw convertHibernateAccessException(ex);
- }
- finally {
- if (!txObject.isNewSession() && !this.hibernateManagedSession) {
- // Clear all pending inserts/updates/deletes in the Session.
- // Necessary for pre-bound Sessions, to avoid inconsistent state.
- txObject.getSessionHolder().getSession().clear();
- }
- }
- }
-
- @Override
- protected void doSetRollbackOnly(DefaultTransactionStatus status) {
- HibernateTransactionObject txObject = (HibernateTransactionObject) status.getTransaction();
- if (status.isDebug()) {
- logger.debug("Setting Hibernate transaction on Session [" +
- txObject.getSessionHolder().getSession() + "] rollback-only");
- }
- txObject.setRollbackOnly();
- }
-
- @Override
- protected void doCleanupAfterCompletion(Object transaction) {
- HibernateTransactionObject txObject = (HibernateTransactionObject) transaction;
-
- // Remove the session holder from the thread.
- if (txObject.isNewSessionHolder()) {
- TransactionSynchronizationManager.unbindResource(getSessionFactory());
- }
-
- // Remove the JDBC connection holder from the thread, if exposed.
- if (getDataSource() != null) {
- TransactionSynchronizationManager.unbindResource(getDataSource());
- }
-
- Session session = txObject.getSessionHolder().getSession();
- if (this.prepareConnection && session.isConnected() && isSameConnectionForEntireSession(session)) {
- // We're running with connection release mode "on_close": We're able to reset
- // the isolation level and/or read-only flag of the JDBC Connection here.
- // Else, we need to rely on the connection pool to perform proper cleanup.
- try {
- Connection con = ((SessionImplementor) session).connection();
- Integer previousHoldability = txObject.getPreviousHoldability();
- if (previousHoldability != null) {
- con.setHoldability(previousHoldability);
- }
- DataSourceUtils.resetConnectionAfterTransaction(con, txObject.getPreviousIsolationLevel());
- }
- catch (HibernateException ex) {
- logger.debug("Could not access JDBC Connection of Hibernate Session", ex);
- }
- catch (Throwable ex) {
- logger.debug("Could not reset JDBC Connection after transaction", ex);
- }
- }
-
- if (txObject.isNewSession()) {
- if (logger.isDebugEnabled()) {
- logger.debug("Closing Hibernate Session [" + session + "] after transaction");
- }
- SessionFactoryUtils.closeSession(session);
- }
- else {
- if (logger.isDebugEnabled()) {
- logger.debug("Not closing pre-bound Hibernate Session [" + session + "] after transaction");
- }
- if (txObject.getSessionHolder().getPreviousFlushMode() != null) {
- session.setFlushMode(txObject.getSessionHolder().getPreviousFlushMode());
- }
- if (!this.allowResultAccessAfterCompletion && !this.hibernateManagedSession) {
- disconnectOnCompletion(session);
- }
- }
- txObject.getSessionHolder().clear();
- }
-
- /**
- * Disconnect a pre-existing Hibernate Session on transaction completion,
- * returning its database connection but preserving its entity state.
- *
The default implementation simply calls {@link Session#disconnect()}.
- * Subclasses may override this with a no-op or with fine-tuned disconnection logic.
- * @param session the Hibernate Session to disconnect
- * @since 4.1.2
- * @see org.hibernate.Session#disconnect()
- */
- protected void disconnectOnCompletion(Session session) {
- session.disconnect();
- }
-
- /**
- * Return whether the given Hibernate Session will always hold the same
- * JDBC Connection. This is used to check whether the transaction manager
- * can safely prepare and clean up the JDBC Connection used for a transaction.
- *
The default implementation checks the Session's connection release mode
- * to be "on_close".
- * @param session the Hibernate Session to check
- * @see org.hibernate.engine.transaction.spi.TransactionContext#getConnectionReleaseMode()
- * @see org.hibernate.ConnectionReleaseMode#ON_CLOSE
- */
- protected boolean isSameConnectionForEntireSession(Session session) {
- if (!(session instanceof TransactionContext)) {
- // The best we can do is to assume we're safe.
- return true;
- }
- ConnectionReleaseMode releaseMode = ((TransactionContext) session).getConnectionReleaseMode();
- return ConnectionReleaseMode.ON_CLOSE.equals(releaseMode);
- }
-
-
- /**
- * Convert the given HibernateException to an appropriate exception
- * from the {@code org.springframework.dao} hierarchy.
- *
Will automatically apply a specified SQLExceptionTranslator to a
- * Hibernate JDBCException, else rely on Hibernate's default translation.
- * @param ex HibernateException that occurred
- * @return a corresponding DataAccessException
- * @see SessionFactoryUtils#convertHibernateAccessException
- */
- protected DataAccessException convertHibernateAccessException(HibernateException ex) {
- return SessionFactoryUtils.convertHibernateAccessException(ex);
- }
-
-
- /**
- * Hibernate transaction object, representing a SessionHolder.
- * Used as transaction object by HibernateTransactionManager.
- */
- private class HibernateTransactionObject extends JdbcTransactionObjectSupport {
-
- private SessionHolder sessionHolder;
-
- private boolean newSessionHolder;
-
- private boolean newSession;
-
- private Integer previousHoldability;
-
- public void setSession(Session session) {
- this.sessionHolder = new SessionHolder(session);
- this.newSessionHolder = true;
- this.newSession = true;
- }
-
- public void setExistingSession(Session session) {
- this.sessionHolder = new SessionHolder(session);
- this.newSessionHolder = true;
- this.newSession = false;
- }
-
- public void setSessionHolder(SessionHolder sessionHolder) {
- this.sessionHolder = sessionHolder;
- this.newSessionHolder = false;
- this.newSession = false;
- }
-
- public SessionHolder getSessionHolder() {
- return this.sessionHolder;
- }
-
- public boolean isNewSessionHolder() {
- return this.newSessionHolder;
- }
-
- public boolean isNewSession() {
- return this.newSession;
- }
-
- public void setPreviousHoldability(Integer previousHoldability) {
- this.previousHoldability = previousHoldability;
- }
-
- public Integer getPreviousHoldability() {
- return this.previousHoldability;
- }
-
- public boolean hasSpringManagedTransaction() {
- return (this.sessionHolder != null && this.sessionHolder.getTransaction() != null);
- }
-
- public boolean hasHibernateManagedTransaction() {
- return (this.sessionHolder != null && this.sessionHolder.getSession().getTransaction().isActive());
- }
-
- public void setRollbackOnly() {
- this.sessionHolder.setRollbackOnly();
- if (hasConnectionHolder()) {
- getConnectionHolder().setRollbackOnly();
- }
- }
-
- @Override
- public boolean isRollbackOnly() {
- return this.sessionHolder.isRollbackOnly() ||
- (hasConnectionHolder() && getConnectionHolder().isRollbackOnly());
- }
-
- @Override
- public void flush() {
- try {
- this.sessionHolder.getSession().flush();
- }
- catch (HibernateException ex) {
- throw convertHibernateAccessException(ex);
- }
- }
- }
-
-
- /**
- * Holder for suspended resources.
- * Used internally by {@code doSuspend} and {@code doResume}.
- */
- private static class SuspendedResourcesHolder {
-
- private final SessionHolder sessionHolder;
-
- private final ConnectionHolder connectionHolder;
-
- private SuspendedResourcesHolder(SessionHolder sessionHolder, ConnectionHolder conHolder) {
- this.sessionHolder = sessionHolder;
- this.connectionHolder = conHolder;
- }
-
- private SessionHolder getSessionHolder() {
- return this.sessionHolder;
- }
-
- private ConnectionHolder getConnectionHolder() {
- return this.connectionHolder;
- }
- }
-
-}
diff --git a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/LocalSessionFactoryBean.java b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/LocalSessionFactoryBean.java
deleted file mode 100644
index 7a42e295d6..0000000000
--- a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/LocalSessionFactoryBean.java
+++ /dev/null
@@ -1,491 +0,0 @@
-/*
- * Copyright 2002-2014 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate4;
-
-import java.io.File;
-import java.io.IOException;
-import java.util.Properties;
-import javax.sql.DataSource;
-
-import org.hibernate.Interceptor;
-import org.hibernate.SessionFactory;
-import org.hibernate.cache.spi.RegionFactory;
-import org.hibernate.cfg.Configuration;
-
-import org.springframework.beans.factory.DisposableBean;
-import org.springframework.beans.factory.FactoryBean;
-import org.springframework.beans.factory.InitializingBean;
-import org.springframework.context.ResourceLoaderAware;
-import org.springframework.core.io.ClassPathResource;
-import org.springframework.core.io.Resource;
-import org.springframework.core.io.ResourceLoader;
-import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
-import org.springframework.core.io.support.ResourcePatternResolver;
-import org.springframework.core.io.support.ResourcePatternUtils;
-import org.springframework.core.type.filter.TypeFilter;
-
-/**
- * {@link org.springframework.beans.factory.FactoryBean} that creates a Hibernate
- * {@link org.hibernate.SessionFactory}. This is the usual way to set up a shared
- * Hibernate SessionFactory in a Spring application context; the SessionFactory can
- * then be passed to Hibernate-based data access objects via dependency injection.
- *
- *
This variant of LocalSessionFactoryBean requires Hibernate 4.0 or higher.
- * As of Spring 4.0, it is compatible with (the quite refactored) Hibernate 4.3 as well.
- * We recommend using the latest Hibernate 4.2.x or 4.3.x version, depending on whether
- * you need to remain JPA 2.0 compatible at runtime (Hibernate 4.2) or can upgrade to
- * JPA 2.1 (Hibernate 4.3).
- *
- *
This class is similar in role to the same-named class in the {@code orm.hibernate3}
- * package. However, in practice, it is closer to {@code AnnotationSessionFactoryBean}
- * since its core purpose is to bootstrap a {@code SessionFactory} from package scanning.
- *
- *
NOTE: To set up Hibernate 4 for Spring-driven JTA transactions, make
- * sure to either specify the {@link #setJtaTransactionManager "jtaTransactionManager"}
- * bean property or to set the "hibernate.transaction.factory_class" property to
- * {@link org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory}.
- * Otherwise, Hibernate's smart flushing mechanism won't work properly.
- *
- * @author Juergen Hoeller
- * @since 3.1
- * @see #setDataSource
- * @see #setPackagesToScan
- * @see LocalSessionFactoryBuilder
- */
-public class LocalSessionFactoryBean extends HibernateExceptionTranslator
- implements FactoryBean, ResourceLoaderAware, InitializingBean, DisposableBean {
-
- private DataSource dataSource;
-
- private Resource[] configLocations;
-
- private String[] mappingResources;
-
- private Resource[] mappingLocations;
-
- private Resource[] cacheableMappingLocations;
-
- private Resource[] mappingJarLocations;
-
- private Resource[] mappingDirectoryLocations;
-
- private Interceptor entityInterceptor;
-
- @SuppressWarnings("deprecation")
- private org.hibernate.cfg.NamingStrategy namingStrategy;
-
- private Object jtaTransactionManager;
-
- private Object multiTenantConnectionProvider;
-
- private Object currentTenantIdentifierResolver;
-
- private RegionFactory cacheRegionFactory;
-
- private TypeFilter[] entityTypeFilters;
-
- private Properties hibernateProperties;
-
- private Class>[] annotatedClasses;
-
- private String[] annotatedPackages;
-
- private String[] packagesToScan;
-
- private ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
-
- private Configuration configuration;
-
- private SessionFactory sessionFactory;
-
-
- /**
- * Set the DataSource to be used by the SessionFactory.
- * If set, this will override corresponding settings in Hibernate properties.
- * If this is set, the Hibernate settings should not define
- * a connection provider to avoid meaningless double configuration.
- */
- public void setDataSource(DataSource dataSource) {
- this.dataSource = dataSource;
- }
-
- /**
- * Set the location of a single Hibernate XML config file, for example as
- * classpath resource "classpath:hibernate.cfg.xml".
- *
Note: Can be omitted when all necessary properties and mapping
- * resources are specified locally via this bean.
- * @see org.hibernate.cfg.Configuration#configure(java.net.URL)
- */
- public void setConfigLocation(Resource configLocation) {
- this.configLocations = new Resource[] {configLocation};
- }
-
- /**
- * Set the locations of multiple Hibernate XML config files, for example as
- * classpath resources "classpath:hibernate.cfg.xml,classpath:extension.cfg.xml".
- *
Note: Can be omitted when all necessary properties and mapping
- * resources are specified locally via this bean.
- * @see org.hibernate.cfg.Configuration#configure(java.net.URL)
- */
- public void setConfigLocations(Resource... configLocations) {
- this.configLocations = configLocations;
- }
-
- /**
- * Set Hibernate mapping resources to be found in the class path,
- * like "example.hbm.xml" or "mypackage/example.hbm.xml".
- * Analogous to mapping entries in a Hibernate XML config file.
- * Alternative to the more generic setMappingLocations method.
- *
Can be used to add to mappings from a Hibernate XML config file,
- * or to specify all mappings locally.
- * @see #setMappingLocations
- * @see org.hibernate.cfg.Configuration#addResource
- */
- public void setMappingResources(String... mappingResources) {
- this.mappingResources = mappingResources;
- }
-
- /**
- * Set locations of Hibernate mapping files, for example as classpath
- * resource "classpath:example.hbm.xml". Supports any resource location
- * via Spring's resource abstraction, for example relative paths like
- * "WEB-INF/mappings/example.hbm.xml" when running in an application context.
- *
Can be used to add to mappings from a Hibernate XML config file,
- * or to specify all mappings locally.
- * @see org.hibernate.cfg.Configuration#addInputStream
- */
- public void setMappingLocations(Resource... mappingLocations) {
- this.mappingLocations = mappingLocations;
- }
-
- /**
- * Set locations of cacheable Hibernate mapping files, for example as web app
- * resource "/WEB-INF/mapping/example.hbm.xml". Supports any resource location
- * via Spring's resource abstraction, as long as the resource can be resolved
- * in the file system.
- *
Can be used to add to mappings from a Hibernate XML config file,
- * or to specify all mappings locally.
- * @see org.hibernate.cfg.Configuration#addCacheableFile(java.io.File)
- */
- public void setCacheableMappingLocations(Resource... cacheableMappingLocations) {
- this.cacheableMappingLocations = cacheableMappingLocations;
- }
-
- /**
- * Set locations of jar files that contain Hibernate mapping resources,
- * like "WEB-INF/lib/example.hbm.jar".
- *
Can be used to add to mappings from a Hibernate XML config file,
- * or to specify all mappings locally.
- * @see org.hibernate.cfg.Configuration#addJar(java.io.File)
- */
- public void setMappingJarLocations(Resource... mappingJarLocations) {
- this.mappingJarLocations = mappingJarLocations;
- }
-
- /**
- * Set locations of directories that contain Hibernate mapping resources,
- * like "WEB-INF/mappings".
- *
Can be used to add to mappings from a Hibernate XML config file,
- * or to specify all mappings locally.
- * @see org.hibernate.cfg.Configuration#addDirectory(java.io.File)
- */
- public void setMappingDirectoryLocations(Resource... mappingDirectoryLocations) {
- this.mappingDirectoryLocations = mappingDirectoryLocations;
- }
-
- /**
- * Set a Hibernate entity interceptor that allows to inspect and change
- * property values before writing to and reading from the database.
- * Will get applied to any new Session created by this factory.
- * @see org.hibernate.cfg.Configuration#setInterceptor
- */
- public void setEntityInterceptor(Interceptor entityInterceptor) {
- this.entityInterceptor = entityInterceptor;
- }
-
- /**
- * Set a Hibernate NamingStrategy for the SessionFactory, determining the
- * physical column and table names given the info in the mapping document.
- * @see org.hibernate.cfg.Configuration#setNamingStrategy
- */
- @SuppressWarnings("deprecation")
- public void setNamingStrategy(org.hibernate.cfg.NamingStrategy namingStrategy) {
- this.namingStrategy = namingStrategy;
- }
-
- /**
- * Set the Spring {@link org.springframework.transaction.jta.JtaTransactionManager}
- * or the JTA {@link javax.transaction.TransactionManager} to be used with Hibernate,
- * if any. Implicitly sets up {@code JtaPlatform} and {@code CMTTransactionStrategy}.
- * @see LocalSessionFactoryBuilder#setJtaTransactionManager
- */
- public void setJtaTransactionManager(Object jtaTransactionManager) {
- this.jtaTransactionManager = jtaTransactionManager;
- }
-
- /**
- * Set a Hibernate 4.1/4.2/4.3 {@code MultiTenantConnectionProvider} to be passed
- * on to the SessionFactory: as an instance, a Class, or a String class name.
- *
Note that the package location of the {@code MultiTenantConnectionProvider}
- * interface changed between Hibernate 4.2 and 4.3. This method accepts both variants.
- * @since 4.0
- * @see LocalSessionFactoryBuilder#setMultiTenantConnectionProvider
- */
- public void setMultiTenantConnectionProvider(Object multiTenantConnectionProvider) {
- this.multiTenantConnectionProvider = multiTenantConnectionProvider;
- }
-
- /**
- * Set a Hibernate 4.1/4.2/4.3 {@code CurrentTenantIdentifierResolver} to be passed
- * on to the SessionFactory: as an instance, a Class, or a String class name.
- * @since 4.0
- * @see LocalSessionFactoryBuilder#setCurrentTenantIdentifierResolver
- */
- public void setCurrentTenantIdentifierResolver(Object currentTenantIdentifierResolver) {
- this.currentTenantIdentifierResolver = currentTenantIdentifierResolver;
- }
-
- /**
- * Set the Hibernate RegionFactory to use for the SessionFactory.
- * Allows for using a Spring-managed RegionFactory instance.
- *
Note: If this is set, the Hibernate settings should not define a
- * cache provider to avoid meaningless double configuration.
- * @since 4.0
- * @see org.hibernate.cache.spi.RegionFactory
- * @see LocalSessionFactoryBuilder#setCacheRegionFactory
- */
- public void setCacheRegionFactory(RegionFactory cacheRegionFactory) {
- this.cacheRegionFactory = cacheRegionFactory;
- }
-
- /**
- * Specify custom type filters for Spring-based scanning for entity classes.
- *
Default is to search all specified packages for classes annotated with
- * {@code @javax.persistence.Entity}, {@code @javax.persistence.Embeddable}
- * or {@code @javax.persistence.MappedSuperclass}.
- * @since 4.1
- * @see #setPackagesToScan
- */
- public void setEntityTypeFilters(TypeFilter... entityTypeFilters) {
- this.entityTypeFilters = entityTypeFilters;
- }
-
- /**
- * Set Hibernate properties, such as "hibernate.dialect".
- *
Note: Do not specify a transaction provider here when using
- * Spring-driven transactions. It is also advisable to omit connection
- * provider settings and use a Spring-set DataSource instead.
- * @see #setDataSource
- */
- public void setHibernateProperties(Properties hibernateProperties) {
- this.hibernateProperties = hibernateProperties;
- }
-
- /**
- * Return the Hibernate properties, if any. Mainly available for
- * configuration through property paths that specify individual keys.
- */
- public Properties getHibernateProperties() {
- if (this.hibernateProperties == null) {
- this.hibernateProperties = new Properties();
- }
- return this.hibernateProperties;
- }
-
- /**
- * Specify annotated entity classes to register with this Hibernate SessionFactory.
- * @see org.hibernate.cfg.Configuration#addAnnotatedClass(Class)
- */
- public void setAnnotatedClasses(Class>... annotatedClasses) {
- this.annotatedClasses = annotatedClasses;
- }
-
- /**
- * Specify the names of annotated packages, for which package-level
- * annotation metadata will be read.
- * @see org.hibernate.cfg.Configuration#addPackage(String)
- */
- public void setAnnotatedPackages(String... annotatedPackages) {
- this.annotatedPackages = annotatedPackages;
- }
-
- /**
- * Specify packages to search for autodetection of your entity classes in the
- * classpath. This is analogous to Spring's component-scan feature
- * ({@link org.springframework.context.annotation.ClassPathBeanDefinitionScanner}).
- */
- public void setPackagesToScan(String... packagesToScan) {
- this.packagesToScan = packagesToScan;
- }
-
- @Override
- public void setResourceLoader(ResourceLoader resourceLoader) {
- this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
- }
-
-
- @Override
- public void afterPropertiesSet() throws IOException {
- LocalSessionFactoryBuilder sfb = new LocalSessionFactoryBuilder(this.dataSource, this.resourcePatternResolver);
-
- if (this.configLocations != null) {
- for (Resource resource : this.configLocations) {
- // Load Hibernate configuration from given location.
- sfb.configure(resource.getURL());
- }
- }
-
- if (this.mappingResources != null) {
- // Register given Hibernate mapping definitions, contained in resource files.
- for (String mapping : this.mappingResources) {
- Resource mr = new ClassPathResource(mapping.trim(), this.resourcePatternResolver.getClassLoader());
- sfb.addInputStream(mr.getInputStream());
- }
- }
-
- if (this.mappingLocations != null) {
- // Register given Hibernate mapping definitions, contained in resource files.
- for (Resource resource : this.mappingLocations) {
- sfb.addInputStream(resource.getInputStream());
- }
- }
-
- if (this.cacheableMappingLocations != null) {
- // Register given cacheable Hibernate mapping definitions, read from the file system.
- for (Resource resource : this.cacheableMappingLocations) {
- sfb.addCacheableFile(resource.getFile());
- }
- }
-
- if (this.mappingJarLocations != null) {
- // Register given Hibernate mapping definitions, contained in jar files.
- for (Resource resource : this.mappingJarLocations) {
- sfb.addJar(resource.getFile());
- }
- }
-
- if (this.mappingDirectoryLocations != null) {
- // Register all Hibernate mapping definitions in the given directories.
- for (Resource resource : this.mappingDirectoryLocations) {
- File file = resource.getFile();
- if (!file.isDirectory()) {
- throw new IllegalArgumentException(
- "Mapping directory location [" + resource + "] does not denote a directory");
- }
- sfb.addDirectory(file);
- }
- }
-
- if (this.entityInterceptor != null) {
- sfb.setInterceptor(this.entityInterceptor);
- }
-
- if (this.namingStrategy != null) {
- sfb.setNamingStrategy(this.namingStrategy);
- }
-
- if (this.jtaTransactionManager != null) {
- sfb.setJtaTransactionManager(this.jtaTransactionManager);
- }
-
- if (this.multiTenantConnectionProvider != null) {
- sfb.setMultiTenantConnectionProvider(this.multiTenantConnectionProvider);
- }
-
- if (this.currentTenantIdentifierResolver != null) {
- sfb.setCurrentTenantIdentifierResolver(this.currentTenantIdentifierResolver);
- }
-
- if (this.cacheRegionFactory != null) {
- sfb.setCacheRegionFactory(this.cacheRegionFactory);
- }
-
- if (this.entityTypeFilters != null) {
- sfb.setEntityTypeFilters(this.entityTypeFilters);
- }
-
- if (this.hibernateProperties != null) {
- sfb.addProperties(this.hibernateProperties);
- }
-
- if (this.annotatedClasses != null) {
- sfb.addAnnotatedClasses(this.annotatedClasses);
- }
-
- if (this.annotatedPackages != null) {
- sfb.addPackages(this.annotatedPackages);
- }
-
- if (this.packagesToScan != null) {
- sfb.scanPackages(this.packagesToScan);
- }
-
- // Build SessionFactory instance.
- this.configuration = sfb;
- this.sessionFactory = buildSessionFactory(sfb);
- }
-
- /**
- * Subclasses can override this method to perform custom initialization
- * of the SessionFactory instance, creating it via the given Configuration
- * object that got prepared by this LocalSessionFactoryBean.
- *
The default implementation invokes LocalSessionFactoryBuilder's buildSessionFactory.
- * A custom implementation could prepare the instance in a specific way (e.g. applying
- * a custom ServiceRegistry) or use a custom SessionFactoryImpl subclass.
- * @param sfb LocalSessionFactoryBuilder prepared by this LocalSessionFactoryBean
- * @return the SessionFactory instance
- * @see LocalSessionFactoryBuilder#buildSessionFactory
- */
- protected SessionFactory buildSessionFactory(LocalSessionFactoryBuilder sfb) {
- return sfb.buildSessionFactory();
- }
-
- /**
- * Return the Hibernate Configuration object used to build the SessionFactory.
- * Allows for access to configuration metadata stored there (rarely needed).
- * @throws IllegalStateException if the Configuration object has not been initialized yet
- */
- public final Configuration getConfiguration() {
- if (this.configuration == null) {
- throw new IllegalStateException("Configuration not initialized yet");
- }
- return this.configuration;
- }
-
-
- @Override
- public SessionFactory getObject() {
- return this.sessionFactory;
- }
-
- @Override
- public Class> getObjectType() {
- return (this.sessionFactory != null ? this.sessionFactory.getClass() : SessionFactory.class);
- }
-
- @Override
- public boolean isSingleton() {
- return true;
- }
-
-
- @Override
- public void destroy() {
- this.sessionFactory.close();
- }
-
-}
diff --git a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/LocalSessionFactoryBuilder.java b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/LocalSessionFactoryBuilder.java
deleted file mode 100644
index dcbfbdb51c..0000000000
--- a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/LocalSessionFactoryBuilder.java
+++ /dev/null
@@ -1,393 +0,0 @@
-/*
- * Copyright 2002-2014 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate4;
-
-import java.io.IOException;
-import java.lang.annotation.Annotation;
-import java.lang.reflect.Method;
-import java.util.Properties;
-import java.util.Set;
-import java.util.TreeSet;
-import javax.persistence.AttributeConverter;
-import javax.persistence.Embeddable;
-import javax.persistence.Entity;
-import javax.persistence.MappedSuperclass;
-import javax.sql.DataSource;
-import javax.transaction.TransactionManager;
-
-import org.hibernate.HibernateException;
-import org.hibernate.MappingException;
-import org.hibernate.SessionFactory;
-import org.hibernate.cache.spi.RegionFactory;
-import org.hibernate.cfg.AvailableSettings;
-import org.hibernate.cfg.Configuration;
-import org.hibernate.cfg.Environment;
-import org.hibernate.cfg.Settings;
-import org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory;
-import org.hibernate.service.ServiceRegistry;
-
-import org.springframework.core.io.Resource;
-import org.springframework.core.io.ResourceLoader;
-import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
-import org.springframework.core.io.support.ResourcePatternResolver;
-import org.springframework.core.io.support.ResourcePatternUtils;
-import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
-import org.springframework.core.type.classreading.MetadataReader;
-import org.springframework.core.type.classreading.MetadataReaderFactory;
-import org.springframework.core.type.filter.AnnotationTypeFilter;
-import org.springframework.core.type.filter.TypeFilter;
-import org.springframework.transaction.jta.JtaTransactionManager;
-import org.springframework.util.Assert;
-import org.springframework.util.ClassUtils;
-
-/**
- * A Spring-provided extension of the standard Hibernate {@link Configuration} class,
- * adding {@link SpringSessionContext} as a default and providing convenient ways
- * to specify a DataSource and an application class loader.
- *
- *
This is designed for programmatic use, e.g. in {@code @Bean} factory methods.
- * Consider using {@link LocalSessionFactoryBean} for XML bean definition files.
- *
- *
Requires Hibernate 4.0 or higher. As of Spring 4.0, it is compatible with
- * (the quite refactored) Hibernate 4.3 as well. We recommend using the latest
- * Hibernate 4.2.x or 4.3.x version, depending on whether you need to remain JPA 2.0
- * compatible at runtime (Hibernate 4.2) or can upgrade to JPA 2.1 (Hibernate 4.3).
- *
- *
NOTE: To set up Hibernate 4 for Spring-driven JTA transactions, make
- * sure to either use the {@link #setJtaTransactionManager} method or to set the
- * "hibernate.transaction.factory_class" property to {@link CMTTransactionFactory}.
- * Otherwise, Hibernate's smart flushing mechanism won't work properly.
- *
- * @author Juergen Hoeller
- * @since 3.1
- * @see LocalSessionFactoryBean
- */
-@SuppressWarnings("serial")
-public class LocalSessionFactoryBuilder extends Configuration {
-
- private static final String RESOURCE_PATTERN = "/**/*.class";
-
- private static final String PACKAGE_INFO_SUFFIX = ".package-info";
-
- private static final TypeFilter[] DEFAULT_ENTITY_TYPE_FILTERS = new TypeFilter[] {
- new AnnotationTypeFilter(Entity.class, false),
- new AnnotationTypeFilter(Embeddable.class, false),
- new AnnotationTypeFilter(MappedSuperclass.class, false)};
-
-
- private static TypeFilter converterTypeFilter;
-
- static {
- try {
- @SuppressWarnings("unchecked")
- Class extends Annotation> converterAnnotation = (Class extends Annotation>)
- ClassUtils.forName("javax.persistence.Converter", LocalSessionFactoryBuilder.class.getClassLoader());
- converterTypeFilter = new AnnotationTypeFilter(converterAnnotation, false);
- }
- catch (ClassNotFoundException ex) {
- // JPA 2.1 API not available - Hibernate <4.3
- }
- }
-
-
- private final ResourcePatternResolver resourcePatternResolver;
-
- private RegionFactory cacheRegionFactory;
-
- private TypeFilter[] entityTypeFilters = DEFAULT_ENTITY_TYPE_FILTERS;
-
-
- /**
- * Create a new LocalSessionFactoryBuilder for the given DataSource.
- * @param dataSource the JDBC DataSource that the resulting Hibernate SessionFactory should be using
- * (may be {@code null})
- */
- public LocalSessionFactoryBuilder(DataSource dataSource) {
- this(dataSource, new PathMatchingResourcePatternResolver());
- }
-
- /**
- * Create a new LocalSessionFactoryBuilder for the given DataSource.
- * @param dataSource the JDBC DataSource that the resulting Hibernate SessionFactory should be using
- * (may be {@code null})
- * @param classLoader the ClassLoader to load application classes from
- */
- public LocalSessionFactoryBuilder(DataSource dataSource, ClassLoader classLoader) {
- this(dataSource, new PathMatchingResourcePatternResolver(classLoader));
- }
-
- /**
- * Create a new LocalSessionFactoryBuilder for the given DataSource.
- * @param dataSource the JDBC DataSource that the resulting Hibernate SessionFactory should be using
- * (may be {@code null})
- * @param resourceLoader the ResourceLoader to load application classes from
- */
- @SuppressWarnings("deprecation") // to be able to build against Hibernate 4.3
- public LocalSessionFactoryBuilder(DataSource dataSource, ResourceLoader resourceLoader) {
- getProperties().put(Environment.CURRENT_SESSION_CONTEXT_CLASS, SpringSessionContext.class.getName());
- if (dataSource != null) {
- getProperties().put(Environment.DATASOURCE, dataSource);
- }
- // APP_CLASSLOADER is deprecated as of Hibernate 4.3 but we need to remain compatible with 4.0+
- getProperties().put(AvailableSettings.APP_CLASSLOADER, resourceLoader.getClassLoader());
- this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
- }
-
-
- /**
- * Set the Spring {@link JtaTransactionManager} or the JTA {@link TransactionManager}
- * to be used with Hibernate, if any. Allows for using a Spring-managed transaction
- * manager for Hibernate 4's session and cache synchronization, with the
- * "hibernate.transaction.jta.platform" automatically set to it. Also sets
- * "hibernate.transaction.factory_class" to {@link CMTTransactionFactory},
- * instructing Hibernate to interact with externally managed transactions.
- *
A passed-in Spring {@link JtaTransactionManager} needs to contain a JTA
- * {@link TransactionManager} reference to be usable here, except for the WebSphere
- * case where we'll automatically set {@code WebSphereExtendedJtaPlatform} accordingly.
- *
Note: If this is set, the Hibernate settings should not contain a JTA platform
- * setting to avoid meaningless double configuration.
- */
- public LocalSessionFactoryBuilder setJtaTransactionManager(Object jtaTransactionManager) {
- Assert.notNull(jtaTransactionManager, "Transaction manager reference must not be null");
- if (jtaTransactionManager instanceof JtaTransactionManager) {
- boolean webspherePresent = ClassUtils.isPresent("com.ibm.wsspi.uow.UOWManager", getClass().getClassLoader());
- if (webspherePresent) {
- getProperties().put(AvailableSettings.JTA_PLATFORM,
- ConfigurableJtaPlatform.getJtaPlatformBasePackage() + "internal.WebSphereExtendedJtaPlatform");
- }
- else {
- JtaTransactionManager jtaTm = (JtaTransactionManager) jtaTransactionManager;
- if (jtaTm.getTransactionManager() == null) {
- throw new IllegalArgumentException(
- "Can only apply JtaTransactionManager which has a TransactionManager reference set");
- }
- getProperties().put(AvailableSettings.JTA_PLATFORM,
- new ConfigurableJtaPlatform(jtaTm.getTransactionManager(), jtaTm.getUserTransaction(),
- jtaTm.getTransactionSynchronizationRegistry()).getJtaPlatformProxy());
- }
- }
- else if (jtaTransactionManager instanceof TransactionManager) {
- getProperties().put(AvailableSettings.JTA_PLATFORM,
- new ConfigurableJtaPlatform((TransactionManager) jtaTransactionManager, null, null).getJtaPlatformProxy());
- }
- else {
- throw new IllegalArgumentException(
- "Unknown transaction manager type: " + jtaTransactionManager.getClass().getName());
- }
- getProperties().put(AvailableSettings.TRANSACTION_STRATEGY, new CMTTransactionFactory());
- return this;
- }
-
- /**
- * Set a Hibernate 4.1/4.2/4.3 {@code MultiTenantConnectionProvider} to be passed
- * on to the SessionFactory: as an instance, a Class, or a String class name.
- *
Note that the package location of the {@code MultiTenantConnectionProvider}
- * interface changed between Hibernate 4.2 and 4.3. This method accepts both variants.
- * @since 4.0
- * @see AvailableSettings#MULTI_TENANT_CONNECTION_PROVIDER
- */
- public LocalSessionFactoryBuilder setMultiTenantConnectionProvider(Object multiTenantConnectionProvider) {
- getProperties().put(AvailableSettings.MULTI_TENANT_CONNECTION_PROVIDER, multiTenantConnectionProvider);
- return this;
- }
-
- /**
- * Set a Hibernate 4.1/4.2/4.3 {@code CurrentTenantIdentifierResolver} to be passed
- * on to the SessionFactory: as an instance, a Class, or a String class name.
- * @since 4.0
- * @see AvailableSettings#MULTI_TENANT_IDENTIFIER_RESOLVER
- */
- public LocalSessionFactoryBuilder setCurrentTenantIdentifierResolver(Object currentTenantIdentifierResolver) {
- getProperties().put(AvailableSettings.MULTI_TENANT_IDENTIFIER_RESOLVER, currentTenantIdentifierResolver);
- return this;
- }
-
- /**
- * Set the Hibernate RegionFactory to use for the SessionFactory.
- * Allows for using a Spring-managed RegionFactory instance.
- *
Note: If this is set, the Hibernate settings should not define a
- * cache provider to avoid meaningless double configuration.
- * @since 4.0
- * @see org.hibernate.cache.spi.RegionFactory
- */
- public LocalSessionFactoryBuilder setCacheRegionFactory(RegionFactory cacheRegionFactory) {
- this.cacheRegionFactory = cacheRegionFactory;
- return this;
- }
-
- /**
- * Specify custom type filters for Spring-based scanning for entity classes.
- *
Default is to search all specified packages for classes annotated with
- * {@code @javax.persistence.Entity}, {@code @javax.persistence.Embeddable}
- * or {@code @javax.persistence.MappedSuperclass}.
- * @since 4.1
- * @see #scanPackages
- */
- public LocalSessionFactoryBuilder setEntityTypeFilters(TypeFilter... entityTypeFilters) {
- this.entityTypeFilters = entityTypeFilters;
- return this;
- }
-
- /**
- * Add the given annotated classes in a batch.
- * @see #addAnnotatedClass
- * @see #scanPackages
- */
- public LocalSessionFactoryBuilder addAnnotatedClasses(Class>... annotatedClasses) {
- for (Class> annotatedClass : annotatedClasses) {
- addAnnotatedClass(annotatedClass);
- }
- return this;
- }
-
- /**
- * Add the given annotated packages in a batch.
- * @see #addPackage
- * @see #scanPackages
- */
- public LocalSessionFactoryBuilder addPackages(String... annotatedPackages) {
- for (String annotatedPackage : annotatedPackages) {
- addPackage(annotatedPackage);
- }
- return this;
- }
-
- /**
- * Perform Spring-based scanning for entity classes, registering them
- * as annotated classes with this {@code Configuration}.
- * @param packagesToScan one or more Java package names
- * @throws HibernateException if scanning fails for any reason
- */
- public LocalSessionFactoryBuilder scanPackages(String... packagesToScan) throws HibernateException {
- Set entityClassNames = new TreeSet();
- Set converterClassNames = new TreeSet();
- Set packageNames = new TreeSet();
- try {
- for (String pkg : packagesToScan) {
- String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
- ClassUtils.convertClassNameToResourcePath(pkg) + RESOURCE_PATTERN;
- Resource[] resources = this.resourcePatternResolver.getResources(pattern);
- MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(this.resourcePatternResolver);
- for (Resource resource : resources) {
- if (resource.isReadable()) {
- MetadataReader reader = readerFactory.getMetadataReader(resource);
- String className = reader.getClassMetadata().getClassName();
- if (matchesEntityTypeFilter(reader, readerFactory)) {
- entityClassNames.add(className);
- }
- else if (converterTypeFilter != null && converterTypeFilter.match(reader, readerFactory)) {
- converterClassNames.add(className);
- }
- else if (className.endsWith(PACKAGE_INFO_SUFFIX)) {
- packageNames.add(className.substring(0, className.length() - PACKAGE_INFO_SUFFIX.length()));
- }
- }
- }
- }
- }
- catch (IOException ex) {
- throw new MappingException("Failed to scan classpath for unlisted classes", ex);
- }
- try {
- ClassLoader cl = this.resourcePatternResolver.getClassLoader();
- for (String className : entityClassNames) {
- addAnnotatedClass(cl.loadClass(className));
- }
- for (String className : converterClassNames) {
- ConverterRegistrationDelegate.registerConverter(this, cl.loadClass(className));
- }
- for (String packageName : packageNames) {
- addPackage(packageName);
- }
- }
- catch (ClassNotFoundException ex) {
- throw new MappingException("Failed to load annotated classes from classpath", ex);
- }
- return this;
- }
-
- /**
- * Check whether any of the configured entity type filters matches
- * the current class descriptor contained in the metadata reader.
- */
- private boolean matchesEntityTypeFilter(MetadataReader reader, MetadataReaderFactory readerFactory) throws IOException {
- if (this.entityTypeFilters != null) {
- for (TypeFilter filter : this.entityTypeFilters) {
- if (filter.match(reader, readerFactory)) {
- return true;
- }
- }
- }
- return false;
- }
-
-
- // Overridden methods from Hibernate's Configuration class
-
- @Override
- public Settings buildSettings(Properties props, ServiceRegistry serviceRegistry) throws HibernateException {
- Settings settings = super.buildSettings(props, serviceRegistry);
- if (this.cacheRegionFactory != null) {
- try {
- Method setRegionFactory = Settings.class.getDeclaredMethod("setRegionFactory", RegionFactory.class);
- setRegionFactory.setAccessible(true);
- setRegionFactory.invoke(settings, this.cacheRegionFactory);
- }
- catch (Exception ex) {
- throw new IllegalStateException("Failed to invoke Hibernate's setRegionFactory method", ex);
- }
- }
- return settings;
- }
-
- /**
- * Build the {@code SessionFactory}.
- */
- @Override
- @SuppressWarnings("deprecation")
- public SessionFactory buildSessionFactory() throws HibernateException {
- ClassLoader appClassLoader = (ClassLoader) getProperties().get(AvailableSettings.APP_CLASSLOADER);
- Thread currentThread = Thread.currentThread();
- ClassLoader threadContextClassLoader = currentThread.getContextClassLoader();
- boolean overrideClassLoader =
- (appClassLoader != null && !appClassLoader.equals(threadContextClassLoader));
- if (overrideClassLoader) {
- currentThread.setContextClassLoader(appClassLoader);
- }
- try {
- return super.buildSessionFactory();
- }
- finally {
- if (overrideClassLoader) {
- currentThread.setContextClassLoader(threadContextClassLoader);
- }
- }
- }
-
-
- /**
- * Inner class to avoid hard dependency on JPA 2.1 / Hibernate 4.3.
- */
- private static class ConverterRegistrationDelegate {
-
- @SuppressWarnings("unchecked")
- public static void registerConverter(Configuration config, Class> converterClass) {
- config.addAttributeConverter((Class extends AttributeConverter, ?>>) converterClass);
- }
- }
-
-}
diff --git a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/SessionFactoryUtils.java b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/SessionFactoryUtils.java
deleted file mode 100644
index 571e900023..0000000000
--- a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/SessionFactoryUtils.java
+++ /dev/null
@@ -1,221 +0,0 @@
-/*
- * Copyright 2002-2014 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate4;
-
-import java.lang.reflect.Method;
-import javax.sql.DataSource;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.hibernate.HibernateException;
-import org.hibernate.JDBCException;
-import org.hibernate.NonUniqueObjectException;
-import org.hibernate.NonUniqueResultException;
-import org.hibernate.ObjectDeletedException;
-import org.hibernate.PersistentObjectException;
-import org.hibernate.PessimisticLockException;
-import org.hibernate.PropertyValueException;
-import org.hibernate.QueryException;
-import org.hibernate.QueryTimeoutException;
-import org.hibernate.Session;
-import org.hibernate.SessionFactory;
-import org.hibernate.StaleObjectStateException;
-import org.hibernate.StaleStateException;
-import org.hibernate.TransientObjectException;
-import org.hibernate.UnresolvableObjectException;
-import org.hibernate.WrongClassException;
-import org.hibernate.dialect.lock.OptimisticEntityLockException;
-import org.hibernate.dialect.lock.PessimisticEntityLockException;
-import org.hibernate.engine.spi.SessionFactoryImplementor;
-import org.hibernate.exception.ConstraintViolationException;
-import org.hibernate.exception.DataException;
-import org.hibernate.exception.JDBCConnectionException;
-import org.hibernate.exception.LockAcquisitionException;
-import org.hibernate.exception.SQLGrammarException;
-import org.hibernate.service.spi.Wrapped;
-
-import org.springframework.dao.CannotAcquireLockException;
-import org.springframework.dao.DataAccessException;
-import org.springframework.dao.DataAccessResourceFailureException;
-import org.springframework.dao.DataIntegrityViolationException;
-import org.springframework.dao.DuplicateKeyException;
-import org.springframework.dao.IncorrectResultSizeDataAccessException;
-import org.springframework.dao.InvalidDataAccessApiUsageException;
-import org.springframework.dao.InvalidDataAccessResourceUsageException;
-import org.springframework.dao.PessimisticLockingFailureException;
-import org.springframework.jdbc.datasource.DataSourceUtils;
-import org.springframework.util.ClassUtils;
-import org.springframework.util.ReflectionUtils;
-
-/**
- * Helper class featuring methods for Hibernate Session handling.
- * Also provides support for exception translation.
- *
- * Used internally by {@link HibernateTransactionManager}.
- * Can also be used directly in application code.
- *
- * @author Juergen Hoeller
- * @since 3.1
- * @see HibernateExceptionTranslator
- * @see HibernateTransactionManager
- */
-public abstract class SessionFactoryUtils {
-
- /**
- * Order value for TransactionSynchronization objects that clean up Hibernate Sessions.
- * Returns {@code DataSourceUtils.CONNECTION_SYNCHRONIZATION_ORDER - 100}
- * to execute Session cleanup before JDBC Connection cleanup, if any.
- * @see org.springframework.jdbc.datasource.DataSourceUtils#CONNECTION_SYNCHRONIZATION_ORDER
- */
- public static final int SESSION_SYNCHRONIZATION_ORDER =
- DataSourceUtils.CONNECTION_SYNCHRONIZATION_ORDER - 100;
-
- static final Log logger = LogFactory.getLog(SessionFactoryUtils.class);
-
- /**
- * Bridging between the different ConnectionProvider package location in 4.0-4.2 vs 4.3.
- */
- private static final Method getConnectionProviderMethod =
- ClassUtils.getMethodIfAvailable(SessionFactoryImplementor.class, "getConnectionProvider");
-
-
- /**
- * Determine the DataSource of the given SessionFactory.
- * @param sessionFactory the SessionFactory to check
- * @return the DataSource, or {@code null} if none found
- * @see org.hibernate.engine.spi.SessionFactoryImplementor#getConnectionProvider
- */
- public static DataSource getDataSource(SessionFactory sessionFactory) {
- if (getConnectionProviderMethod != null && sessionFactory instanceof SessionFactoryImplementor) {
- Wrapped cp = (Wrapped) ReflectionUtils.invokeMethod(getConnectionProviderMethod, sessionFactory);
- if (cp != null) {
- return cp.unwrap(DataSource.class);
- }
- }
- return null;
- }
-
- /**
- * Perform actual closing of the Hibernate Session,
- * catching and logging any cleanup exceptions thrown.
- * @param session the Hibernate Session to close (may be {@code null})
- * @see org.hibernate.Session#close()
- */
- public static void closeSession(Session session) {
- if (session != null) {
- try {
- session.close();
- }
- catch (HibernateException ex) {
- logger.debug("Could not close Hibernate Session", ex);
- }
- catch (Throwable ex) {
- logger.debug("Unexpected exception on closing Hibernate Session", ex);
- }
- }
- }
-
- /**
- * Convert the given HibernateException to an appropriate exception
- * from the {@code org.springframework.dao} hierarchy.
- * @param ex HibernateException that occurred
- * @return the corresponding DataAccessException instance
- * @see HibernateExceptionTranslator#convertHibernateAccessException
- * @see HibernateTransactionManager#convertHibernateAccessException
- */
- public static DataAccessException convertHibernateAccessException(HibernateException ex) {
- if (ex instanceof JDBCConnectionException) {
- return new DataAccessResourceFailureException(ex.getMessage(), ex);
- }
- if (ex instanceof SQLGrammarException) {
- SQLGrammarException jdbcEx = (SQLGrammarException) ex;
- return new InvalidDataAccessResourceUsageException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex);
- }
- if (ex instanceof QueryTimeoutException) {
- QueryTimeoutException jdbcEx = (QueryTimeoutException) ex;
- return new org.springframework.dao.QueryTimeoutException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex);
- }
- if (ex instanceof LockAcquisitionException) {
- LockAcquisitionException jdbcEx = (LockAcquisitionException) ex;
- return new CannotAcquireLockException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex);
- }
- if (ex instanceof PessimisticLockException) {
- PessimisticLockException jdbcEx = (PessimisticLockException) ex;
- return new PessimisticLockingFailureException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex);
- }
- if (ex instanceof ConstraintViolationException) {
- ConstraintViolationException jdbcEx = (ConstraintViolationException) ex;
- return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() +
- "]; constraint [" + jdbcEx.getConstraintName() + "]", ex);
- }
- if (ex instanceof DataException) {
- DataException jdbcEx = (DataException) ex;
- return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex);
- }
- if (ex instanceof JDBCException) {
- return new HibernateJdbcException((JDBCException) ex);
- }
- // end of JDBCException (subclass) handling
-
- if (ex instanceof QueryException) {
- return new HibernateQueryException((QueryException) ex);
- }
- if (ex instanceof NonUniqueResultException) {
- return new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex);
- }
- if (ex instanceof NonUniqueObjectException) {
- return new DuplicateKeyException(ex.getMessage(), ex);
- }
- if (ex instanceof PropertyValueException) {
- return new DataIntegrityViolationException(ex.getMessage(), ex);
- }
- if (ex instanceof PersistentObjectException) {
- return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
- }
- if (ex instanceof TransientObjectException) {
- return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
- }
- if (ex instanceof ObjectDeletedException) {
- return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
- }
- if (ex instanceof UnresolvableObjectException) {
- return new HibernateObjectRetrievalFailureException((UnresolvableObjectException) ex);
- }
- if (ex instanceof WrongClassException) {
- return new HibernateObjectRetrievalFailureException((WrongClassException) ex);
- }
- if (ex instanceof StaleObjectStateException) {
- return new HibernateOptimisticLockingFailureException((StaleObjectStateException) ex);
- }
- if (ex instanceof StaleStateException) {
- return new HibernateOptimisticLockingFailureException((StaleStateException) ex);
- }
- if (ex instanceof OptimisticEntityLockException) {
- return new HibernateOptimisticLockingFailureException((OptimisticEntityLockException) ex);
- }
- if (ex instanceof PessimisticEntityLockException) {
- if (ex.getCause() instanceof LockAcquisitionException) {
- return new CannotAcquireLockException(ex.getMessage(), ex.getCause());
- }
- return new PessimisticLockingFailureException(ex.getMessage(), ex);
- }
-
- // fallback
- return new HibernateSystemException(ex);
- }
-
-}
diff --git a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/SessionHolder.java b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/SessionHolder.java
deleted file mode 100644
index 67bb9c6339..0000000000
--- a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/SessionHolder.java
+++ /dev/null
@@ -1,80 +0,0 @@
-/*
- * 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate4;
-
-import org.hibernate.FlushMode;
-import org.hibernate.Session;
-import org.hibernate.Transaction;
-
-import org.springframework.transaction.support.ResourceHolderSupport;
-import org.springframework.util.Assert;
-
-/**
- * Session holder, wrapping a Hibernate Session and a Hibernate Transaction.
- * HibernateTransactionManager binds instances of this class to the thread,
- * for a given SessionFactory.
- *
- *
Note: This is an SPI class, not intended to be used by applications.
- *
- * @author Juergen Hoeller
- * @since 3.1
- * @see HibernateTransactionManager
- * @see SessionFactoryUtils
- */
-public class SessionHolder extends ResourceHolderSupport {
-
- private Session session;
-
- private Transaction transaction;
-
- private FlushMode previousFlushMode;
-
-
- public SessionHolder(Session session) {
- Assert.notNull(session, "Session must not be null");
- this.session = session;
- }
-
- public Session getSession() {
- return this.session;
- }
-
- public void setTransaction(Transaction transaction) {
- this.transaction = transaction;
- }
-
- public Transaction getTransaction() {
- return this.transaction;
- }
-
- public void setPreviousFlushMode(FlushMode previousFlushMode) {
- this.previousFlushMode = previousFlushMode;
- }
-
- public FlushMode getPreviousFlushMode() {
- return this.previousFlushMode;
- }
-
-
- @Override
- public void clear() {
- super.clear();
- this.transaction = null;
- this.previousFlushMode = null;
- }
-
-}
diff --git a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/SpringFlushSynchronization.java b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/SpringFlushSynchronization.java
deleted file mode 100644
index 07d8d6981e..0000000000
--- a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/SpringFlushSynchronization.java
+++ /dev/null
@@ -1,64 +0,0 @@
-/*
- * Copyright 2002-2014 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate4;
-
-import org.hibernate.HibernateException;
-import org.hibernate.Session;
-
-import org.springframework.transaction.support.TransactionSynchronizationAdapter;
-
-/**
- * Simple synchronization adapter that propagates a {@code flush()} call
- * to the underlying Hibernate Session. Used in combination with JTA.
- *
- * @author Juergen Hoeller
- * @since 3.1
- */
-public class SpringFlushSynchronization extends TransactionSynchronizationAdapter {
-
- private final Session session;
-
-
- public SpringFlushSynchronization(Session session) {
- this.session = session;
- }
-
-
- @Override
- public void flush() {
- try {
- SessionFactoryUtils.logger.debug("Flushing Hibernate Session on explicit request");
- this.session.flush();
- }
- catch (HibernateException ex) {
- throw SessionFactoryUtils.convertHibernateAccessException(ex);
- }
- }
-
-
- @Override
- public boolean equals(Object obj) {
- return (obj instanceof SpringFlushSynchronization &&
- this.session == ((SpringFlushSynchronization) obj).session);
- }
-
- @Override
- public int hashCode() {
- return this.session.hashCode();
- }
-
-}
diff --git a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/SpringJtaSessionContext.java b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/SpringJtaSessionContext.java
deleted file mode 100644
index 2b2ad05893..0000000000
--- a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/SpringJtaSessionContext.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate4;
-
-import org.hibernate.FlushMode;
-import org.hibernate.Session;
-import org.hibernate.context.internal.JTASessionContext;
-import org.hibernate.engine.spi.SessionFactoryImplementor;
-
-import org.springframework.transaction.support.TransactionSynchronizationManager;
-
-/**
- * Spring-specific subclass of Hibernate's JTASessionContext,
- * setting {@code FlushMode.MANUAL} for read-only transactions.
- *
- * @author Juergen Hoeller
- * @since 3.1
- */
-@SuppressWarnings("serial")
-public class SpringJtaSessionContext extends JTASessionContext {
-
- public SpringJtaSessionContext(SessionFactoryImplementor factory) {
- super(factory);
- }
-
- @Override
- protected Session buildOrObtainSession() {
- Session session = super.buildOrObtainSession();
- if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
- session.setFlushMode(FlushMode.MANUAL);
- }
- return session;
- }
-
-}
diff --git a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/SpringSessionContext.java b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/SpringSessionContext.java
deleted file mode 100644
index 99d6f69261..0000000000
--- a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/SpringSessionContext.java
+++ /dev/null
@@ -1,138 +0,0 @@
-/*
- * Copyright 2002-2014 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate4;
-
-import java.lang.reflect.Method;
-import javax.transaction.Status;
-import javax.transaction.SystemException;
-import javax.transaction.TransactionManager;
-
-import org.apache.commons.logging.LogFactory;
-import org.hibernate.FlushMode;
-import org.hibernate.HibernateException;
-import org.hibernate.Session;
-import org.hibernate.context.spi.CurrentSessionContext;
-import org.hibernate.engine.spi.SessionFactoryImplementor;
-
-import org.springframework.transaction.support.TransactionSynchronizationManager;
-import org.springframework.util.ReflectionUtils;
-
-/**
- * Implementation of Hibernate 3.1's CurrentSessionContext interface
- * that delegates to Spring's SessionFactoryUtils for providing a
- * Spring-managed current Session.
- *
- *
This CurrentSessionContext implementation can also be specified in custom
- * SessionFactory setup through the "hibernate.current_session_context_class"
- * property, with the fully qualified name of this class as value.
- *
- * @author Juergen Hoeller
- * @since 3.1
- */
-@SuppressWarnings("serial")
-public class SpringSessionContext implements CurrentSessionContext {
-
- private final SessionFactoryImplementor sessionFactory;
-
- private TransactionManager transactionManager;
-
- private CurrentSessionContext jtaSessionContext;
-
-
- /**
- * Create a new SpringSessionContext for the given Hibernate SessionFactory.
- * @param sessionFactory the SessionFactory to provide current Sessions for
- */
- public SpringSessionContext(SessionFactoryImplementor sessionFactory) {
- this.sessionFactory = sessionFactory;
- try {
- Object jtaPlatform = sessionFactory.getServiceRegistry().getService(ConfigurableJtaPlatform.jtaPlatformClass);
- Method rtmMethod = ConfigurableJtaPlatform.jtaPlatformClass.getMethod("retrieveTransactionManager");
- this.transactionManager = (TransactionManager) ReflectionUtils.invokeMethod(rtmMethod, jtaPlatform);
- if (this.transactionManager != null) {
- this.jtaSessionContext = new SpringJtaSessionContext(sessionFactory);
- }
- }
- catch (Exception ex) {
- LogFactory.getLog(SpringSessionContext.class).warn(
- "Could not introspect Hibernate JtaPlatform for SpringJtaSessionContext", ex);
- }
- }
-
-
- /**
- * Retrieve the Spring-managed Session for the current thread, if any.
- */
- @Override
- public Session currentSession() throws HibernateException {
- Object value = TransactionSynchronizationManager.getResource(this.sessionFactory);
- if (value instanceof Session) {
- return (Session) value;
- }
- else if (value instanceof SessionHolder) {
- SessionHolder sessionHolder = (SessionHolder) value;
- Session session = sessionHolder.getSession();
- if (!sessionHolder.isSynchronizedWithTransaction() &&
- TransactionSynchronizationManager.isSynchronizationActive()) {
- TransactionSynchronizationManager.registerSynchronization(
- new SpringSessionSynchronization(sessionHolder, this.sessionFactory, false));
- sessionHolder.setSynchronizedWithTransaction(true);
- // Switch to FlushMode.AUTO, as we have to assume a thread-bound Session
- // with FlushMode.MANUAL, which needs to allow flushing within the transaction.
- FlushMode flushMode = session.getFlushMode();
- if (flushMode.equals(FlushMode.MANUAL) &&
- !TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
- session.setFlushMode(FlushMode.AUTO);
- sessionHolder.setPreviousFlushMode(flushMode);
- }
- }
- return session;
- }
-
- if (this.transactionManager != null) {
- try {
- if (this.transactionManager.getStatus() == Status.STATUS_ACTIVE) {
- Session session = this.jtaSessionContext.currentSession();
- if (TransactionSynchronizationManager.isSynchronizationActive()) {
- TransactionSynchronizationManager.registerSynchronization(new SpringFlushSynchronization(session));
- }
- return session;
- }
- }
- catch (SystemException ex) {
- throw new HibernateException("JTA TransactionManager found but status check failed", ex);
- }
- }
-
- if (TransactionSynchronizationManager.isSynchronizationActive()) {
- Session session = this.sessionFactory.openSession();
- if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
- session.setFlushMode(FlushMode.MANUAL);
- }
- SessionHolder sessionHolder = new SessionHolder(session);
- TransactionSynchronizationManager.registerSynchronization(
- new SpringSessionSynchronization(sessionHolder, this.sessionFactory, true));
- TransactionSynchronizationManager.bindResource(this.sessionFactory, sessionHolder);
- sessionHolder.setSynchronizedWithTransaction(true);
- return session;
- }
- else {
- throw new HibernateException("Could not obtain transaction-synchronized Session for current thread");
- }
- }
-
-}
diff --git a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/SpringSessionSynchronization.java b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/SpringSessionSynchronization.java
deleted file mode 100644
index 6eee684dfb..0000000000
--- a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/SpringSessionSynchronization.java
+++ /dev/null
@@ -1,155 +0,0 @@
-/*
- * Copyright 2002-2015 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate4;
-
-import org.hibernate.FlushMode;
-import org.hibernate.HibernateException;
-import org.hibernate.Session;
-import org.hibernate.SessionFactory;
-
-import org.springframework.core.Ordered;
-import org.springframework.dao.DataAccessException;
-import org.springframework.transaction.support.TransactionSynchronization;
-import org.springframework.transaction.support.TransactionSynchronizationManager;
-
-/**
- * Callback for resource cleanup at the end of a Spring-managed transaction
- * for a pre-bound Hibernate Session.
- *
- * @author Juergen Hoeller
- * @since 3.1
- */
-public class SpringSessionSynchronization implements TransactionSynchronization, Ordered {
-
- private final SessionHolder sessionHolder;
-
- private final SessionFactory sessionFactory;
-
- private final boolean newSession;
-
- private boolean holderActive = true;
-
-
- public SpringSessionSynchronization(SessionHolder sessionHolder, SessionFactory sessionFactory) {
- this(sessionHolder, sessionFactory, false);
- }
-
- public SpringSessionSynchronization(SessionHolder sessionHolder, SessionFactory sessionFactory, boolean newSession) {
- this.sessionHolder = sessionHolder;
- this.sessionFactory = sessionFactory;
- this.newSession = newSession;
- }
-
-
- private Session getCurrentSession() {
- return this.sessionHolder.getSession();
- }
-
-
- @Override
- public int getOrder() {
- return SessionFactoryUtils.SESSION_SYNCHRONIZATION_ORDER;
- }
-
- @Override
- public void suspend() {
- if (this.holderActive) {
- TransactionSynchronizationManager.unbindResource(this.sessionFactory);
- // Eagerly disconnect the Session here, to make release mode "on_close" work on JBoss.
- getCurrentSession().disconnect();
- }
- }
-
- @Override
- public void resume() {
- if (this.holderActive) {
- TransactionSynchronizationManager.bindResource(this.sessionFactory, this.sessionHolder);
- }
- }
-
- @Override
- public void flush() {
- try {
- SessionFactoryUtils.logger.debug("Flushing Hibernate Session on explicit request");
- getCurrentSession().flush();
- }
- catch (HibernateException ex) {
- throw SessionFactoryUtils.convertHibernateAccessException(ex);
- }
- }
-
- @Override
- public void beforeCommit(boolean readOnly) throws DataAccessException {
- if (!readOnly) {
- Session session = getCurrentSession();
- // Read-write transaction -> flush the Hibernate Session.
- // Further check: only flush when not FlushMode.MANUAL.
- if (!session.getFlushMode().equals(FlushMode.MANUAL)) {
- try {
- SessionFactoryUtils.logger.debug("Flushing Hibernate Session on transaction synchronization");
- session.flush();
- }
- catch (HibernateException ex) {
- throw SessionFactoryUtils.convertHibernateAccessException(ex);
- }
- }
- }
- }
-
- @Override
- public void beforeCompletion() {
- try {
- Session session = this.sessionHolder.getSession();
- if (this.sessionHolder.getPreviousFlushMode() != null) {
- // In case of pre-bound Session, restore previous flush mode.
- session.setFlushMode(this.sessionHolder.getPreviousFlushMode());
- }
- // Eagerly disconnect the Session here, to make release mode "on_close" work nicely.
- session.disconnect();
- }
- finally {
- // Unbind at this point if it's a new Session...
- if (this.newSession) {
- TransactionSynchronizationManager.unbindResource(this.sessionFactory);
- this.holderActive = false;
- }
- }
- }
-
- @Override
- public void afterCommit() {
- }
-
- @Override
- public void afterCompletion(int status) {
- try {
- if (status != STATUS_COMMITTED) {
- // Clear all pending inserts/updates/deletes in the Session.
- // Necessary for pre-bound Sessions, to avoid inconsistent state.
- this.sessionHolder.getSession().clear();
- }
- }
- finally {
- this.sessionHolder.setSynchronizedWithTransaction(false);
- // Call close() at this point if it's a new Session...
- if (this.newSession) {
- SessionFactoryUtils.closeSession(this.sessionHolder.getSession());
- }
- }
- }
-
-}
diff --git a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/package-info.java b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/package-info.java
deleted file mode 100644
index 7b2fc61903..0000000000
--- a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/package-info.java
+++ /dev/null
@@ -1,16 +0,0 @@
-/**
- * Package providing integration of
- * Hibernate 4.x
- * with Spring concepts.
- *
- *
Contains an implementation of Spring's transaction SPI for local Hibernate transactions.
- * This package is intentionally rather minimal, with no template classes or the like,
- * in order to follow Hibernate recommendations as closely as possible. We recommend
- * using Hibernate's native sessionFactory.getCurrentSession() style.
- *
- *
This package supports Hibernate 4.x only.
- * See the {@code org.springframework.orm.hibernate3} package for Hibernate 3.x support.
- * Note: Do not use HibernateTemplate or other classes from the hibernate3 package
- * with Hibernate 4; this will lead to class definition exceptions at runtime.
- */
-package org.springframework.orm.hibernate4;
diff --git a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/support/AsyncRequestInterceptor.java b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/support/AsyncRequestInterceptor.java
deleted file mode 100644
index 2878605d42..0000000000
--- a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/support/AsyncRequestInterceptor.java
+++ /dev/null
@@ -1,120 +0,0 @@
-/*
- * Copyright 2002-2015 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate4.support;
-
-import java.util.concurrent.Callable;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.hibernate.SessionFactory;
-
-import org.springframework.orm.hibernate4.SessionFactoryUtils;
-import org.springframework.orm.hibernate4.SessionHolder;
-import org.springframework.transaction.support.TransactionSynchronizationManager;
-import org.springframework.web.context.request.NativeWebRequest;
-import org.springframework.web.context.request.async.CallableProcessingInterceptorAdapter;
-import org.springframework.web.context.request.async.DeferredResult;
-import org.springframework.web.context.request.async.DeferredResultProcessingInterceptor;
-
-/**
- * An interceptor with asynchronous web requests used in OpenSessionInViewFilter and
- * OpenSessionInViewInterceptor.
- *
- * Ensures the following:
- * 1) The session is bound/unbound when "callable processing" is started
- * 2) The session is closed if an async request times out
- *
- * @author Rossen Stoyanchev
- * @since 3.2.5
- */
-class AsyncRequestInterceptor extends CallableProcessingInterceptorAdapter implements DeferredResultProcessingInterceptor {
-
- private static final Log logger = LogFactory.getLog(AsyncRequestInterceptor.class);
-
- private final SessionFactory sessionFactory;
-
- private final SessionHolder sessionHolder;
-
- private volatile boolean timeoutInProgress;
-
-
- public AsyncRequestInterceptor(SessionFactory sessionFactory, SessionHolder sessionHolder) {
- this.sessionFactory = sessionFactory;
- this.sessionHolder = sessionHolder;
- }
-
-
- @Override
- public void preProcess(NativeWebRequest request, Callable task) {
- bindSession();
- }
-
- public void bindSession() {
- this.timeoutInProgress = false;
- TransactionSynchronizationManager.bindResource(this.sessionFactory, this.sessionHolder);
- }
-
- @Override
- public void postProcess(NativeWebRequest request, Callable task, Object concurrentResult) {
- TransactionSynchronizationManager.unbindResource(this.sessionFactory);
- }
-
- @Override
- public Object handleTimeout(NativeWebRequest request, Callable task) {
- this.timeoutInProgress = true;
- return RESULT_NONE; // give other interceptors a chance to handle the timeout
- }
-
- @Override
- public void afterCompletion(NativeWebRequest request, Callable task) throws Exception {
- closeAfterTimeout();
- }
-
- private void closeAfterTimeout() {
- if (this.timeoutInProgress) {
- logger.debug("Closing Hibernate Session after async request timeout");
- SessionFactoryUtils.closeSession(this.sessionHolder.getSession());
- }
- }
-
-
- // Implementation of DeferredResultProcessingInterceptor methods
-
- @Override
- public void beforeConcurrentHandling(NativeWebRequest request, DeferredResult deferredResult) {
- }
-
- @Override
- public void preProcess(NativeWebRequest request, DeferredResult deferredResult) {
- }
-
- @Override
- public void postProcess(NativeWebRequest request, DeferredResult deferredResult, Object result) {
- }
-
- @Override
- public boolean handleTimeout(NativeWebRequest request, DeferredResult deferredResult) {
- this.timeoutInProgress = true;
- return true; // give other interceptors a chance to handle the timeout
- }
-
- @Override
- public void afterCompletion(NativeWebRequest request, DeferredResult deferredResult) {
- closeAfterTimeout();
- }
-
-}
diff --git a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/support/HibernateDaoSupport.java b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/support/HibernateDaoSupport.java
deleted file mode 100644
index 4d1083feba..0000000000
--- a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/support/HibernateDaoSupport.java
+++ /dev/null
@@ -1,132 +0,0 @@
-/*
- * Copyright 2002-2014 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate4.support;
-
-import org.hibernate.Session;
-import org.hibernate.SessionFactory;
-
-import org.springframework.dao.DataAccessResourceFailureException;
-import org.springframework.dao.support.DaoSupport;
-import org.springframework.orm.hibernate4.HibernateTemplate;
-
-/**
- * Convenient super class for Hibernate-based data access objects.
- *
- * Requires a {@link org.hibernate.SessionFactory} to be set, providing a
- * {@link org.springframework.orm.hibernate4.HibernateTemplate} based on it to
- * subclasses through the {@link #getHibernateTemplate()} method.
- * Can alternatively be initialized directly with a HibernateTemplate,
- * in order to reuse the latter's settings such as the SessionFactory,
- * exception translator, flush mode, etc.
- *
- *
This class will create its own HibernateTemplate instance if a SessionFactory
- * is passed in. The "allowCreate" flag on that HibernateTemplate will be "true"
- * by default. A custom HibernateTemplate instance can be used through overriding
- * {@link #createHibernateTemplate}.
- *
- *
NOTE: Hibernate access code can also be coded in plain Hibernate style.
- * Hence, for newly started projects, consider adopting the standard Hibernate
- * style of coding data access objects instead, based on
- * {@link org.hibernate.SessionFactory#getCurrentSession()}.
- * This HibernateTemplate primarily exists as a migration helper for Hibernate 3
- * based data access code, to benefit from bug fixes in Hibernate 4.x.
- *
- * @author Juergen Hoeller
- * @since 4.0.1
- * @see #setSessionFactory
- * @see #getHibernateTemplate
- * @see org.springframework.orm.hibernate4.HibernateTemplate
- */
-public abstract class HibernateDaoSupport extends DaoSupport {
-
- private HibernateTemplate hibernateTemplate;
-
-
- /**
- * Set the Hibernate SessionFactory to be used by this DAO.
- * Will automatically create a HibernateTemplate for the given SessionFactory.
- * @see #createHibernateTemplate
- * @see #setHibernateTemplate
- */
- public final void setSessionFactory(SessionFactory sessionFactory) {
- if (this.hibernateTemplate == null || sessionFactory != this.hibernateTemplate.getSessionFactory()) {
- this.hibernateTemplate = createHibernateTemplate(sessionFactory);
- }
- }
-
- /**
- * Create a HibernateTemplate for the given SessionFactory.
- * Only invoked if populating the DAO with a SessionFactory reference!
- *
Can be overridden in subclasses to provide a HibernateTemplate instance
- * with different configuration, or a custom HibernateTemplate subclass.
- * @param sessionFactory the Hibernate SessionFactory to create a HibernateTemplate for
- * @return the new HibernateTemplate instance
- * @see #setSessionFactory
- */
- protected HibernateTemplate createHibernateTemplate(SessionFactory sessionFactory) {
- return new HibernateTemplate(sessionFactory);
- }
-
- /**
- * Return the Hibernate SessionFactory used by this DAO.
- */
- public final SessionFactory getSessionFactory() {
- return (this.hibernateTemplate != null ? this.hibernateTemplate.getSessionFactory() : null);
- }
-
- /**
- * Set the HibernateTemplate for this DAO explicitly,
- * as an alternative to specifying a SessionFactory.
- * @see #setSessionFactory
- */
- public final void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
- this.hibernateTemplate = hibernateTemplate;
- }
-
- /**
- * Return the HibernateTemplate for this DAO,
- * pre-initialized with the SessionFactory or set explicitly.
- *
Note: The returned HibernateTemplate is a shared instance.
- * You may introspect its configuration, but not modify the configuration
- * (other than from within an {@link #initDao} implementation).
- * Consider creating a custom HibernateTemplate instance via
- * {@code new HibernateTemplate(getSessionFactory())}, in which case
- * you're allowed to customize the settings on the resulting instance.
- */
- public final HibernateTemplate getHibernateTemplate() {
- return this.hibernateTemplate;
- }
-
- @Override
- protected final void checkDaoConfig() {
- if (this.hibernateTemplate == null) {
- throw new IllegalArgumentException("'sessionFactory' or 'hibernateTemplate' is required");
- }
- }
-
-
- /**
- * Conveniently obtain the current Hibernate Session.
- * @return the Hibernate Session
- * @throws DataAccessResourceFailureException if the Session couldn't be created
- * @see org.hibernate.SessionFactory#getCurrentSession()
- */
- protected final Session currentSession() throws DataAccessResourceFailureException {
- return getSessionFactory().getCurrentSession();
- }
-
-}
diff --git a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/support/OpenSessionInViewFilter.java b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/support/OpenSessionInViewFilter.java
deleted file mode 100644
index 88f0573aeb..0000000000
--- a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/support/OpenSessionInViewFilter.java
+++ /dev/null
@@ -1,221 +0,0 @@
-/*
- * Copyright 2002-2015 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate4.support;
-
-import java.io.IOException;
-import javax.servlet.FilterChain;
-import javax.servlet.ServletException;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
-import org.hibernate.FlushMode;
-import org.hibernate.HibernateException;
-import org.hibernate.Session;
-import org.hibernate.SessionFactory;
-
-import org.springframework.dao.DataAccessResourceFailureException;
-import org.springframework.orm.hibernate4.SessionFactoryUtils;
-import org.springframework.orm.hibernate4.SessionHolder;
-import org.springframework.transaction.support.TransactionSynchronizationManager;
-import org.springframework.web.context.WebApplicationContext;
-import org.springframework.web.context.request.async.WebAsyncManager;
-import org.springframework.web.context.request.async.WebAsyncUtils;
-import org.springframework.web.context.support.WebApplicationContextUtils;
-import org.springframework.web.filter.OncePerRequestFilter;
-
-/**
- * Servlet Filter that binds a Hibernate Session to the thread for the entire
- * processing of the request. Intended for the "Open Session in View" pattern,
- * i.e. to allow for lazy loading in web views despite the original transactions
- * already being completed.
- *
- *
This filter makes Hibernate Sessions available via the current thread, which
- * will be autodetected by transaction managers. It is suitable for service layer
- * transactions via {@link org.springframework.orm.hibernate4.HibernateTransactionManager}
- * as well as for non-transactional execution (if configured appropriately).
- *
- *
NOTE : This filter will by default not flush the Hibernate Session,
- * with the flush mode set to {@code FlushMode.NEVER}. It assumes to be used
- * in combination with service layer transactions that care for the flushing: The
- * active transaction manager will temporarily change the flush mode to
- * {@code FlushMode.AUTO} during a read-write transaction, with the flush
- * mode reset to {@code FlushMode.NEVER} at the end of each transaction.
- *
- *
WARNING: Applying this filter to existing logic can cause issues that
- * have not appeared before, through the use of a single Hibernate Session for the
- * processing of an entire request. In particular, the reassociation of persistent
- * objects with a Hibernate Session has to occur at the very beginning of request
- * processing, to avoid clashes with already loaded instances of the same objects.
- *
- *
Looks up the SessionFactory in Spring's root web application context.
- * Supports a "sessionFactoryBeanName" filter init-param in {@code web.xml};
- * the default bean name is "sessionFactory".
- *
- * @author Juergen Hoeller
- * @since 3.1
- * @see #lookupSessionFactory
- * @see OpenSessionInViewInterceptor
- * @see OpenSessionInterceptor
- * @see org.springframework.orm.hibernate4.HibernateTransactionManager
- * @see org.springframework.transaction.support.TransactionSynchronizationManager
- * @see org.hibernate.SessionFactory#getCurrentSession()
- */
-public class OpenSessionInViewFilter extends OncePerRequestFilter {
-
- public static final String DEFAULT_SESSION_FACTORY_BEAN_NAME = "sessionFactory";
-
- private String sessionFactoryBeanName = DEFAULT_SESSION_FACTORY_BEAN_NAME;
-
-
- /**
- * Set the bean name of the SessionFactory to fetch from Spring's
- * root application context. Default is "sessionFactory".
- * @see #DEFAULT_SESSION_FACTORY_BEAN_NAME
- */
- public void setSessionFactoryBeanName(String sessionFactoryBeanName) {
- this.sessionFactoryBeanName = sessionFactoryBeanName;
- }
-
- /**
- * Return the bean name of the SessionFactory to fetch from Spring's
- * root application context.
- */
- protected String getSessionFactoryBeanName() {
- return this.sessionFactoryBeanName;
- }
-
-
- /**
- * Returns "false" so that the filter may re-bind the opened Hibernate
- * {@code Session} to each asynchronously dispatched thread and postpone
- * closing it until the very last asynchronous dispatch.
- */
- @Override
- protected boolean shouldNotFilterAsyncDispatch() {
- return false;
- }
-
- /**
- * Returns "false" so that the filter may provide a Hibernate
- * {@code Session} to each error dispatches.
- */
- @Override
- protected boolean shouldNotFilterErrorDispatch() {
- return false;
- }
-
- @Override
- protected void doFilterInternal(
- HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
- throws ServletException, IOException {
-
- SessionFactory sessionFactory = lookupSessionFactory(request);
- boolean participate = false;
-
- WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
- String key = getAlreadyFilteredAttributeName();
-
- if (TransactionSynchronizationManager.hasResource(sessionFactory)) {
- // Do not modify the Session: just set the participate flag.
- participate = true;
- }
- else {
- boolean isFirstRequest = !isAsyncDispatch(request);
- if (isFirstRequest || !applySessionBindingInterceptor(asyncManager, key)) {
- logger.debug("Opening Hibernate Session in OpenSessionInViewFilter");
- Session session = openSession(sessionFactory);
- SessionHolder sessionHolder = new SessionHolder(session);
- TransactionSynchronizationManager.bindResource(sessionFactory, sessionHolder);
-
- AsyncRequestInterceptor interceptor = new AsyncRequestInterceptor(sessionFactory, sessionHolder);
- asyncManager.registerCallableInterceptor(key, interceptor);
- asyncManager.registerDeferredResultInterceptor(key, interceptor);
- }
- }
-
- try {
- filterChain.doFilter(request, response);
- }
-
- finally {
- if (!participate) {
- SessionHolder sessionHolder =
- (SessionHolder) TransactionSynchronizationManager.unbindResource(sessionFactory);
- if (!isAsyncStarted(request)) {
- logger.debug("Closing Hibernate Session in OpenSessionInViewFilter");
- SessionFactoryUtils.closeSession(sessionHolder.getSession());
- }
- }
- }
- }
-
- /**
- * Look up the SessionFactory that this filter should use,
- * taking the current HTTP request as argument.
- *
The default implementation delegates to the {@link #lookupSessionFactory()}
- * variant without arguments.
- * @param request the current request
- * @return the SessionFactory to use
- */
- protected SessionFactory lookupSessionFactory(HttpServletRequest request) {
- return lookupSessionFactory();
- }
-
- /**
- * Look up the SessionFactory that this filter should use.
- *
The default implementation looks for a bean with the specified name
- * in Spring's root application context.
- * @return the SessionFactory to use
- * @see #getSessionFactoryBeanName
- */
- protected SessionFactory lookupSessionFactory() {
- if (logger.isDebugEnabled()) {
- logger.debug("Using SessionFactory '" + getSessionFactoryBeanName() + "' for OpenSessionInViewFilter");
- }
- WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
- return wac.getBean(getSessionFactoryBeanName(), SessionFactory.class);
- }
-
- /**
- * Open a Session for the SessionFactory that this filter uses.
- *
The default implementation delegates to the {@link SessionFactory#openSession}
- * method and sets the {@link Session}'s flush mode to "MANUAL".
- * @param sessionFactory the SessionFactory that this filter uses
- * @return the Session to use
- * @throws DataAccessResourceFailureException if the Session could not be created
- * @see org.hibernate.FlushMode#MANUAL
- */
- protected Session openSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException {
- try {
- Session session = sessionFactory.openSession();
- session.setFlushMode(FlushMode.MANUAL);
- return session;
- }
- catch (HibernateException ex) {
- throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex);
- }
- }
-
- private boolean applySessionBindingInterceptor(WebAsyncManager asyncManager, String key) {
- if (asyncManager.getCallableInterceptor(key) == null) {
- return false;
- }
- ((AsyncRequestInterceptor) asyncManager.getCallableInterceptor(key)).bindSession();
- return true;
- }
-
-}
diff --git a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/support/OpenSessionInViewInterceptor.java b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/support/OpenSessionInViewInterceptor.java
deleted file mode 100644
index 39233b0acd..0000000000
--- a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/support/OpenSessionInViewInterceptor.java
+++ /dev/null
@@ -1,211 +0,0 @@
-/*
- * Copyright 2002-2014 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate4.support;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.hibernate.FlushMode;
-import org.hibernate.HibernateException;
-import org.hibernate.Session;
-import org.hibernate.SessionFactory;
-
-import org.springframework.dao.DataAccessException;
-import org.springframework.dao.DataAccessResourceFailureException;
-import org.springframework.orm.hibernate4.SessionFactoryUtils;
-import org.springframework.orm.hibernate4.SessionHolder;
-import org.springframework.transaction.support.TransactionSynchronizationManager;
-import org.springframework.ui.ModelMap;
-import org.springframework.web.context.request.AsyncWebRequestInterceptor;
-import org.springframework.web.context.request.WebRequest;
-import org.springframework.web.context.request.async.WebAsyncManager;
-import org.springframework.web.context.request.async.WebAsyncUtils;
-
-/**
- * Spring web request interceptor that binds a Hibernate {@code Session} to the
- * thread for the entire processing of the request.
- *
- *
This class is a concrete expression of the "Open Session in View" pattern, which
- * is a pattern that allows for the lazy loading of associations in web views despite
- * the original transactions already being completed.
- *
- *
This interceptor makes Hibernate Sessions available via the current thread,
- * which will be autodetected by transaction managers. It is suitable for service layer
- * transactions via {@link org.springframework.orm.hibernate4.HibernateTransactionManager}
- * as well as for non-transactional execution (if configured appropriately).
- *
- *
In contrast to {@link OpenSessionInViewFilter}, this interceptor is configured
- * in a Spring application context and can thus take advantage of bean wiring.
- *
- *
WARNING: Applying this interceptor to existing logic can cause issues
- * that have not appeared before, through the use of a single Hibernate
- * {@code Session} for the processing of an entire request. In particular, the
- * reassociation of persistent objects with a Hibernate {@code Session} has to
- * occur at the very beginning of request processing, to avoid clashes with already
- * loaded instances of the same objects.
- *
- * @author Juergen Hoeller
- * @since 3.1
- * @see OpenSessionInViewFilter
- * @see OpenSessionInterceptor
- * @see org.springframework.orm.hibernate4.HibernateTransactionManager
- * @see org.springframework.transaction.support.TransactionSynchronizationManager
- * @see org.hibernate.SessionFactory#getCurrentSession()
- */
-public class OpenSessionInViewInterceptor implements AsyncWebRequestInterceptor {
-
- /**
- * Suffix that gets appended to the {@code SessionFactory}
- * {@code toString()} representation for the "participate in existing
- * session handling" request attribute.
- * @see #getParticipateAttributeName
- */
- public static final String PARTICIPATE_SUFFIX = ".PARTICIPATE";
-
- protected final Log logger = LogFactory.getLog(getClass());
-
- private SessionFactory sessionFactory;
-
-
- /**
- * Set the Hibernate SessionFactory that should be used to create Hibernate Sessions.
- */
- public void setSessionFactory(SessionFactory sessionFactory) {
- this.sessionFactory = sessionFactory;
- }
-
- /**
- * Return the Hibernate SessionFactory that should be used to create Hibernate Sessions.
- */
- public SessionFactory getSessionFactory() {
- return this.sessionFactory;
- }
-
-
- /**
- * Open a new Hibernate {@code Session} according and bind it to the thread via the
- * {@link org.springframework.transaction.support.TransactionSynchronizationManager}.
- */
- @Override
- public void preHandle(WebRequest request) throws DataAccessException {
- String participateAttributeName = getParticipateAttributeName();
-
- WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
- if (asyncManager.hasConcurrentResult()) {
- if (applySessionBindingInterceptor(asyncManager, participateAttributeName)) {
- return;
- }
- }
-
- if (TransactionSynchronizationManager.hasResource(getSessionFactory())) {
- // Do not modify the Session: just mark the request accordingly.
- Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
- int newCount = (count != null ? count + 1 : 1);
- request.setAttribute(getParticipateAttributeName(), newCount, WebRequest.SCOPE_REQUEST);
- }
- else {
- logger.debug("Opening Hibernate Session in OpenSessionInViewInterceptor");
- Session session = openSession();
- SessionHolder sessionHolder = new SessionHolder(session);
- TransactionSynchronizationManager.bindResource(getSessionFactory(), sessionHolder);
-
- AsyncRequestInterceptor asyncRequestInterceptor =
- new AsyncRequestInterceptor(getSessionFactory(), sessionHolder);
- asyncManager.registerCallableInterceptor(participateAttributeName, asyncRequestInterceptor);
- asyncManager.registerDeferredResultInterceptor(participateAttributeName, asyncRequestInterceptor);
- }
- }
-
- @Override
- public void postHandle(WebRequest request, ModelMap model) {
- }
-
- /**
- * Unbind the Hibernate {@code Session} from the thread and close it).
- * @see org.springframework.transaction.support.TransactionSynchronizationManager
- */
- @Override
- public void afterCompletion(WebRequest request, Exception ex) throws DataAccessException {
- if (!decrementParticipateCount(request)) {
- SessionHolder sessionHolder =
- (SessionHolder) TransactionSynchronizationManager.unbindResource(getSessionFactory());
- logger.debug("Closing Hibernate Session in OpenSessionInViewInterceptor");
- SessionFactoryUtils.closeSession(sessionHolder.getSession());
-
- }
- }
-
- private boolean decrementParticipateCount(WebRequest request) {
- String participateAttributeName = getParticipateAttributeName();
- Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
- if (count == null) {
- return false;
- }
- // Do not modify the Session: just clear the marker.
- if (count > 1) {
- request.setAttribute(participateAttributeName, count - 1, WebRequest.SCOPE_REQUEST);
- }
- else {
- request.removeAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
- }
- return true;
- }
-
- @Override
- public void afterConcurrentHandlingStarted(WebRequest request) {
- if (!decrementParticipateCount(request)) {
- TransactionSynchronizationManager.unbindResource(getSessionFactory());
- }
- }
-
- /**
- * Open a Session for the SessionFactory that this interceptor uses.
- *
The default implementation delegates to the {@link SessionFactory#openSession}
- * method and sets the {@link Session}'s flush mode to "MANUAL".
- * @return the Session to use
- * @throws DataAccessResourceFailureException if the Session could not be created
- * @see org.hibernate.FlushMode#MANUAL
- */
- protected Session openSession() throws DataAccessResourceFailureException {
- try {
- Session session = getSessionFactory().openSession();
- session.setFlushMode(FlushMode.MANUAL);
- return session;
- }
- catch (HibernateException ex) {
- throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex);
- }
- }
-
- /**
- * Return the name of the request attribute that identifies that a request is
- * already intercepted.
- *
The default implementation takes the {@code toString()} representation
- * of the {@code SessionFactory} instance and appends {@link #PARTICIPATE_SUFFIX}.
- */
- protected String getParticipateAttributeName() {
- return getSessionFactory().toString() + PARTICIPATE_SUFFIX;
- }
-
- private boolean applySessionBindingInterceptor(WebAsyncManager asyncManager, String key) {
- if (asyncManager.getCallableInterceptor(key) == null) {
- return false;
- }
- ((AsyncRequestInterceptor) asyncManager.getCallableInterceptor(key)).bindSession();
- return true;
- }
-
-}
diff --git a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/support/OpenSessionInterceptor.java b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/support/OpenSessionInterceptor.java
deleted file mode 100644
index 65be305494..0000000000
--- a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/support/OpenSessionInterceptor.java
+++ /dev/null
@@ -1,116 +0,0 @@
-/*
- * Copyright 2002-2014 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate4.support;
-
-import org.aopalliance.intercept.MethodInterceptor;
-import org.aopalliance.intercept.MethodInvocation;
-import org.hibernate.FlushMode;
-import org.hibernate.HibernateException;
-import org.hibernate.Session;
-import org.hibernate.SessionFactory;
-
-import org.springframework.beans.factory.InitializingBean;
-import org.springframework.dao.DataAccessResourceFailureException;
-import org.springframework.orm.hibernate4.SessionFactoryUtils;
-import org.springframework.orm.hibernate4.SessionHolder;
-import org.springframework.transaction.support.TransactionSynchronizationManager;
-
-/**
- * Simple AOP Alliance {@link MethodInterceptor} implementation that binds a new
- * Hibernate {@link Session} for each method invocation, if none bound before.
- *
- *
This is a simple Hibernate Session scoping interceptor along the lines of
- * {@link OpenSessionInViewInterceptor}, just for use with AOP setup instead of
- * MVC setup. It opens a new {@link Session} with flush mode "MANUAL" since the
- * Session is only meant for reading, except when participating in a transaction.
- *
- * @author Juergen Hoeller
- * @since 4.0.2
- * @see OpenSessionInViewInterceptor
- * @see OpenSessionInViewFilter
- * @see org.springframework.orm.hibernate4.HibernateTransactionManager
- * @see org.springframework.transaction.support.TransactionSynchronizationManager
- * @see org.hibernate.SessionFactory#getCurrentSession()
- */
-public class OpenSessionInterceptor implements MethodInterceptor, InitializingBean {
-
- private SessionFactory sessionFactory;
-
-
- /**
- * Set the Hibernate SessionFactory that should be used to create Hibernate Sessions.
- */
- public void setSessionFactory(SessionFactory sessionFactory) {
- this.sessionFactory = sessionFactory;
- }
-
- /**
- * Return the Hibernate SessionFactory that should be used to create Hibernate Sessions.
- */
- public SessionFactory getSessionFactory() {
- return this.sessionFactory;
- }
-
- @Override
- public void afterPropertiesSet() {
- if (getSessionFactory() == null) {
- throw new IllegalArgumentException("Property 'sessionFactory' is required");
- }
- }
-
-
- @Override
- public Object invoke(MethodInvocation invocation) throws Throwable {
- SessionFactory sf = getSessionFactory();
- if (!TransactionSynchronizationManager.hasResource(sf)) {
- // New Session to be bound for the current method's scope...
- Session session = openSession();
- try {
- TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
- return invocation.proceed();
- }
- finally {
- SessionFactoryUtils.closeSession(session);
- TransactionSynchronizationManager.unbindResource(sf);
- }
- }
- else {
- // Pre-bound Session found -> simply proceed.
- return invocation.proceed();
- }
- }
-
- /**
- * Open a Session for the SessionFactory that this interceptor uses.
- *
The default implementation delegates to the {@link SessionFactory#openSession}
- * method and sets the {@link Session}'s flush mode to "MANUAL".
- * @return the Session to use
- * @throws DataAccessResourceFailureException if the Session could not be created
- * @see org.hibernate.FlushMode#MANUAL
- */
- protected Session openSession() throws DataAccessResourceFailureException {
- try {
- Session session = getSessionFactory().openSession();
- session.setFlushMode(FlushMode.MANUAL);
- return session;
- }
- catch (HibernateException ex) {
- throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex);
- }
- }
-
-}
diff --git a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/support/package-info.java b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/support/package-info.java
deleted file mode 100644
index 546c98d593..0000000000
--- a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/support/package-info.java
+++ /dev/null
@@ -1,4 +0,0 @@
-/**
- * Classes supporting the {@code org.springframework.orm.hibernate4} package.
- */
-package org.springframework.orm.hibernate4.support;
diff --git a/spring-orm-hibernate4/src/test/java/org/springframework/orm/hibernate4/HibernateTemplateTests.java b/spring-orm-hibernate4/src/test/java/org/springframework/orm/hibernate4/HibernateTemplateTests.java
deleted file mode 100644
index 806ae297e6..0000000000
--- a/spring-orm-hibernate4/src/test/java/org/springframework/orm/hibernate4/HibernateTemplateTests.java
+++ /dev/null
@@ -1,1189 +0,0 @@
-/*
- * Copyright 2002-2014 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate4;
-
-import java.lang.reflect.Proxy;
-import java.sql.SQLException;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.List;
-
-import org.hibernate.Criteria;
-import org.hibernate.Filter;
-import org.hibernate.FlushMode;
-import org.hibernate.HibernateException;
-import org.hibernate.JDBCException;
-import org.hibernate.NonUniqueResultException;
-import org.hibernate.ObjectDeletedException;
-import org.hibernate.ObjectNotFoundException;
-import org.hibernate.PersistentObjectException;
-import org.hibernate.PropertyValueException;
-import org.hibernate.Query;
-import org.hibernate.QueryException;
-import org.hibernate.ReplicationMode;
-import org.hibernate.Session;
-import org.hibernate.SessionFactory;
-import org.hibernate.StaleObjectStateException;
-import org.hibernate.StaleStateException;
-import org.hibernate.TransientObjectException;
-import org.hibernate.UnresolvableObjectException;
-import org.hibernate.WrongClassException;
-import org.hibernate.exception.ConstraintViolationException;
-import org.hibernate.exception.DataException;
-import org.hibernate.exception.JDBCConnectionException;
-import org.hibernate.exception.LockAcquisitionException;
-import org.hibernate.exception.SQLGrammarException;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import org.mockito.InOrder;
-
-import org.springframework.dao.CannotAcquireLockException;
-import org.springframework.dao.DataAccessResourceFailureException;
-import org.springframework.dao.DataIntegrityViolationException;
-import org.springframework.dao.IncorrectResultSizeDataAccessException;
-import org.springframework.dao.InvalidDataAccessApiUsageException;
-import org.springframework.dao.InvalidDataAccessResourceUsageException;
-import org.springframework.tests.sample.beans.TestBean;
-import org.springframework.transaction.support.TransactionSynchronizationManager;
-
-import static org.junit.Assert.*;
-import static org.mockito.BDDMockito.*;
-
-/**
- * @author Juergen Hoeller
- * @since 4.0.1
- */
-@SuppressWarnings({ "rawtypes", "unchecked" })
-public class HibernateTemplateTests {
-
- private SessionFactory sessionFactory;
- private Session session;
- private HibernateTemplate hibernateTemplate;
-
- @Before
- public void setUp() {
- this.sessionFactory = mock(SessionFactory.class);
- this.session = mock(Session.class);
- this.hibernateTemplate = new HibernateTemplate(sessionFactory);
- given(sessionFactory.getCurrentSession()).willReturn(session);
- }
-
- @After
- public void tearDown() {
- assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
- assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
- }
-
- @Test
- public void testExecuteWithNewSession() {
- given(sessionFactory.getCurrentSession()).willThrow(new HibernateException("no current session"));
- given(sessionFactory.openSession()).willReturn(session);
-
- final List l = new ArrayList();
- l.add("test");
- List result = hibernateTemplate.execute(new HibernateCallback() {
- @Override
- public List doInHibernate(Session session) {
- return l;
- }
- });
- assertTrue("Correct result list", result == l);
- verify(session).close();
- }
-
- @Test
- public void testExecuteWithNewSessionAndFilter() {
- given(sessionFactory.getCurrentSession()).willThrow(new HibernateException("no current session"));
- given(sessionFactory.openSession()).willReturn(session);
- hibernateTemplate.setFilterNames("myFilter");
-
- final List l = new ArrayList();
- l.add("test");
- List result = hibernateTemplate.execute(new HibernateCallback() {
- @Override
- public List doInHibernate(Session session) {
- return l;
- }
- });
- assertTrue("Correct result list", result == l);
- verify(session).enableFilter("myFilter");
- verify(session).close();
- }
-
- @Test
- public void testExecuteWithNewSessionAndFilters() {
- given(sessionFactory.getCurrentSession()).willThrow(new HibernateException("no current session"));
- given(sessionFactory.openSession()).willReturn(session);
- hibernateTemplate.setFilterNames("myFilter", "yourFilter");
-
- final List l = new ArrayList();
- l.add("test");
- List result = hibernateTemplate.execute(new HibernateCallback() {
- @Override
- public List doInHibernate(Session session) {
- return l;
- }
- });
- assertTrue("Correct result list", result == l);
- InOrder ordered = inOrder(session);
- ordered.verify(session).enableFilter("myFilter");
- ordered.verify(session).enableFilter("yourFilter");
- ordered.verify(session).close();
- }
-
- @Test
- public void testExecuteWithThreadBound() {
- final List l = new ArrayList();
- l.add("test");
- List result = hibernateTemplate.execute(new HibernateCallback() {
- @Override
- public List doInHibernate(Session session) {
- return l;
- }
- });
- assertTrue("Correct result list", result == l);
- }
-
- @Test
- public void testExecuteWithThreadBoundAndFilter() {
- hibernateTemplate.setFilterNames("myFilter");
-
- final List l = new ArrayList();
- l.add("test");
- List result = hibernateTemplate.execute(new HibernateCallback() {
- @Override
- public List doInHibernate(Session session) {
- return l;
- }
- });
- assertTrue("Correct result list", result == l);
-
- InOrder ordered = inOrder(session);
- ordered.verify(session).enableFilter("myFilter");
- ordered.verify(session).disableFilter("myFilter");
- }
-
- @Test
- public void testExecuteWithThreadBoundAndFilters() {
- hibernateTemplate.setFilterNames("myFilter", "yourFilter");
-
- final List l = new ArrayList();
- l.add("test");
- List result = hibernateTemplate.execute(new HibernateCallback() {
- @Override
- public List doInHibernate(Session session) {
- return l;
- }
- });
- assertTrue("Correct result list", result == l);
-
- InOrder ordered = inOrder(session);
- ordered.verify(session).enableFilter("myFilter");
- ordered.verify(session).enableFilter("yourFilter");
- ordered.verify(session).disableFilter("myFilter");
- ordered.verify(session).disableFilter("yourFilter");
- }
-
- @Test
- public void testExecuteWithThreadBoundAndParameterizedFilter() {
- Filter filter = mock(Filter.class);
- given(session.enableFilter("myFilter")).willReturn(filter);
- hibernateTemplate.setFilterNames("myFilter");
-
- final List l = new ArrayList();
- l.add("test");
- Filter f = hibernateTemplate.enableFilter("myFilter");
- assertTrue("Correct filter", f == filter);
-
- InOrder ordered = inOrder(session);
- ordered.verify(session).getEnabledFilter("myFilter");
- ordered.verify(session).enableFilter("myFilter");
- }
-
- @Test
- public void testExecuteWithThreadBoundAndParameterizedExistingFilter() {
- Filter filter = mock(Filter.class);
- given(session.enableFilter("myFilter")).willReturn(filter);
- hibernateTemplate.setFilterNames("myFilter");
-
- final List l = new ArrayList();
- l.add("test");
- Filter f = hibernateTemplate.enableFilter("myFilter");
- assertTrue("Correct filter", f == filter);
-
- verify(session).getEnabledFilter("myFilter");
- }
-
- @Test
- public void testExecuteWithCacheQueries() {
- Query query1 = mock(Query.class);
- Query query2 = mock(Query.class);
- Criteria criteria = mock(Criteria.class);
- given(session.createQuery("some query")).willReturn(query1);
- given(query1.setCacheable(true)).willReturn(query1);
- given(session.getNamedQuery("some query name")).willReturn(query2);
- given(query2.setCacheable(true)).willReturn(query2);
- given(session.createCriteria(TestBean.class)).willReturn(criteria);
- given(criteria.setCacheable(true)).willReturn(criteria);
-
- hibernateTemplate.setCacheQueries(true);
- hibernateTemplate.execute(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session sess) {
- assertNotSame(session, sess);
- assertTrue(Proxy.isProxyClass(sess.getClass()));
- sess.createQuery("some query");
- sess.getNamedQuery("some query name");
- sess.createCriteria(TestBean.class);
- // should be ignored
- sess.close();
- return null;
- }
- });
- }
-
- @Test
- public void testExecuteWithCacheQueriesAndCacheRegion() {
- Query query1 = mock(Query.class);
- Query query2 = mock(Query.class);
- Criteria criteria = mock(Criteria.class);
- given(session.createQuery("some query")).willReturn(query1);
- given(query1.setCacheable(true)).willReturn(query1);
- given(query1.setCacheRegion("myRegion")).willReturn(query1);
- given(session.getNamedQuery("some query name")).willReturn(query2);
- given(query2.setCacheable(true)).willReturn(query2);
- given(query2.setCacheRegion("myRegion")).willReturn(query2);
- given(session.createCriteria(TestBean.class)).willReturn(criteria);
- given(criteria.setCacheable(true)).willReturn(criteria);
- given(criteria.setCacheRegion("myRegion")).willReturn(criteria);
-
- hibernateTemplate.setCacheQueries(true);
- hibernateTemplate.setQueryCacheRegion("myRegion");
- hibernateTemplate.execute(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session sess) {
- assertNotSame(session, sess);
- assertTrue(Proxy.isProxyClass(sess.getClass()));
- sess.createQuery("some query");
- sess.getNamedQuery("some query name");
- sess.createCriteria(TestBean.class);
- // should be ignored
- sess.close();
- return null;
- }
- });
- }
-
- @Test
- public void testExecuteWithCacheQueriesAndCacheRegionAndNativeSession() {
- Query query1 = mock(Query.class);
- Query query2 = mock(Query.class);
- Criteria criteria = mock(Criteria.class);
-
- given(session.createQuery("some query")).willReturn(query1);
- given(session.getNamedQuery("some query name")).willReturn(query2);
- given(session.createCriteria(TestBean.class)).willReturn(criteria);
-
- hibernateTemplate.setExposeNativeSession(true);
- hibernateTemplate.setCacheQueries(true);
- hibernateTemplate.setQueryCacheRegion("myRegion");
- hibernateTemplate.execute(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session sess) {
- assertSame(session, sess);
- sess.createQuery("some query");
- sess.getNamedQuery("some query name");
- sess.createCriteria(TestBean.class);
- return null;
- }
- });
- }
-
- @Test
- public void testExecuteWithFetchSizeAndMaxResults() {
- Query query1 = mock(Query.class);
- Query query2 = mock(Query.class);
- Criteria criteria = mock(Criteria.class);
-
- given(session.createQuery("some query")).willReturn(query1);
- given(query1.setFetchSize(10)).willReturn(query1);
- given(query1.setMaxResults(20)).willReturn(query1);
- given(session.getNamedQuery("some query name")).willReturn(query2);
- given(query2.setFetchSize(10)).willReturn(query2);
- given(query2.setMaxResults(20)).willReturn(query2);
- given(session.createCriteria(TestBean.class)).willReturn(criteria);
- given(criteria.setFetchSize(10)).willReturn(criteria);
- given(criteria.setMaxResults(20)).willReturn(criteria);
-
- hibernateTemplate.setFetchSize(10);
- hibernateTemplate.setMaxResults(20);
- hibernateTemplate.execute(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session sess) {
- sess.createQuery("some query");
- sess.getNamedQuery("some query name");
- sess.createCriteria(TestBean.class);
- return null;
- }
- });
- }
-
- @Test
- public void testGet() {
- TestBean tb = new TestBean();
- given(session.get(TestBean.class, "")).willReturn(tb);
- Object result = hibernateTemplate.get(TestBean.class, "");
- assertTrue("Correct result", result == tb);
- }
-
- @Test
- public void testGetWithEntityName() {
- TestBean tb = new TestBean();
- given(session.get("myEntity", "")).willReturn(tb);
- Object result = hibernateTemplate.get("myEntity", "");
- assertTrue("Correct result", result == tb);
- }
-
- @Test
- public void testLoad() {
- TestBean tb = new TestBean();
- given(session.load(TestBean.class, "")).willReturn(tb);
- Object result = hibernateTemplate.load(TestBean.class, "");
- assertTrue("Correct result", result == tb);
- }
-
- @Test
- public void testLoadWithNotFound() {
- ObjectNotFoundException onfex = new ObjectNotFoundException("id", TestBean.class.getName());
- given(session.load(TestBean.class, "id")).willThrow(onfex);
- try {
- hibernateTemplate.load(TestBean.class, "id");
- fail("Should have thrown HibernateObjectRetrievalFailureException");
- }
- catch (HibernateObjectRetrievalFailureException ex) {
- // expected
- assertEquals(TestBean.class.getName(), ex.getPersistentClassName());
- assertEquals("id", ex.getIdentifier());
- assertEquals(onfex, ex.getCause());
- }
- }
-
- @Test
- public void testLoadWithEntityName() {
- TestBean tb = new TestBean();
- given(session.load("myEntity", "")).willReturn(tb);
- Object result = hibernateTemplate.load("myEntity", "");
- assertTrue("Correct result", result == tb);
- }
-
- @Test
- public void testLoadWithObject() {
- TestBean tb = new TestBean();
- hibernateTemplate.load(tb, "");
- verify(session).load(tb, "");
- }
-
- @Test
- public void testLoadAll() {
- Criteria criteria = mock(Criteria.class);
- List list = new ArrayList();
- given(session.createCriteria(TestBean.class)).willReturn(criteria);
- given(criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)).willReturn(criteria);
- given(criteria.list()).willReturn(list);
- List result = hibernateTemplate.loadAll(TestBean.class);
- assertTrue("Correct result", result == list);
- }
-
- @Test
- public void testLoadAllWithCacheable() {
- Criteria criteria = mock(Criteria.class);
- List list = new ArrayList();
- given(session.createCriteria(TestBean.class)).willReturn(criteria);
- given(criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)).willReturn(criteria);
- given(criteria.setCacheable(true)).willReturn(criteria);
- given(criteria.list()).willReturn(list);
-
- hibernateTemplate.setCacheQueries(true);
- List result = hibernateTemplate.loadAll(TestBean.class);
- assertTrue("Correct result", result == list);
- verify(criteria).setCacheable(true);
- }
-
- @Test
- public void testLoadAllWithCacheableAndCacheRegion() {
- Criteria criteria = mock(Criteria.class);
- List list = new ArrayList();
- given(session.createCriteria(TestBean.class)).willReturn(criteria);
- given(criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)).willReturn(criteria);
- given(criteria.setCacheable(true)).willReturn(criteria);
- given(criteria.setCacheRegion("myCacheRegion")).willReturn(criteria);
- given(criteria.list()).willReturn(list);
-
- hibernateTemplate.setCacheQueries(true);
- hibernateTemplate.setQueryCacheRegion("myCacheRegion");
- List result = hibernateTemplate.loadAll(TestBean.class);
- assertTrue("Correct result", result == list);
- verify(criteria).setCacheable(true);
- verify(criteria).setCacheRegion("myCacheRegion");
- }
-
- @Test public void testRefresh() {
- TestBean tb = new TestBean();
- hibernateTemplate.refresh(tb);
- verify(session).refresh(tb);
- }
-
- @Test public void testContains() {
- TestBean tb = new TestBean();
- given(session.contains(tb)).willReturn(true);
- assertTrue(hibernateTemplate.contains(tb));
- }
-
- @Test
- public void testEvict() {
- TestBean tb = new TestBean();
- hibernateTemplate.evict(tb);
- verify(session).evict(tb);
- }
-
- @Test
- public void testSave() {
- TestBean tb = new TestBean();
- given(session.getFlushMode()).willReturn(FlushMode.AUTO);
- given(session.save(tb)).willReturn(0);
- assertEquals("Correct return value", hibernateTemplate.save(tb), 0);
- }
-
- @Test
- public void testSaveWithEntityName() {
- TestBean tb = new TestBean();
- given(session.getFlushMode()).willReturn(FlushMode.AUTO);
- given(session.save("myEntity", tb)).willReturn(0);
- assertEquals("Correct return value", hibernateTemplate.save("myEntity", tb), 0);
- }
-
- @Test
- public void testUpdate() {
- TestBean tb = new TestBean();
- given(session.getFlushMode()).willReturn(FlushMode.AUTO);
- hibernateTemplate.update(tb);
- verify(session).update(tb);
- }
-
- @Test
- public void testUpdateWithEntityName() {
- TestBean tb = new TestBean();
- given(session.getFlushMode()).willReturn(FlushMode.AUTO);
- hibernateTemplate.update("myEntity", tb);
- verify(session).update("myEntity", tb);
- }
-
- @Test
- public void testSaveOrUpdate() {
- TestBean tb = new TestBean();
- given(session.getFlushMode()).willReturn(FlushMode.AUTO);
- hibernateTemplate.saveOrUpdate(tb);
- verify(session).saveOrUpdate(tb);
- }
-
- @Test
- public void testSaveOrUpdateWithFlushModeNever() {
- TestBean tb = new TestBean();
- given(session.getFlushMode()).willReturn(FlushMode.MANUAL);
- try {
- hibernateTemplate.saveOrUpdate(tb);
- fail("Should have thrown InvalidDataAccessApiUsageException");
- }
- catch (InvalidDataAccessApiUsageException ex) {
- // expected
- }
- }
-
- @Test
- public void testSaveOrUpdateWithEntityName() {
- TestBean tb = new TestBean();
-
- given(session.getFlushMode()).willReturn(FlushMode.AUTO);
- hibernateTemplate.saveOrUpdate("myEntity", tb);
- verify(session).saveOrUpdate("myEntity", tb);
- }
-
- @Test
- public void testReplicate() {
- TestBean tb = new TestBean();
- given(session.getFlushMode()).willReturn(FlushMode.AUTO);
- hibernateTemplate.replicate(tb, ReplicationMode.LATEST_VERSION);
- verify(session).replicate(tb, ReplicationMode.LATEST_VERSION);
- }
-
- @Test
- public void testReplicateWithEntityName() {
- TestBean tb = new TestBean();
- given(session.getFlushMode()).willReturn(FlushMode.AUTO);
- hibernateTemplate.replicate("myEntity", tb, ReplicationMode.LATEST_VERSION);
- verify(session).replicate("myEntity", tb, ReplicationMode.LATEST_VERSION);
- }
-
- @Test
- public void testPersist() {
- TestBean tb = new TestBean();
- given(session.getFlushMode()).willReturn(FlushMode.AUTO);
- hibernateTemplate.persist(tb);
- verify(session).persist(tb);
- }
-
- @Test
- public void testPersistWithEntityName() {
- TestBean tb = new TestBean();
- given(session.getFlushMode()).willReturn(FlushMode.AUTO);
- hibernateTemplate.persist("myEntity", tb);
- verify(session).persist("myEntity", tb);
- }
-
- @Test
- public void testMerge() {
- TestBean tb = new TestBean();
- TestBean tbMerged = new TestBean();
- given(session.getFlushMode()).willReturn(FlushMode.AUTO);
- given(session.merge(tb)).willReturn(tbMerged);
- assertSame(tbMerged, hibernateTemplate.merge(tb));
- }
-
- @Test
- public void testMergeWithEntityName() {
- TestBean tb = new TestBean();
- TestBean tbMerged = new TestBean();
- given(session.getFlushMode()).willReturn(FlushMode.AUTO);
- given(session.merge("myEntity", tb)).willReturn(tbMerged);
- assertSame(tbMerged, hibernateTemplate.merge("myEntity", tb));
- }
-
- @Test
- public void testDelete() {
- TestBean tb = new TestBean();
- given(session.getFlushMode()).willReturn(FlushMode.AUTO);
- hibernateTemplate.delete(tb);
- verify(session).delete(tb);
- }
-
- @Test
- public void testDeleteWithEntityName() {
- TestBean tb = new TestBean();
- given(session.getFlushMode()).willReturn(FlushMode.AUTO);
- hibernateTemplate.delete("myEntity", tb);
- verify(session).delete("myEntity", tb);
- }
-
- @Test
- public void testDeleteAll() {
- TestBean tb1 = new TestBean();
- TestBean tb2 = new TestBean();
- given(session.getFlushMode()).willReturn(FlushMode.AUTO);
- List tbs = new ArrayList();
- tbs.add(tb1);
- tbs.add(tb2);
- hibernateTemplate.deleteAll(tbs);
- verify(session).delete(same(tb1));
- verify(session).delete(same(tb2));
- }
-
- @Test
- public void testFlush() {
- hibernateTemplate.flush();
- verify(session).flush();
- }
-
- @Test
- public void testClear() {
- hibernateTemplate.clear();
- verify(session).clear();
- }
-
- @Test
- public void testFind() {
- Query query = mock(Query.class);
- List list = new ArrayList();
- given(session.createQuery("some query string")).willReturn(query);
- given(query.list()).willReturn(list);
- List result = hibernateTemplate.find("some query string");
- assertTrue("Correct list", result == list);
- }
-
- @Test
- public void testFindWithParameter() {
- Query query = mock(Query.class);
- List list = new ArrayList();
- given(session.createQuery("some query string")).willReturn(query);
- given(query.setParameter(0, "myvalue")).willReturn(query);
- given(query.list()).willReturn(list);
- List result = hibernateTemplate.find("some query string", "myvalue");
- assertTrue("Correct list", result == list);
- verify(query).setParameter(0, "myvalue");
- }
-
- @Test
- public void testFindWithParameters() {
- Query query = mock(Query.class);
- List list = new ArrayList();
- given(session.createQuery("some query string")).willReturn(query);
- given(query.setParameter(0, "myvalue1")).willReturn(query);
- given(query.setParameter(1, 2)).willReturn(query);
- given(query.list()).willReturn(list);
- List result = hibernateTemplate.find("some query string", "myvalue1", 2);
- assertTrue("Correct list", result == list);
- verify(query).setParameter(0, "myvalue1");
- verify(query).setParameter(1, 2);
- }
-
- @Test
- public void testFindWithNamedParameter() {
- Query query = mock(Query.class);
- List list = new ArrayList();
- given(session.createQuery("some query string")).willReturn(query);
- given(query.setParameter("myparam", "myvalue")).willReturn(query);
- given(query.list()).willReturn(list);
- List result = hibernateTemplate.findByNamedParam("some query string", "myparam", "myvalue");
- assertTrue("Correct list", result == list);
- verify(query).setParameter("myparam", "myvalue");
- }
-
- @Test
- public void testFindWithNamedParameters() {
- Query query = mock(Query.class);
- List list = new ArrayList();
- given(session.createQuery("some query string")).willReturn(query);
- given(query.setParameter("myparam1", "myvalue1")).willReturn(query);
- given(query.setParameter("myparam2", 2)).willReturn(query);
- given(query.list()).willReturn(list);
- List result = hibernateTemplate.findByNamedParam("some query string",
- new String[]{"myparam1", "myparam2"},
- new Object[]{"myvalue1", 2});
- assertTrue("Correct list", result == list);
- verify(query).setParameter("myparam1", "myvalue1");
- verify(query).setParameter("myparam2", 2);
- }
-
- @Test
- public void testFindByValueBean() {
- Query query = mock(Query.class);
- TestBean tb = new TestBean();
- List list = new ArrayList();
- given(session.createQuery("some query string")).willReturn(query);
- given(query.setProperties(tb)).willReturn(query);
- given(query.list()).willReturn(list);
- List result = hibernateTemplate.findByValueBean("some query string", tb);
- assertTrue("Correct list", result == list);
- verify(query).setProperties(tb);
- }
-
- @Test
- public void testFindByNamedQuery() {
- Query query = mock(Query.class);
- List list = new ArrayList();
- given(session.getNamedQuery("some query name")).willReturn(query);
- given(query.list()).willReturn(list);
- List result = hibernateTemplate.findByNamedQuery("some query name");
- assertTrue("Correct list", result == list);
- }
-
- @Test
- public void testFindByNamedQueryWithParameter() {
- Query query = mock(Query.class);
- List list = new ArrayList();
- given(session.getNamedQuery("some query name")).willReturn(query);
- given(query.setParameter(0, "myvalue")).willReturn(query);
- given(query.list()).willReturn(list);
- List result = hibernateTemplate.findByNamedQuery("some query name", "myvalue");
- assertTrue("Correct list", result == list);
- verify(query).setParameter(0, "myvalue");
- }
-
- @Test
- public void testFindByNamedQueryWithParameters() {
- Query query = mock(Query.class);
- List list = new ArrayList();
- given(session.getNamedQuery("some query name")).willReturn(query);
- given(query.setParameter(0, "myvalue1")).willReturn(query);
- given(query.setParameter(1, 2)).willReturn(query);
- given(query.list()).willReturn(list);
- List result = hibernateTemplate.findByNamedQuery("some query name", new Object[] {"myvalue1", 2});
- assertTrue("Correct list", result == list);
- verify(query).setParameter(0, "myvalue1");
- verify(query).setParameter(1, 2);
- }
-
- @Test
- public void testFindByNamedQueryWithNamedParameter() {
- Query query = mock(Query.class);
- List list = new ArrayList();
- given(session.getNamedQuery("some query name")).willReturn(query);
- given(query.setParameter("myparam", "myvalue")).willReturn(query);
- given(query.list()).willReturn(list);
- List result = hibernateTemplate.findByNamedQueryAndNamedParam("some query name", "myparam", "myvalue");
- assertTrue("Correct list", result == list);
- verify(query).setParameter("myparam", "myvalue");
- }
-
- @Test
- public void testFindByNamedQueryWithNamedParameters() {
- Query query = mock(Query.class);
- List list = new ArrayList();
- given(session.getNamedQuery("some query name")).willReturn(query);
- given(query.setParameter("myparam1", "myvalue1")).willReturn(query);
- given(query.setParameter("myparam2", 2)).willReturn(query);
- given(query.list()).willReturn(list);
- List result = hibernateTemplate.findByNamedQueryAndNamedParam("some query name",
- new String[]{"myparam1", "myparam2"},
- new Object[]{"myvalue1", 2});
- assertTrue("Correct list", result == list);
- verify(query).setParameter("myparam1", "myvalue1");
- verify(query).setParameter("myparam2", 2);
- }
-
- @Test
- public void testFindByNamedQueryAndValueBean() {
- Query query = mock(Query.class);
- TestBean tb = new TestBean();
- List list = new ArrayList();
- given(session.getNamedQuery("some query name")).willReturn(query);
- given(query.setProperties(tb)).willReturn(query);
- given(query.list()).willReturn(list);
- List result = hibernateTemplate.findByNamedQueryAndValueBean("some query name", tb);
- assertTrue("Correct list", result == list);
- verify(query).setProperties(tb);
- }
-
- @Test
- public void testFindWithCacheable() {
- Query query = mock(Query.class);
- List list = new ArrayList();
- given(session.createQuery("some query string")).willReturn(query);
- given(query.setCacheable(true)).willReturn(query);
- given(query.list()).willReturn(list);
- hibernateTemplate.setCacheQueries(true);
- List result = hibernateTemplate.find("some query string");
- assertTrue("Correct list", result == list);
- verify(query).setCacheable(true);
- }
-
- @Test
- public void testFindWithCacheableAndCacheRegion() {
- Query query = mock(Query.class);
- List list = new ArrayList();
- given(session.createQuery("some query string")).willReturn(query);
- given(query.setCacheable(true)).willReturn(query);
- given(query.setCacheRegion("myCacheRegion")).willReturn(query);
- given(query.list()).willReturn(list);
- hibernateTemplate.setCacheQueries(true);
- hibernateTemplate.setQueryCacheRegion("myCacheRegion");
- List result = hibernateTemplate.find("some query string");
- assertTrue("Correct list", result == list);
- verify(query).setCacheable(true);
- verify(query).setCacheRegion("myCacheRegion");
- }
-
- @Test
- public void testFindByNamedQueryWithCacheable() {
- Query query = mock(Query.class);
- List list = new ArrayList();
- given(session.getNamedQuery("some query name")).willReturn(query);
- given(query.setCacheable(true)).willReturn(query);
- given(query.list()).willReturn(list);
- hibernateTemplate.setCacheQueries(true);
- List result = hibernateTemplate.findByNamedQuery("some query name");
- assertTrue("Correct list", result == list);
- verify(query).setCacheable(true);
- }
-
- @Test
- public void testFindByNamedQueryWithCacheableAndCacheRegion() {
- Query query = mock(Query.class);
- List list = new ArrayList();
- given(session.getNamedQuery("some query name")).willReturn(query);
- given(query.setCacheable(true)).willReturn(query);
- given(query.setCacheRegion("myCacheRegion")).willReturn(query);
- given(query.list()).willReturn(list);
- hibernateTemplate.setCacheQueries(true);
- hibernateTemplate.setQueryCacheRegion("myCacheRegion");
- List result = hibernateTemplate.findByNamedQuery("some query name");
- assertTrue("Correct list", result == list);
- verify(query).setCacheable(true);
- verify(query).setCacheRegion("myCacheRegion");
- }
-
- @Test
- public void testIterate() {
- Query query = mock(Query.class);
- Iterator it = Collections.EMPTY_LIST.iterator();
- given(session.createQuery("some query string")).willReturn(query);
- given(query.iterate()).willReturn(it);
- Iterator result = hibernateTemplate.iterate("some query string");
- assertTrue("Correct list", result == it);
- }
-
- @Test
- public void testIterateWithParameter() {
- Query query = mock(Query.class);
- Iterator it = Collections.EMPTY_LIST.iterator();
- given(session.createQuery("some query string")).willReturn(query);
- given(query.setParameter(0, "myvalue")).willReturn(query);
- given(query.iterate()).willReturn(it);
- Iterator result = hibernateTemplate.iterate("some query string", "myvalue");
- assertTrue("Correct list", result == it);
- verify(query).setParameter(0, "myvalue");
- }
-
- @Test
- public void testIterateWithParameters() {
- Query query = mock(Query.class);
- Iterator it = Collections.EMPTY_LIST.iterator();
- given(session.createQuery("some query string")).willReturn(query);
- given(query.setParameter(0, "myvalue1")).willReturn(query);
- given(query.setParameter(1, 2)).willReturn(query);
- given(query.iterate()).willReturn(it);
- Iterator result = hibernateTemplate.iterate("some query string", "myvalue1", 2);
- assertTrue("Correct list", result == it);
- verify(query).setParameter(0, "myvalue1");
- verify(query).setParameter(1, 2);
- }
-
- @Test
- public void testBulkUpdate() {
- Query query = mock(Query.class);
- given(session.createQuery("some query string")).willReturn(query);
- given(query.executeUpdate()).willReturn(5);
- int result = hibernateTemplate.bulkUpdate("some query string");
- assertTrue("Correct list", result == 5);
- }
-
- @Test
- public void testBulkUpdateWithParameter() {
- Query query = mock(Query.class);
- given(session.createQuery("some query string")).willReturn(query);
- given(query.setParameter(0, "myvalue")).willReturn(query);
- given(query.executeUpdate()).willReturn(5);
- int result = hibernateTemplate.bulkUpdate("some query string", "myvalue");
- assertTrue("Correct list", result == 5);
- verify(query).setParameter(0, "myvalue");
- }
-
- @Test
- public void testBulkUpdateWithParameters() {
- Query query = mock(Query.class);
- given(session.createQuery("some query string")).willReturn(query);
- given(query.setParameter(0, "myvalue1")).willReturn(query);
- given(query.setParameter(1, 2)).willReturn(query);
- given(query.executeUpdate()).willReturn(5);
- int result = hibernateTemplate.bulkUpdate("some query string", "myvalue1", 2);
- assertTrue("Correct list", result == 5);
- verify(query).setParameter(0, "myvalue1");
- verify(query).setParameter(1, 2);
- }
-
- @Test
- public void testExceptions() {
- SQLException sqlEx = new SQLException("argh", "27");
-
- final JDBCConnectionException jcex = new JDBCConnectionException("mymsg", sqlEx);
- try {
- hibernateTemplate.execute(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) {
- throw jcex;
- }
- });
- fail("Should have thrown DataAccessResourceFailureException");
- }
- catch (DataAccessResourceFailureException ex) {
- // expected
- assertEquals(jcex, ex.getCause());
- assertTrue(ex.getMessage().contains("mymsg"));
- }
-
- final SQLGrammarException sgex = new SQLGrammarException("mymsg", sqlEx);
- try {
- hibernateTemplate.execute(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) {
- throw sgex;
- }
- });
- fail("Should have thrown InvalidDataAccessResourceUsageException");
- }
- catch (InvalidDataAccessResourceUsageException ex) {
- // expected
- assertEquals(sgex, ex.getCause());
- assertTrue(ex.getMessage().contains("mymsg"));
- }
-
- final LockAcquisitionException laex = new LockAcquisitionException("mymsg", sqlEx);
- try {
- hibernateTemplate.execute(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) {
- throw laex;
- }
- });
- fail("Should have thrown CannotAcquireLockException");
- }
- catch (CannotAcquireLockException ex) {
- // expected
- assertEquals(laex, ex.getCause());
- assertTrue(ex.getMessage().contains("mymsg"));
- }
-
- final ConstraintViolationException cvex = new ConstraintViolationException("mymsg", sqlEx, "myconstraint");
- try {
- hibernateTemplate.execute(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) {
- throw cvex;
- }
- });
- fail("Should have thrown DataIntegrityViolationException");
- }
- catch (DataIntegrityViolationException ex) {
- // expected
- assertEquals(cvex, ex.getCause());
- assertTrue(ex.getMessage().contains("mymsg"));
- }
-
- final DataException dex = new DataException("mymsg", sqlEx);
- try {
- hibernateTemplate.execute(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) {
- throw dex;
- }
- });
- fail("Should have thrown DataIntegrityViolationException");
- }
- catch (DataIntegrityViolationException ex) {
- // expected
- assertEquals(dex, ex.getCause());
- assertTrue(ex.getMessage().contains("mymsg"));
- }
-
- final JDBCException jdex = new JDBCException("mymsg", sqlEx);
- try {
- hibernateTemplate.execute(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) {
- throw jdex;
- }
- });
- fail("Should have thrown HibernateJdbcException");
- }
- catch (HibernateJdbcException ex) {
- // expected
- assertEquals(jdex, ex.getCause());
- assertTrue(ex.getMessage().contains("mymsg"));
- }
-
- final PropertyValueException pvex = new PropertyValueException("mymsg", "myentity", "myproperty");
- try {
- hibernateTemplate.execute(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) {
- throw pvex;
- }
- });
- fail("Should have thrown DataIntegrityViolationException");
- }
- catch (DataIntegrityViolationException ex) {
- // expected
- assertEquals(pvex, ex.getCause());
- assertTrue(ex.getMessage().contains("mymsg"));
- }
-
- try {
- hibernateTemplate.execute(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) {
- throw new PersistentObjectException("");
- }
- });
- fail("Should have thrown InvalidDataAccessApiUsageException");
- }
- catch (InvalidDataAccessApiUsageException ex) {
- // expected
- }
-
- try {
- hibernateTemplate.execute(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) {
- throw new TransientObjectException("");
- }
- });
- fail("Should have thrown InvalidDataAccessApiUsageException");
- }
- catch (InvalidDataAccessApiUsageException ex) {
- // expected
- }
-
- final ObjectDeletedException odex = new ObjectDeletedException("msg", "id", TestBean.class.getName());
- try {
- hibernateTemplate.execute(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) {
- throw odex;
- }
- });
- fail("Should have thrown InvalidDataAccessApiUsageException");
- }
- catch (InvalidDataAccessApiUsageException ex) {
- // expected
- assertEquals(odex, ex.getCause());
- }
-
- final QueryException qex = new QueryException("msg", "query");
- try {
- hibernateTemplate.execute(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) {
- throw qex;
- }
- });
- fail("Should have thrown InvalidDataAccessResourceUsageException");
- }
- catch (HibernateQueryException ex) {
- // expected
- assertEquals(qex, ex.getCause());
- assertEquals("query", ex.getQueryString());
- }
-
- final UnresolvableObjectException uoex = new UnresolvableObjectException("id", TestBean.class.getName());
- try {
- hibernateTemplate.execute(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) {
- throw uoex;
- }
- });
- fail("Should have thrown HibernateObjectRetrievalFailureException");
- }
- catch (HibernateObjectRetrievalFailureException ex) {
- // expected
- assertEquals(TestBean.class.getName(), ex.getPersistentClassName());
- assertEquals("id", ex.getIdentifier());
- assertEquals(uoex, ex.getCause());
- }
-
- final ObjectNotFoundException onfe = new ObjectNotFoundException("id", TestBean.class.getName());
- try {
- hibernateTemplate.execute(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) {
- throw onfe;
- }
- });
- fail("Should have thrown HibernateObjectRetrievalFailureException");
- }
- catch (HibernateObjectRetrievalFailureException ex) {
- // expected
- assertEquals(TestBean.class.getName(), ex.getPersistentClassName());
- assertEquals("id", ex.getIdentifier());
- assertEquals(onfe, ex.getCause());
- }
-
- final WrongClassException wcex = new WrongClassException("msg", "id", TestBean.class.getName());
- try {
- hibernateTemplate.execute(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) {
- throw wcex;
- }
- });
- fail("Should have thrown HibernateObjectRetrievalFailureException");
- }
- catch (HibernateObjectRetrievalFailureException ex) {
- // expected
- assertEquals(TestBean.class.getName(), ex.getPersistentClassName());
- assertEquals("id", ex.getIdentifier());
- assertEquals(wcex, ex.getCause());
- }
-
- final NonUniqueResultException nuex = new NonUniqueResultException(2);
- try {
- hibernateTemplate.execute(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) {
- throw nuex;
- }
- });
- fail("Should have thrown IncorrectResultSizeDataAccessException");
- }
- catch (IncorrectResultSizeDataAccessException ex) {
- // expected
- assertEquals(1, ex.getExpectedSize());
- assertEquals(-1, ex.getActualSize());
- }
-
- final StaleObjectStateException sosex = new StaleObjectStateException(TestBean.class.getName(), "id");
- try {
- hibernateTemplate.execute(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) {
- throw sosex;
- }
- });
- fail("Should have thrown HibernateOptimisticLockingFailureException");
- }
- catch (HibernateOptimisticLockingFailureException ex) {
- // expected
- assertEquals(TestBean.class.getName(), ex.getPersistentClassName());
- assertEquals("id", ex.getIdentifier());
- assertEquals(sosex, ex.getCause());
- }
-
- final StaleStateException ssex = new StaleStateException("msg");
- try {
- hibernateTemplate.execute(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) {
- throw ssex;
- }
- });
- fail("Should have thrown HibernateOptimisticLockingFailureException");
- }
- catch (HibernateOptimisticLockingFailureException ex) {
- // expected
- assertNull(ex.getPersistentClassName());
- assertNull(ex.getIdentifier());
- assertEquals(ssex, ex.getCause());
- }
-
- final HibernateException hex = new HibernateException("msg");
- try {
- hibernateTemplate.execute(new HibernateCallback() {
- @Override
- public Object doInHibernate(Session session) {
- throw hex;
- }
- });
- fail("Should have thrown HibernateSystemException");
- }
- catch (HibernateSystemException ex) {
- // expected
- assertEquals(hex, ex.getCause());
- }
- }
-
-}
diff --git a/spring-orm-hibernate4/src/test/java/org/springframework/orm/hibernate4/HibernateTransactionManagerTests.java b/spring-orm-hibernate4/src/test/java/org/springframework/orm/hibernate4/HibernateTransactionManagerTests.java
deleted file mode 100644
index 9192ca9d7e..0000000000
--- a/spring-orm-hibernate4/src/test/java/org/springframework/orm/hibernate4/HibernateTransactionManagerTests.java
+++ /dev/null
@@ -1,1331 +0,0 @@
-/*
- * Copyright 2002-2014 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate4;
-
-import java.sql.Connection;
-import java.sql.DatabaseMetaData;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Savepoint;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Properties;
-import javax.sql.DataSource;
-import javax.transaction.TransactionManager;
-import javax.transaction.TransactionSynchronizationRegistry;
-import javax.transaction.UserTransaction;
-
-import org.hibernate.FlushMode;
-import org.hibernate.Interceptor;
-import org.hibernate.Query;
-import org.hibernate.Session;
-import org.hibernate.SessionBuilder;
-import org.hibernate.SessionFactory;
-import org.hibernate.Transaction;
-import org.hibernate.cfg.AvailableSettings;
-import org.hibernate.dialect.HSQLDialect;
-import org.hibernate.engine.spi.SessionFactoryImplementor;
-import org.hibernate.engine.spi.SessionImplementor;
-import org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory;
-import org.hibernate.exception.ConstraintViolationException;
-import org.junit.After;
-import org.junit.Test;
-import org.mockito.InOrder;
-
-import org.springframework.beans.factory.BeanFactory;
-import org.springframework.dao.DataIntegrityViolationException;
-import org.springframework.jdbc.datasource.ConnectionHolder;
-import org.springframework.jdbc.datasource.DriverManagerDataSource;
-import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy;
-import org.springframework.transaction.CannotCreateTransactionException;
-import org.springframework.transaction.PlatformTransactionManager;
-import org.springframework.transaction.TransactionDefinition;
-import org.springframework.transaction.TransactionStatus;
-import org.springframework.transaction.UnexpectedRollbackException;
-import org.springframework.transaction.jta.JtaTransactionManager;
-import org.springframework.transaction.support.TransactionCallback;
-import org.springframework.transaction.support.TransactionCallbackWithoutResult;
-import org.springframework.transaction.support.TransactionSynchronizationManager;
-import org.springframework.transaction.support.TransactionTemplate;
-
-import static org.junit.Assert.*;
-import static org.mockito.BDDMockito.*;
-
-/**
- * @author Juergen Hoeller
- * @since 3.2
- */
-@SuppressWarnings({"rawtypes", "unchecked"})
-public class HibernateTransactionManagerTests {
-
- @After
- public void tearDown() {
- assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
- assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
- assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
- assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
- }
-
- @Test
- public void testTransactionCommit() throws Exception {
- final DataSource ds = mock(DataSource.class);
- Connection con = mock(Connection.class);
- final SessionFactory sf = mock(SessionFactory.class);
- final ImplementingSession session = mock(ImplementingSession.class);
- Transaction tx = mock(Transaction.class);
- Query query = mock(Query.class);
-
- final List list = new ArrayList();
- list.add("test");
- given(con.getTransactionIsolation()).willReturn(Connection.TRANSACTION_READ_COMMITTED);
- given(sf.openSession()).willReturn(session);
- given(session.getTransaction()).willReturn(tx);
- given(session.connection()).willReturn(con);
- given(session.isOpen()).willReturn(true);
- given(session.createQuery("some query string")).willReturn(query);
- given(query.list()).willReturn(list);
- given(session.isConnected()).willReturn(true);
-
- LocalSessionFactoryBean lsfb = new LocalSessionFactoryBean() {
- @Override
- protected SessionFactory buildSessionFactory(LocalSessionFactoryBuilder sfb) {
- return sf;
- }
- };
- lsfb.afterPropertiesSet();
- final SessionFactory sfProxy = lsfb.getObject();
-
- HibernateTransactionManager tm = new HibernateTransactionManager();
- tm.setSessionFactory(sfProxy);
- tm.setDataSource(ds);
- TransactionTemplate tt = new TransactionTemplate(tm);
- tt.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
- tt.setTimeout(10);
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sfProxy));
- assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
- assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
-
- Object result = tt.execute(new TransactionCallback() {
- @Override
- public Object doInTransaction(TransactionStatus status) {
- assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sfProxy));
- assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(ds));
- assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
- assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
- Session session = ((SessionHolder) TransactionSynchronizationManager.getResource(sfProxy)).getSession();
- return session.createQuery("some query string").list();
- }
- });
- assertTrue("Correct result list", result == list);
-
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sfProxy));
- assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
- assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
-
- verify(con).setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
- verify(con).setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
- verify(tx).setTimeout(10);
- verify(tx).begin();
- verify(tx).commit();
- verify(session).close();
- }
-
- @Test
- public void testTransactionRollback() throws Exception {
- Connection con = mock(Connection.class);
- final SessionFactory sf = mock(SessionFactory.class);
- ImplementingSession session = mock(ImplementingSession.class);
- Transaction tx = mock(Transaction.class);
-
- given(sf.openSession()).willReturn(session);
- given(session.beginTransaction()).willReturn(tx);
- given(session.isOpen()).willReturn(true);
- given(session.isConnected()).willReturn(true);
- given(session.connection()).willReturn(con);
-
- PlatformTransactionManager tm = new HibernateTransactionManager(sf);
- TransactionTemplate tt = new TransactionTemplate(tm);
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
- assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
-
- try {
- tt.execute(new TransactionCallback() {
- @Override
- public Object doInTransaction(TransactionStatus status) {
- assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
- throw new RuntimeException("application exception");
- }
- });
- fail("Should have thrown RuntimeException");
- }
- catch (RuntimeException ex) {
- // expected
- }
-
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
- assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
- verify(session).close();
- verify(tx).rollback();
- }
-
- @Test
- public void testTransactionRollbackOnly() throws Exception {
- Connection con = mock(Connection.class);
- final SessionFactory sf = mock(SessionFactory.class);
- ImplementingSession session = mock(ImplementingSession.class);
- Transaction tx = mock(Transaction.class);
-
- given(sf.openSession()).willReturn(session);
- given(session.beginTransaction()).willReturn(tx);
- given(session.isOpen()).willReturn(true);
- given(session.getFlushMode()).willReturn(FlushMode.AUTO);
- given(session.isConnected()).willReturn(true);
- given(session.connection()).willReturn(con);
-
- PlatformTransactionManager tm = new HibernateTransactionManager(sf);
- TransactionTemplate tt = new TransactionTemplate(tm);
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
-
- tt.execute(new TransactionCallback() {
- @Override
- public Object doInTransaction(TransactionStatus status) {
- assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
- Session session = ((SessionHolder) TransactionSynchronizationManager.getResource(sf)).getSession();
- session.flush();
- status.setRollbackOnly();
- return null;
- }
- });
-
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
- verify(session).flush();
- verify(session).close();
- verify(tx).rollback();
- }
-
- @Test
- public void testParticipatingTransactionWithCommit() throws Exception {
- Connection con = mock(Connection.class);
- final SessionFactory sf = mock(SessionFactory.class);
- final ImplementingSession session = mock(ImplementingSession.class);
- Transaction tx = mock(Transaction.class);
-
- given(sf.openSession()).willReturn(session);
- given(session.beginTransaction()).willReturn(tx);
- given(session.isOpen()).willReturn(true);
- given(session.getFlushMode()).willReturn(FlushMode.AUTO);
- given(session.isConnected()).willReturn(true);
- given(session.connection()).willReturn(con);
-
- LocalSessionFactoryBean lsfb = new LocalSessionFactoryBean() {
- @Override
- protected SessionFactory buildSessionFactory(LocalSessionFactoryBuilder sfb) {
- return sf;
- }
- };
- lsfb.afterPropertiesSet();
- final SessionFactory sfProxy = lsfb.getObject();
-
- PlatformTransactionManager tm = new HibernateTransactionManager(sfProxy);
- final TransactionTemplate tt = new TransactionTemplate(tm);
- final List l = new ArrayList();
- l.add("test");
-
- Object result = tt.execute(new TransactionCallback() {
- @Override
- public Object doInTransaction(TransactionStatus status) {
- return tt.execute(new TransactionCallback() {
- @Override
- public Object doInTransaction(TransactionStatus status) {
- Session session = ((SessionHolder) TransactionSynchronizationManager.getResource(sf)).getSession();
- session.flush();
- return l;
- }
- });
- }
- });
- assertTrue("Correct result list", result == l);
-
- verify(session).flush();
- verify(session).close();
- verify(tx).commit();
- }
-
- @Test
- public void testParticipatingTransactionWithRollback() throws Exception {
- Connection con = mock(Connection.class);
- final SessionFactory sf = mock(SessionFactory.class);
- ImplementingSession session = mock(ImplementingSession.class);
- Transaction tx = mock(Transaction.class);
-
- given(sf.openSession()).willReturn(session);
- given(session.beginTransaction()).willReturn(tx);
- given(session.isOpen()).willReturn(true);
- given(session.getFlushMode()).willReturn(FlushMode.AUTO);
- given(session.isConnected()).willReturn(true);
- given(session.connection()).willReturn(con);
-
- PlatformTransactionManager tm = new HibernateTransactionManager(sf);
- final TransactionTemplate tt = new TransactionTemplate(tm);
- try {
- tt.execute(new TransactionCallback() {
- @Override
- public Object doInTransaction(TransactionStatus status) {
- return tt.execute(new TransactionCallback() {
- @Override
- public Object doInTransaction(TransactionStatus status) {
- throw new RuntimeException("application exception");
- }
- });
- }
- });
- fail("Should have thrown RuntimeException");
- }
- catch (RuntimeException ex) {
- // expected
- }
-
- verify(session).close();
- verify(tx).rollback();
- }
-
- @Test
- public void testParticipatingTransactionWithRollbackOnly() throws Exception {
- Connection con = mock(Connection.class);
- final SessionFactory sf = mock(SessionFactory.class);
- ImplementingSession session = mock(ImplementingSession.class);
- Transaction tx = mock(Transaction.class);
-
- given(sf.openSession()).willReturn(session);
- given(session.beginTransaction()).willReturn(tx);
- given(session.isOpen()).willReturn(true);
- given(session.isConnected()).willReturn(true);
- given(session.connection()).willReturn(con);
-
- PlatformTransactionManager tm = new HibernateTransactionManager(sf);
- final TransactionTemplate tt = new TransactionTemplate(tm);
- final List l = new ArrayList();
- l.add("test");
-
- try {
- tt.execute(new TransactionCallback() {
- @Override
- public Object doInTransaction(TransactionStatus status) {
- return tt.execute(new TransactionCallback() {
- @Override
- public Object doInTransaction(TransactionStatus status) {
- status.setRollbackOnly();
- return null;
- }
- });
- }
- });
- fail("Should have thrown UnexpectedRollbackException");
- }
- catch (UnexpectedRollbackException ex) {
- // expected
- }
-
- verify(session).close();
- verify(tx).rollback();
- }
-
- @Test
- public void testParticipatingTransactionWithRequiresNew() throws Exception {
- final SessionFactory sf = mock(SessionFactory.class);
- ImplementingSession session1 = mock(ImplementingSession.class);
- ImplementingSession session2 = mock(ImplementingSession.class);
- Connection con = mock(Connection.class);
- Transaction tx = mock(Transaction.class);
-
- given(sf.openSession()).willReturn(session1, session2);
- given(session1.beginTransaction()).willReturn(tx);
- given(session1.isOpen()).willReturn(true);
- given(session2.beginTransaction()).willReturn(tx);
- given(session2.isOpen()).willReturn(true);
- given(session2.getFlushMode()).willReturn(FlushMode.AUTO);
- given(session1.isConnected()).willReturn(true);
- given(session1.connection()).willReturn(con);
- given(session2.isConnected()).willReturn(true);
- given(session2.connection()).willReturn(con);
-
- PlatformTransactionManager tm = new HibernateTransactionManager(sf);
- final TransactionTemplate tt = new TransactionTemplate(tm);
- tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
-
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
- tt.execute(new TransactionCallback() {
- @Override
- public Object doInTransaction(TransactionStatus status) {
- final SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sf);
- assertTrue("Has thread session", holder != null);
- assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
- assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
- tt.execute(new TransactionCallback() {
- @Override
- public Object doInTransaction(TransactionStatus status) {
- Session session = ((SessionHolder) TransactionSynchronizationManager.getResource(sf)).getSession();
- assertTrue("Not enclosing session", session != holder.getSession());
- session.flush();
- assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
- assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
- return null;
- }
- });
- assertTrue("Same thread session as before",
- holder.getSession() == ((SessionHolder) TransactionSynchronizationManager.getResource(sf)).getSession());
- assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
- assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
- return null;
- }
- });
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
-
- verify(session2).flush();
- verify(session1).close();
- verify(session2).close();
- verify(tx, times(2)).commit();
- }
-
- @Test
- public void testParticipatingTransactionWithNotSupported() throws Exception {
- final SessionFactory sf = mock(SessionFactory.class);
- ImplementingSession session = mock(ImplementingSession.class);
- Connection con = mock(Connection.class);
- Transaction tx = mock(Transaction.class);
-
- given(sf.openSession()).willReturn(session);
- given(session.getSessionFactory()).willReturn(sf);
- given(session.beginTransaction()).willReturn(tx);
- given(session.isOpen()).willReturn(true);
- given(session.getFlushMode()).willReturn(FlushMode.AUTO);
- given(session.isConnected()).willReturn(true);
- given(session.connection()).willReturn(con);
-
- HibernateTransactionManager tm = new HibernateTransactionManager(sf);
- final TransactionTemplate tt = new TransactionTemplate(tm);
- tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
-
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
- tt.execute(new TransactionCallback() {
- @Override
- public Object doInTransaction(TransactionStatus status) {
- SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sf);
- assertTrue("Has thread session", holder != null);
- assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
- assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
- tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
- tt.execute(new TransactionCallback() {
- @Override
- public Object doInTransaction(TransactionStatus status) {
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
- assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
- assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
- return null;
- }
- });
- assertTrue("Same thread session as before",
- holder.getSession() == ((SessionHolder) TransactionSynchronizationManager.getResource(sf)).getSession());
- assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
- assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
- return null;
- }
- });
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
-
- verify(session).close();
- verify(tx).commit();
- }
-
- @Test
- public void testTransactionWithPropagationSupports() throws Exception {
- final SessionFactory sf = mock(SessionFactory.class);
- final Session session = mock(Session.class);
-
- given(sf.openSession()).willReturn(session);
- given(session.getSessionFactory()).willReturn(sf);
- given(session.getFlushMode()).willReturn(FlushMode.MANUAL);
-
- LocalSessionFactoryBean lsfb = new LocalSessionFactoryBean() {
- @Override
- protected SessionFactory buildSessionFactory(LocalSessionFactoryBuilder sfb) {
- return sf;
- }
- };
- lsfb.afterPropertiesSet();
- final SessionFactory sfProxy = lsfb.getObject();
-
- PlatformTransactionManager tm = new HibernateTransactionManager(sfProxy);
- TransactionTemplate tt = new TransactionTemplate(tm);
- tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sfProxy));
-
- tt.execute(new TransactionCallback() {
- @Override
- public Object doInTransaction(TransactionStatus status) {
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sfProxy));
- assertTrue("Is not new transaction", !status.isNewTransaction());
- assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
- assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
- Session session = sf.openSession();
- session.flush();
- session.close();
- return null;
- }
- });
-
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sfProxy));
- InOrder ordered = inOrder(session);
- ordered.verify(session).flush();
- ordered.verify(session).close();
- }
-
- @Test
- public void testTransactionWithPropagationSupportsAndCurrentSession() throws Exception {
- final SessionFactoryImplementor sf = mock(SessionFactoryImplementor.class);
- final Session session = mock(Session.class);
-
- given(sf.openSession()).willReturn(session);
- given(session.getSessionFactory()).willReturn(sf);
- given(session.getFlushMode()).willReturn(FlushMode.MANUAL);
-
- LocalSessionFactoryBean lsfb = new LocalSessionFactoryBean() {
- @Override
- protected SessionFactory buildSessionFactory(LocalSessionFactoryBuilder sfb) {
- return sf;
- }
- };
- lsfb.afterPropertiesSet();
- final SessionFactory sfProxy = lsfb.getObject();
-
- PlatformTransactionManager tm = new HibernateTransactionManager(sfProxy);
- TransactionTemplate tt = new TransactionTemplate(tm);
- tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sfProxy));
-
- tt.execute(new TransactionCallback() {
- @Override
- public Object doInTransaction(TransactionStatus status) {
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sfProxy));
- assertTrue("Is not new transaction", !status.isNewTransaction());
- assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
- assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
- Session session = new SpringSessionContext(sf).currentSession();
- assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sfProxy));
- session.flush();
- return null;
- }
- });
-
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sfProxy));
- InOrder ordered = inOrder(session);
- ordered.verify(session).flush();
- ordered.verify(session).close();
- }
-
- @Test
- public void testTransactionWithPropagationSupportsAndInnerTransaction() throws Exception {
- final SessionFactory sf = mock(SessionFactory.class);
- final ImplementingSession session1 = mock(ImplementingSession.class);
- final ImplementingSession session2 = mock(ImplementingSession.class);
- Connection con = mock(Connection.class);
- Transaction tx = mock(Transaction.class);
-
- given(sf.openSession()).willReturn(session1, session2);
- given(session1.getSessionFactory()).willReturn(sf);
- given(session1.getFlushMode()).willReturn(FlushMode.AUTO);
- given(session2.beginTransaction()).willReturn(tx);
- given(session2.connection()).willReturn(con);
- given(session2.getFlushMode()).willReturn(FlushMode.AUTO);
- given(session2.isOpen()).willReturn(true);
- given(session2.isConnected()).willReturn(true);
-
- LocalSessionFactoryBean lsfb = new LocalSessionFactoryBean() {
- @Override
- protected SessionFactory buildSessionFactory(LocalSessionFactoryBuilder sfb) {
- return sf;
- }
- };
- lsfb.afterPropertiesSet();
- final SessionFactory sfProxy = lsfb.getObject();
-
- PlatformTransactionManager tm = new HibernateTransactionManager(sfProxy);
- TransactionTemplate tt = new TransactionTemplate(tm);
- tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
- final TransactionTemplate tt2 = new TransactionTemplate(tm);
- tt2.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
-
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
- tt.execute(new TransactionCallback() {
- @Override
- public Object doInTransaction(TransactionStatus status) {
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sfProxy));
- assertTrue("Is not new transaction", !status.isNewTransaction());
- assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
- assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
- Session session = sfProxy.openSession();
- assertSame(session1, session);
- tt2.execute(new TransactionCallback() {
- @Override
- public Object doInTransaction(TransactionStatus status) {
- assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
- assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
- Session session = ((SessionHolder) TransactionSynchronizationManager.getResource(sf)).getSession();
- assertSame(session2, session);
- session.flush();
- return null;
- }
- });
- session.flush();
- session.close();
- assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
- assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
- return null;
- }
- });
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
-
- verify(session1).flush();
- verify(session1).close();
- verify(session2).flush();
- verify(session2).close();
- verify(tx).commit();
- }
-
- @Test
- public void testTransactionCommitWithEntityInterceptor() throws Exception {
- Interceptor entityInterceptor = mock(Interceptor.class);
- Connection con = mock(Connection.class);
- final SessionFactory sf = mock(SessionFactory.class);
- ImplementingSession session = mock(ImplementingSession.class);
- SessionBuilder options = mock(SessionBuilder.class);
- Transaction tx = mock(Transaction.class);
-
- given(sf.withOptions()).willReturn(options);
- given(options.interceptor(entityInterceptor)).willReturn(options);
- given(options.openSession()).willReturn(session);
- given(session.beginTransaction()).willReturn(tx);
- given(session.isOpen()).willReturn(true);
- given(session.isConnected()).willReturn(true);
- given(session.connection()).willReturn(con);
-
- HibernateTransactionManager tm = new HibernateTransactionManager(sf);
- tm.setEntityInterceptor(entityInterceptor);
- tm.setAllowResultAccessAfterCompletion(true);
- TransactionTemplate tt = new TransactionTemplate(tm);
- tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
- assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
-
- tt.execute(new TransactionCallbackWithoutResult() {
- @Override
- public void doInTransactionWithoutResult(TransactionStatus status) {
- assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
- }
- });
-
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
- assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
-
- verify(session).close();
- verify(tx).commit();
- }
-
- @Test
- public void testTransactionCommitWithEntityInterceptorBeanName() throws Exception {
- Interceptor entityInterceptor = mock(Interceptor.class);
- Interceptor entityInterceptor2 = mock(Interceptor.class);
- Connection con = mock(Connection.class);
- final SessionFactory sf = mock(SessionFactory.class);
- ImplementingSession session = mock(ImplementingSession.class);
- SessionBuilder options = mock(SessionBuilder.class);
- Transaction tx = mock(Transaction.class);
-
- given(sf.withOptions()).willReturn(options);
- given(options.interceptor(entityInterceptor)).willReturn(options);
- given(options.interceptor(entityInterceptor2)).willReturn(options);
- given(options.openSession()).willReturn(session);
- given(session.beginTransaction()).willReturn(tx);
- given(session.isOpen()).willReturn(true);
- given(session.isConnected()).willReturn(true);
- given(session.connection()).willReturn(con);
-
- BeanFactory beanFactory = mock(BeanFactory.class);
- given(beanFactory.getBean("entityInterceptor", Interceptor.class)).willReturn(
- entityInterceptor, entityInterceptor2);
-
- HibernateTransactionManager tm = new HibernateTransactionManager(sf);
- tm.setEntityInterceptorBeanName("entityInterceptor");
- tm.setBeanFactory(beanFactory);
-
- TransactionTemplate tt = new TransactionTemplate(tm);
- tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
- assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
-
- for (int i = 0; i < 2; i++) {
- tt.execute(new TransactionCallbackWithoutResult() {
- @Override
- public void doInTransactionWithoutResult(TransactionStatus status) {
- assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
- }
- });
- }
-
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
- assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
-
- verify(session, times(2)).close();
- verify(tx, times(2)).commit();
- }
-
- @Test
- public void testTransactionCommitWithReadOnly() throws Exception {
- Connection con = mock(Connection.class);
- final SessionFactory sf = mock(SessionFactory.class);
- ImplementingSession session = mock(ImplementingSession.class);
- Transaction tx = mock(Transaction.class);
- Query query = mock(Query.class);
-
- final List list = new ArrayList();
- list.add("test");
- given(sf.openSession()).willReturn(session);
- given(session.beginTransaction()).willReturn(tx);
- given(session.connection()).willReturn(con);
- given(session.isOpen()).willReturn(true);
- given(session.createQuery("some query string")).willReturn(query);
- given(query.list()).willReturn(list);
- given(session.isConnected()).willReturn(true);
- given(con.isReadOnly()).willReturn(true);
-
- HibernateTransactionManager tm = new HibernateTransactionManager(sf);
- TransactionTemplate tt = new TransactionTemplate(tm);
- tt.setReadOnly(true);
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
- assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
-
- Object result = tt.execute(new TransactionCallback() {
- @Override
- public Object doInTransaction(TransactionStatus status) {
- assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
- assertTrue(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
- assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
- Session session = ((SessionHolder) TransactionSynchronizationManager.getResource(sf)).getSession();
- return session.createQuery("some query string").list();
- }
- });
- assertTrue("Correct result list", result == list);
-
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
- assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
-
- verify(session).setFlushMode(FlushMode.MANUAL);
- verify(con).setReadOnly(true);
- verify(tx).commit();
- verify(con).setReadOnly(false);
- verify(session).close();
- }
-
- @Test
- public void testTransactionCommitWithFlushFailure() throws Exception {
- Connection con = mock(Connection.class);
- final SessionFactory sf = mock(SessionFactory.class);
- ImplementingSession session = mock(ImplementingSession.class);
- Transaction tx = mock(Transaction.class);
-
- given(sf.openSession()).willReturn(session);
- given(session.beginTransaction()).willReturn(tx);
- given(session.isOpen()).willReturn(true);
- SQLException sqlEx = new SQLException("argh", "27");
- Exception rootCause = null;
- ConstraintViolationException jdbcEx = new ConstraintViolationException("mymsg", sqlEx, null);
- rootCause = jdbcEx;
- willThrow(jdbcEx).given(tx).commit();
- given(session.isConnected()).willReturn(true);
- given(session.connection()).willReturn(con);
-
- HibernateTransactionManager tm = new HibernateTransactionManager(sf);
- TransactionTemplate tt = new TransactionTemplate(tm);
- final List l = new ArrayList();
- l.add("test");
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
- assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
-
- try {
- tt.execute(new TransactionCallback() {
- @Override
- public Object doInTransaction(TransactionStatus status) {
- assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
- return l;
- }
- });
- fail("Should have thrown DataIntegrityViolationException");
- }
- catch (DataIntegrityViolationException ex) {
- // expected
- assertEquals(rootCause, ex.getCause());
- assertTrue(ex.getMessage().contains("mymsg"));
- }
-
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
- assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
-
- verify(session).close();
- verify(tx).rollback();
- }
-
- @Test
- public void testTransactionCommitWithPreBound() throws Exception {
- final DataSource ds = mock(DataSource.class);
- Connection con = mock(Connection.class);
- final SessionFactory sf = mock(SessionFactory.class);
- final ImplementingSession session = mock(ImplementingSession.class);
- Transaction tx = mock(Transaction.class);
-
- given(session.beginTransaction()).willReturn(tx);
- given(session.isOpen()).willReturn(true);
- given(session.getFlushMode()).willReturn(FlushMode.MANUAL);
- given(session.connection()).willReturn(con);
- given(con.getTransactionIsolation()).willReturn(Connection.TRANSACTION_READ_COMMITTED);
- given(session.isConnected()).willReturn(true);
-
- HibernateTransactionManager tm = new HibernateTransactionManager();
- tm.setSessionFactory(sf);
- tm.setDataSource(ds);
- TransactionTemplate tt = new TransactionTemplate(tm);
- tt.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
- final List l = new ArrayList();
- l.add("test");
- assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
- assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
- TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
- assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
-
- Object result = tt.execute(new TransactionCallback() {
- @Override
- public Object doInTransaction(TransactionStatus status) {
- assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
- assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(ds));
- SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sf);
- assertTrue("Has thread transaction", sessionHolder.getTransaction() != null);
- Session sess = ((SessionHolder) TransactionSynchronizationManager.getResource(sf)).getSession();
- assertEquals(session, sess);
- return l;
- }
- });
- assertTrue("Correct result list", result == l);
-
- assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
- SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sf);
- assertTrue("Hasn't thread transaction", sessionHolder.getTransaction() == null);
- TransactionSynchronizationManager.unbindResource(sf);
- assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
- assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
-
- InOrder ordered = inOrder(session, con);
- ordered.verify(con).setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
- ordered.verify(session).setFlushMode(FlushMode.AUTO);
- ordered.verify(con).setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
- ordered.verify(session).setFlushMode(FlushMode.MANUAL);
- verify(tx).commit();
- verify(session).disconnect();
- }
-
- @Test
- public void testTransactionCommitWithPreBoundAndResultAccessAfterCommit() throws Exception {
- final DataSource ds = mock(DataSource.class);
- Connection con = mock(Connection.class);
- final SessionFactory sf = mock(SessionFactory.class);
- final ImplementingSession session = mock(ImplementingSession.class);
- Transaction tx = mock(Transaction.class);
-
- given(session.beginTransaction()).willReturn(tx);
- given(session.isOpen()).willReturn(true);
- given(session.getFlushMode()).willReturn(FlushMode.MANUAL);
- given(session.connection()).willReturn(con);
- given(con.getTransactionIsolation()).willReturn(Connection.TRANSACTION_READ_COMMITTED);
- given(con.getHoldability()).willReturn(ResultSet.CLOSE_CURSORS_AT_COMMIT);
- given(session.isConnected()).willReturn(true);
-
- HibernateTransactionManager tm = new HibernateTransactionManager();
- tm.setSessionFactory(sf);
- tm.setDataSource(ds);
- tm.setAllowResultAccessAfterCompletion(true);
- TransactionTemplate tt = new TransactionTemplate(tm);
- tt.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
- final List l = new ArrayList();
- l.add("test");
- assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
- assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
- TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
- assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
-
- Object result = tt.execute(new TransactionCallback() {
- @Override
- public Object doInTransaction(TransactionStatus status) {
- assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
- assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(ds));
- SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sf);
- assertTrue("Has thread transaction", sessionHolder.getTransaction() != null);
- Session sess = ((SessionHolder) TransactionSynchronizationManager.getResource(sf)).getSession();
- assertEquals(session, sess);
- return l;
- }
- });
- assertTrue("Correct result list", result == l);
-
- assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
- SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sf);
- assertTrue("Hasn't thread transaction", sessionHolder.getTransaction() == null);
- TransactionSynchronizationManager.unbindResource(sf);
- assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
- assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
-
- InOrder ordered = inOrder(session, con);
- ordered.verify(con).setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
- ordered.verify(con).setHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT);
- ordered.verify(session).setFlushMode(FlushMode.AUTO);
- ordered.verify(con).setHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT);
- ordered.verify(con).setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
- ordered.verify(session).setFlushMode(FlushMode.MANUAL);
- verify(tx).commit();
- }
-
- @Test
- public void testTransactionRollbackWithPreBound() throws Exception {
- final DataSource ds = mock(DataSource.class);
- Connection con = mock(Connection.class);
- final SessionFactory sf = mock(SessionFactory.class);
- final ImplementingSession session = mock(ImplementingSession.class);
- final Transaction tx1 = mock(Transaction.class);
- final Transaction tx2 = mock(Transaction.class);
-
- given(session.beginTransaction()).willReturn(tx1, tx2);
- given(session.isOpen()).willReturn(true);
- given(session.getFlushMode()).willReturn(FlushMode.MANUAL);
- given(session.isConnected()).willReturn(true);
- given(session.connection()).willReturn(con);
-
- HibernateTransactionManager tm = new HibernateTransactionManager();
- tm.setSessionFactory(sf);
- tm.setDataSource(ds);
- final TransactionTemplate tt = new TransactionTemplate(tm);
- assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
- assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
- TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
- assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
-
- try {
- tt.execute(new TransactionCallbackWithoutResult() {
- @Override
- public void doInTransactionWithoutResult(TransactionStatus status) {
- assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
- assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(ds));
- SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sf);
- assertEquals(tx1, sessionHolder.getTransaction());
- tt.execute(new TransactionCallbackWithoutResult() {
- @Override
- public void doInTransactionWithoutResult(TransactionStatus status) {
- status.setRollbackOnly();
- Session sess = ((SessionHolder) TransactionSynchronizationManager.getResource(sf)).getSession();
- assertEquals(session, sess);
- }
- });
- }
- });
- fail("Should have thrown UnexpectedRollbackException");
- }
- catch (UnexpectedRollbackException ex) {
- // expected
- }
-
- assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
- SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sf);
- assertTrue("Hasn't thread transaction", sessionHolder.getTransaction() == null);
- assertTrue("Not marked rollback-only", !sessionHolder.isRollbackOnly());
-
- tt.execute(new TransactionCallbackWithoutResult() {
- @Override
- public void doInTransactionWithoutResult(TransactionStatus status) {
- assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
- assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(ds));
- SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sf);
- assertEquals(tx2, sessionHolder.getTransaction());
- Session sess = ((SessionHolder) TransactionSynchronizationManager.getResource(sf)).getSession();
- assertEquals(session, sess);
- }
- });
-
- assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
- assertTrue("Hasn't thread transaction", sessionHolder.getTransaction() == null);
- TransactionSynchronizationManager.unbindResource(sf);
- assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
- assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
-
- verify(tx1).rollback();
- verify(tx2).commit();
- InOrder ordered = inOrder(session);
- ordered.verify(session).clear();
- ordered.verify(session).setFlushMode(FlushMode.AUTO);
- ordered.verify(session).setFlushMode(FlushMode.MANUAL);
- ordered.verify(session).disconnect();
- }
-
- @Test
- public void testTransactionRollbackWithHibernateManagedSession() throws Exception {
- final SessionFactory sf = mock(SessionFactory.class);
- final Session session = mock(Session.class);
- final Transaction tx1 = mock(Transaction.class);
- final Transaction tx2 = mock(Transaction.class);
-
- given(sf.getCurrentSession()).willReturn(session);
- given(session.isOpen()).willReturn(true);
- given(session.getTransaction()).willReturn(tx1, tx2);
- given(session.beginTransaction()).willReturn(tx1, tx2);
- given(session.getFlushMode()).willReturn(FlushMode.MANUAL);
-
- HibernateTransactionManager tm = new HibernateTransactionManager();
- tm.setSessionFactory(sf);
- tm.setPrepareConnection(false);
- tm.setHibernateManagedSession(true);
- final TransactionTemplate tt = new TransactionTemplate(tm);
-
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
-
- try {
- tt.execute(new TransactionCallbackWithoutResult() {
- @Override
- public void doInTransactionWithoutResult(TransactionStatus status) {
- assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
- tt.execute(new TransactionCallbackWithoutResult() {
- @Override
- public void doInTransactionWithoutResult(TransactionStatus status) {
- status.setRollbackOnly();
- Session sess = ((SessionHolder) TransactionSynchronizationManager.getResource(sf)).getSession();
- assertEquals(session, sess);
- }
- });
- }
- });
- fail("Should have thrown UnexpectedRollbackException");
- }
- catch (UnexpectedRollbackException ex) {
- // expected
- }
-
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
-
- tt.execute(new TransactionCallbackWithoutResult() {
- @Override
- public void doInTransactionWithoutResult(TransactionStatus status) {
- assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
- Session sess = ((SessionHolder) TransactionSynchronizationManager.getResource(sf)).getSession();
- assertEquals(session, sess);
- }
- });
-
- verify(tx1).rollback();
- verify(tx2).commit();
- InOrder ordered = inOrder(session);
- ordered.verify(session).setFlushMode(FlushMode.AUTO);
- ordered.verify(session).setFlushMode(FlushMode.MANUAL);
- }
-
- @Test
- public void testExistingTransactionWithPropagationNestedAndRollback() throws Exception {
- doTestExistingTransactionWithPropagationNestedAndRollback(false);
- }
-
- @Test
- public void testExistingTransactionWithManualSavepointAndRollback() throws Exception {
- doTestExistingTransactionWithPropagationNestedAndRollback(true);
- }
-
- private void doTestExistingTransactionWithPropagationNestedAndRollback(final boolean manualSavepoint)
- throws Exception {
-
- final DataSource ds = mock(DataSource.class);
- Connection con = mock(Connection.class);
- DatabaseMetaData md = mock(DatabaseMetaData.class);
- Savepoint sp = mock(Savepoint.class);
- final SessionFactory sf = mock(SessionFactory.class);
- ImplementingSession session = mock(ImplementingSession.class);
- Transaction tx = mock(Transaction.class);
- Query query = mock(Query.class);
-
- final List list = new ArrayList();
- list.add("test");
- given(sf.openSession()).willReturn(session);
- given(session.beginTransaction()).willReturn(tx);
- given(session.connection()).willReturn(con);
- given(session.isOpen()).willReturn(true);
- given(md.supportsSavepoints()).willReturn(true);
- given(con.getMetaData()).willReturn(md);
- given(con.setSavepoint(ConnectionHolder.SAVEPOINT_NAME_PREFIX + 1)).willReturn(sp);
- given(session.createQuery("some query string")).willReturn(query);
- given(query.list()).willReturn(list);
- given(session.isConnected()).willReturn(true);
-
- HibernateTransactionManager tm = new HibernateTransactionManager();
- tm.setNestedTransactionAllowed(true);
- tm.setSessionFactory(sf);
- tm.setDataSource(ds);
- final TransactionTemplate tt = new TransactionTemplate(tm);
- tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
- assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
- assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
-
- Object result = tt.execute(new TransactionCallback() {
- @Override
- public Object doInTransaction(TransactionStatus status) {
- assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
- assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(ds));
- if (manualSavepoint) {
- Object savepoint = status.createSavepoint();
- status.rollbackToSavepoint(savepoint);
- }
- else {
- tt.execute(new TransactionCallbackWithoutResult() {
- @Override
- protected void doInTransactionWithoutResult(TransactionStatus status) {
- assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
- assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(ds));
- status.setRollbackOnly();
- }
- });
- }
- Session session = ((SessionHolder) TransactionSynchronizationManager.getResource(sf)).getSession();
- return session.createQuery("some query string").list();
- }
- });
- assertTrue("Correct result list", result == list);
-
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
- assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
- assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
-
- verify(con).setSavepoint(ConnectionHolder.SAVEPOINT_NAME_PREFIX + 1);
- verify(con).rollback(sp);
- verify(session).close();
- verify(tx).commit();
- }
-
- @Test
- public void testTransactionCommitWithNonExistingDatabase() throws Exception {
- final DriverManagerDataSource ds = new DriverManagerDataSource();
- LocalSessionFactoryBean lsfb = new LocalSessionFactoryBean();
- lsfb.setDataSource(ds);
- Properties props = new Properties();
- props.setProperty("hibernate.dialect", HSQLDialect.class.getName());
- lsfb.setHibernateProperties(props);
- lsfb.afterPropertiesSet();
- final SessionFactory sf = lsfb.getObject();
-
- HibernateTransactionManager tm = new HibernateTransactionManager();
- tm.setSessionFactory(sf);
- tm.afterPropertiesSet();
- TransactionTemplate tt = new TransactionTemplate(tm);
- tt.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
- tt.setTimeout(10);
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
- assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
- assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
-
- try {
- tt.execute(new TransactionCallback() {
- @Override
- public Object doInTransaction(TransactionStatus status) {
- assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
- assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(ds));
- Session session = ((SessionHolder) TransactionSynchronizationManager.getResource(sf)).getSession();
- return session.createQuery("from java.lang.Object").list();
- }
- });
- fail("Should have thrown CannotCreateTransactionException");
- }
- catch (CannotCreateTransactionException ex) {
- // expected
- }
-
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
- assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
- assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
- }
-
- @Test
- public void testTransactionCommitWithPreBoundSessionAndNonExistingDatabase() throws Exception {
- final DriverManagerDataSource ds = new DriverManagerDataSource();
- LocalSessionFactoryBean lsfb = new LocalSessionFactoryBean();
- lsfb.setDataSource(ds);
- Properties props = new Properties();
- props.setProperty("hibernate.dialect", HSQLDialect.class.getName());
- lsfb.setHibernateProperties(props);
- lsfb.afterPropertiesSet();
- final SessionFactory sf = lsfb.getObject();
-
- HibernateTransactionManager tm = new HibernateTransactionManager();
- tm.setSessionFactory(sf);
- tm.afterPropertiesSet();
- TransactionTemplate tt = new TransactionTemplate(tm);
- tt.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
- tt.setTimeout(10);
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
- assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
- assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
-
- Session session = sf.openSession();
- TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
- try {
- tt.execute(new TransactionCallback() {
- @Override
- public Object doInTransaction(TransactionStatus status) {
- assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
- assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(ds));
- Session session = ((SessionHolder) TransactionSynchronizationManager.getResource(sf)).getSession();
- return session.createQuery("from java.lang.Object").list();
- }
- });
- fail("Should have thrown CannotCreateTransactionException");
- }
- catch (CannotCreateTransactionException ex) {
- // expected
- SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sf);
- assertFalse(holder.isSynchronizedWithTransaction());
- }
- finally {
- TransactionSynchronizationManager.unbindResource(sf);
- session.close();
- }
-
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
- assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
- assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
- }
-
- @Test
- public void testTransactionCommitWithNonExistingDatabaseAndLazyConnection() throws Exception {
- DriverManagerDataSource dsTarget = new DriverManagerDataSource();
- final LazyConnectionDataSourceProxy ds = new LazyConnectionDataSourceProxy();
- ds.setTargetDataSource(dsTarget);
- ds.setDefaultAutoCommit(true);
- ds.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
- //ds.setDefaultTransactionIsolationName("TRANSACTION_READ_COMMITTED");
-
- LocalSessionFactoryBean lsfb = new LocalSessionFactoryBean();
- lsfb.setDataSource(ds);
- Properties props = new Properties();
- props.setProperty("hibernate.dialect", HSQLDialect.class.getName());
- props.setProperty("hibernate.temp.use_jdbc_metadata_defaults", "false");
- lsfb.setHibernateProperties(props);
- lsfb.afterPropertiesSet();
- final SessionFactory sf = lsfb.getObject();
-
- HibernateTransactionManager tm = new HibernateTransactionManager();
- tm.setSessionFactory(sf);
- tm.afterPropertiesSet();
- TransactionTemplate tt = new TransactionTemplate(tm);
- tt.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
- tt.setTimeout(10);
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
- assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
- assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
-
- tt.execute(new TransactionCallback() {
- @Override
- public Object doInTransaction(TransactionStatus status) {
- assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
- assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(ds));
- Session session = ((SessionHolder) TransactionSynchronizationManager.getResource(sf)).getSession();
- return session.createQuery("from java.lang.Object").list();
- }
- });
-
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
- assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
- assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
- }
-
- @Test
- public void testTransactionFlush() throws Exception {
- final SessionFactory sf = mock(SessionFactory.class);
- final Session session = mock(Session.class);
- Transaction tx = mock(Transaction.class);
-
- given(sf.openSession()).willReturn(session);
- given(session.beginTransaction()).willReturn(tx);
-
- HibernateTransactionManager tm = new HibernateTransactionManager(sf);
- tm.setPrepareConnection(false);
- TransactionTemplate tt = new TransactionTemplate(tm);
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
- assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
-
- tt.execute(new TransactionCallbackWithoutResult() {
- @Override
- public void doInTransactionWithoutResult(TransactionStatus status) {
- assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
- assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
- assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
- status.flush();
- }
- });
-
- assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
- assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
-
- verify(session).flush();
- verify(tx).commit();
- verify(session).close();
- }
-
- @Test
- public void testSetJtaTransactionManager() throws Exception {
- DataSource ds = mock(DataSource.class);
- TransactionManager tm = mock(TransactionManager.class);
- UserTransaction ut = mock(UserTransaction.class);
- TransactionSynchronizationRegistry tsr = mock(TransactionSynchronizationRegistry.class);
- JtaTransactionManager jtm = new JtaTransactionManager();
- jtm.setTransactionManager(tm);
- jtm.setUserTransaction(ut);
- jtm.setTransactionSynchronizationRegistry(tsr);
- LocalSessionFactoryBuilder lsfb = new LocalSessionFactoryBuilder(ds);
- lsfb.setJtaTransactionManager(jtm);
- Object jtaPlatform = lsfb.getProperties().get(AvailableSettings.JTA_PLATFORM);
- assertNotNull(jtaPlatform);
- assertSame(tm, jtaPlatform.getClass().getMethod("retrieveTransactionManager").invoke(jtaPlatform));
- assertSame(ut, jtaPlatform.getClass().getMethod("retrieveUserTransaction").invoke(jtaPlatform));
- assertTrue(lsfb.getProperties().get(AvailableSettings.TRANSACTION_STRATEGY) instanceof CMTTransactionFactory);
- }
-
- @Test
- public void testSetTransactionManager() throws Exception {
- DataSource ds = mock(DataSource.class);
- TransactionManager tm = mock(TransactionManager.class);
- LocalSessionFactoryBuilder lsfb = new LocalSessionFactoryBuilder(ds);
- lsfb.setJtaTransactionManager(tm);
- Object jtaPlatform = lsfb.getProperties().get(AvailableSettings.JTA_PLATFORM);
- assertNotNull(jtaPlatform);
- assertSame(tm, jtaPlatform.getClass().getMethod("retrieveTransactionManager").invoke(jtaPlatform));
- assertTrue(lsfb.getProperties().get(AvailableSettings.TRANSACTION_STRATEGY) instanceof CMTTransactionFactory);
- }
-
-
- public interface ImplementingSession extends Session, SessionImplementor {
- }
-
-}
diff --git a/spring-orm-hibernate4/src/test/java/org/springframework/orm/jpa/jpa21/PersistenceContextTransactionTests.java b/spring-orm-hibernate4/src/test/java/org/springframework/orm/jpa/jpa21/PersistenceContextTransactionTests.java
deleted file mode 100644
index 1a93a4ea74..0000000000
--- a/spring-orm-hibernate4/src/test/java/org/springframework/orm/jpa/jpa21/PersistenceContextTransactionTests.java
+++ /dev/null
@@ -1,270 +0,0 @@
-/*
- * Copyright 2002-2015 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.jpa.jpa21;
-
-import javax.persistence.EntityManager;
-import javax.persistence.EntityManagerFactory;
-import javax.persistence.EntityTransaction;
-import javax.persistence.PersistenceContext;
-import javax.persistence.PersistenceContextType;
-import javax.persistence.SynchronizationType;
-
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-
-import org.springframework.orm.jpa.JpaTransactionManager;
-import org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor;
-import org.springframework.transaction.TransactionDefinition;
-import org.springframework.transaction.support.TransactionSynchronizationManager;
-import org.springframework.transaction.support.TransactionTemplate;
-
-import static org.junit.Assert.*;
-import static org.mockito.BDDMockito.*;
-
-/**
- * Copy of {@link org.springframework.orm.jpa.support.PersistenceContextTransactionTests},
- * here to be tested against JPA 2.1, including unsynchronized persistence contexts.
- *
- * @author Juergen Hoeller
- * @since 4.1.2
- */
-public class PersistenceContextTransactionTests {
-
- private EntityManagerFactory factory;
-
- private EntityManager manager;
-
- private EntityTransaction tx;
-
- private TransactionTemplate tt;
-
- private EntityManagerHoldingBean bean;
-
-
- @Before
- public void setUp() throws Exception {
- factory = mock(EntityManagerFactory.class);
- manager = mock(EntityManager.class);
- tx = mock(EntityTransaction.class);
-
- JpaTransactionManager tm = new JpaTransactionManager(factory);
- tt = new TransactionTemplate(tm);
-
- given(factory.createEntityManager()).willReturn(manager);
- given(manager.getTransaction()).willReturn(tx);
- given(manager.isOpen()).willReturn(true);
-
- bean = new EntityManagerHoldingBean();
- @SuppressWarnings("serial")
- PersistenceAnnotationBeanPostProcessor pabpp = new PersistenceAnnotationBeanPostProcessor() {
- @Override
- protected EntityManagerFactory findEntityManagerFactory(String unitName, String requestingBeanName) {
- return factory;
- }
- };
- pabpp.postProcessPropertyValues(null, null, bean, "bean");
-
- assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
- assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
- }
-
- @After
- public void tearDown() throws Exception {
- assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
- assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
- assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
- assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
- }
-
-
- @Test
- public void testTransactionCommitWithSharedEntityManager() {
- given(manager.getTransaction()).willReturn(tx);
-
- tt.execute(status -> {
- bean.sharedEntityManager.flush();
- return null;
- });
-
- verify(tx).commit();
- verify(manager).flush();
- verify(manager).close();
- }
-
- @Test
- public void testTransactionCommitWithSharedEntityManagerAndPropagationSupports() {
- given(manager.isOpen()).willReturn(true);
-
- tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
-
- tt.execute(status -> {
- bean.sharedEntityManager.clear();
- return null;
- });
-
- verify(manager).clear();
- verify(manager).close();
- }
-
- @Test
- public void testTransactionCommitWithExtendedEntityManager() {
- given(manager.getTransaction()).willReturn(tx);
-
- tt.execute(status -> {
- bean.extendedEntityManager.flush();
- return null;
- });
-
- verify(tx, times(2)).commit();
- verify(manager).flush();
- verify(manager).close();
- }
-
- @Test
- public void testTransactionCommitWithExtendedEntityManagerAndPropagationSupports() {
- given(manager.isOpen()).willReturn(true);
-
- tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
-
- tt.execute(status -> {
- bean.extendedEntityManager.flush();
- return null;
- });
-
- verify(manager).flush();
- }
-
- @Test
- public void testTransactionCommitWithSharedEntityManagerUnsynchronized() {
- given(manager.getTransaction()).willReturn(tx);
-
- tt.execute(status -> {
- bean.sharedEntityManagerUnsynchronized.flush();
- return null;
- });
-
- verify(tx).commit();
- verify(manager).flush();
- verify(manager, times(2)).close();
- }
-
- @Test
- public void testTransactionCommitWithSharedEntityManagerUnsynchronizedAndPropagationSupports() {
- given(manager.isOpen()).willReturn(true);
-
- tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
-
- tt.execute(status -> {
- bean.sharedEntityManagerUnsynchronized.clear();
- return null;
- });
-
- verify(manager).clear();
- verify(manager).close();
- }
-
- @Test
- public void testTransactionCommitWithExtendedEntityManagerUnsynchronized() {
- given(manager.getTransaction()).willReturn(tx);
-
- tt.execute(status -> {
- bean.extendedEntityManagerUnsynchronized.flush();
- return null;
- });
-
- verify(tx).commit();
- verify(manager).flush();
- verify(manager).close();
- }
-
- @Test
- public void testTransactionCommitWithExtendedEntityManagerUnsynchronizedAndPropagationSupports() {
- given(manager.isOpen()).willReturn(true);
-
- tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
-
- tt.execute(status -> {
- bean.extendedEntityManagerUnsynchronized.flush();
- return null;
- });
-
- verify(manager).flush();
- }
-
- @Test
- public void testTransactionCommitWithSharedEntityManagerUnsynchronizedJoined() {
- given(manager.getTransaction()).willReturn(tx);
-
- tt.execute(status -> {
- bean.sharedEntityManagerUnsynchronized.joinTransaction();
- bean.sharedEntityManagerUnsynchronized.flush();
- return null;
- });
-
- verify(tx).commit();
- verify(manager).flush();
- verify(manager, times(2)).close();
- }
-
- @Test
- public void testTransactionCommitWithExtendedEntityManagerUnsynchronizedJoined() {
- given(manager.getTransaction()).willReturn(tx);
-
- tt.execute(status -> {
- bean.extendedEntityManagerUnsynchronized.joinTransaction();
- bean.extendedEntityManagerUnsynchronized.flush();
- return null;
- });
-
- verify(tx, times(2)).commit();
- verify(manager).flush();
- verify(manager).close();
- }
-
- @Test
- public void testTransactionCommitWithExtendedEntityManagerUnsynchronizedJoinedAndPropagationSupports() {
- given(manager.isOpen()).willReturn(true);
-
- tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
-
- tt.execute(status -> {
- bean.extendedEntityManagerUnsynchronized.joinTransaction();
- bean.extendedEntityManagerUnsynchronized.flush();
- return null;
- });
-
- verify(manager).flush();
- }
-
-
- public static class EntityManagerHoldingBean {
-
- @PersistenceContext
- public EntityManager sharedEntityManager;
-
- @PersistenceContext(type = PersistenceContextType.EXTENDED)
- public EntityManager extendedEntityManager;
-
- @PersistenceContext(synchronization = SynchronizationType.UNSYNCHRONIZED)
- public EntityManager sharedEntityManagerUnsynchronized;
-
- @PersistenceContext(type = PersistenceContextType.EXTENDED, synchronization = SynchronizationType.UNSYNCHRONIZED)
- public EntityManager extendedEntityManagerUnsynchronized;
- }
-
-}
diff --git a/spring-orm-hibernate4/src/test/java/org/springframework/validation/hibernatevalidator5/BeanValidationPostProcessorTests.java b/spring-orm-hibernate4/src/test/java/org/springframework/validation/hibernatevalidator5/BeanValidationPostProcessorTests.java
deleted file mode 100644
index 36837d8cad..0000000000
--- a/spring-orm-hibernate4/src/test/java/org/springframework/validation/hibernatevalidator5/BeanValidationPostProcessorTests.java
+++ /dev/null
@@ -1,166 +0,0 @@
-/*
- * Copyright 2002-2015 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.validation.hibernatevalidator5;
-
-import javax.annotation.PostConstruct;
-import javax.validation.constraints.NotNull;
-import javax.validation.constraints.Size;
-
-import org.junit.Test;
-
-import org.springframework.beans.factory.BeanCreationException;
-import org.springframework.beans.factory.support.RootBeanDefinition;
-import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor;
-import org.springframework.context.support.GenericApplicationContext;
-import org.springframework.tests.sample.beans.TestBean;
-import org.springframework.validation.beanvalidation.BeanValidationPostProcessor;
-
-import static org.junit.Assert.*;
-
-/**
- * Copy of {@link org.springframework.validation.beanvalidation.BeanValidationPostProcessor},
- * here to be tested against Hibernate Validator 5.
- *
- * @author Juergen Hoeller
- * @since 4.1
- */
-public class BeanValidationPostProcessorTests {
-
- @Test
- public void testNotNullConstraint() {
- GenericApplicationContext ac = new GenericApplicationContext();
- ac.registerBeanDefinition("bvpp", new RootBeanDefinition(BeanValidationPostProcessor.class));
- ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
- ac.registerBeanDefinition("bean", new RootBeanDefinition(NotNullConstrainedBean.class));
- try {
- ac.refresh();
- fail("Should have thrown BeanCreationException");
- }
- catch (BeanCreationException ex) {
- assertTrue(ex.getRootCause().getMessage().contains("testBean"));
- assertTrue(ex.getRootCause().getMessage().contains("invalid"));
- }
- ac.close();
- }
-
- @Test
- public void testNotNullConstraintSatisfied() {
- GenericApplicationContext ac = new GenericApplicationContext();
- ac.registerBeanDefinition("bvpp", new RootBeanDefinition(BeanValidationPostProcessor.class));
- ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
- RootBeanDefinition bd = new RootBeanDefinition(NotNullConstrainedBean.class);
- bd.getPropertyValues().add("testBean", new TestBean());
- ac.registerBeanDefinition("bean", bd);
- ac.refresh();
- ac.close();
- }
-
- @Test
- public void testNotNullConstraintAfterInitialization() {
- GenericApplicationContext ac = new GenericApplicationContext();
- RootBeanDefinition bvpp = new RootBeanDefinition(BeanValidationPostProcessor.class);
- bvpp.getPropertyValues().add("afterInitialization", true);
- ac.registerBeanDefinition("bvpp", bvpp);
- ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
- ac.registerBeanDefinition("bean", new RootBeanDefinition(AfterInitConstraintBean.class));
- ac.refresh();
- ac.close();
- }
-
- @Test
- public void testSizeConstraint() {
- GenericApplicationContext ac = new GenericApplicationContext();
- ac.registerBeanDefinition("bvpp", new RootBeanDefinition(BeanValidationPostProcessor.class));
- RootBeanDefinition bd = new RootBeanDefinition(NotNullConstrainedBean.class);
- bd.getPropertyValues().add("testBean", new TestBean());
- bd.getPropertyValues().add("stringValue", "s");
- ac.registerBeanDefinition("bean", bd);
- try {
- ac.refresh();
- fail("Should have thrown BeanCreationException");
- }
- catch (BeanCreationException ex) {
- assertTrue(ex.getRootCause().getMessage().contains("stringValue"));
- assertTrue(ex.getRootCause().getMessage().contains("invalid"));
- }
- ac.close();
- }
-
- @Test
- public void testSizeConstraintSatisfied() {
- GenericApplicationContext ac = new GenericApplicationContext();
- ac.registerBeanDefinition("bvpp", new RootBeanDefinition(BeanValidationPostProcessor.class));
- RootBeanDefinition bd = new RootBeanDefinition(NotNullConstrainedBean.class);
- bd.getPropertyValues().add("testBean", new TestBean());
- bd.getPropertyValues().add("stringValue", "ss");
- ac.registerBeanDefinition("bean", bd);
- ac.refresh();
- ac.close();
- }
-
-
- public static class NotNullConstrainedBean {
-
- @NotNull
- private TestBean testBean;
-
- @Size(min = 2)
- private String stringValue;
-
- public TestBean getTestBean() {
- return testBean;
- }
-
- public void setTestBean(TestBean testBean) {
- this.testBean = testBean;
- }
-
- public String getStringValue() {
- return stringValue;
- }
-
- public void setStringValue(String stringValue) {
- this.stringValue = stringValue;
- }
-
- @PostConstruct
- public void init() {
- assertNotNull("Shouldn't be here after constraint checking", this.testBean);
- }
- }
-
-
- public static class AfterInitConstraintBean {
-
- @NotNull
- private TestBean testBean;
-
- public TestBean getTestBean() {
- return testBean;
- }
-
- public void setTestBean(TestBean testBean) {
- this.testBean = testBean;
- }
-
- @PostConstruct
- public void init() {
- this.testBean = new TestBean();
- }
- }
-
-}
diff --git a/spring-orm-hibernate4/src/test/java/org/springframework/validation/hibernatevalidator5/MethodValidationTests.java b/spring-orm-hibernate4/src/test/java/org/springframework/validation/hibernatevalidator5/MethodValidationTests.java
deleted file mode 100644
index af94ccb0af..0000000000
--- a/spring-orm-hibernate4/src/test/java/org/springframework/validation/hibernatevalidator5/MethodValidationTests.java
+++ /dev/null
@@ -1,173 +0,0 @@
-/*
- * Copyright 2002-2015 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.validation.hibernatevalidator5;
-
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import javax.validation.constraints.Max;
-import javax.validation.constraints.NotNull;
-import javax.validation.groups.Default;
-
-import org.junit.Test;
-
-import org.springframework.aop.framework.ProxyFactory;
-import org.springframework.beans.MutablePropertyValues;
-import org.springframework.context.support.StaticApplicationContext;
-import org.springframework.scheduling.annotation.Async;
-import org.springframework.scheduling.annotation.AsyncAnnotationAdvisor;
-import org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor;
-import org.springframework.validation.annotation.Validated;
-import org.springframework.validation.beanvalidation.MethodValidationInterceptor;
-import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
-
-import static org.junit.Assert.*;
-
-/**
- * Copy of {@link org.springframework.validation.beanvalidation.MethodValidationTests},
- * here to be tested against Hibernate Validator 5.
- *
- * @author Juergen Hoeller
- * @since 4.1
- */
-@SuppressWarnings("rawtypes")
-public class MethodValidationTests {
-
- @Test
- public void testMethodValidationInterceptor() {
- MyValidBean bean = new MyValidBean();
- ProxyFactory proxyFactory = new ProxyFactory(bean);
- proxyFactory.addAdvice(new MethodValidationInterceptor());
- proxyFactory.addAdvisor(new AsyncAnnotationAdvisor());
- doTestProxyValidation((MyValidInterface) proxyFactory.getProxy());
- }
-
- @Test
- public void testMethodValidationPostProcessor() {
- StaticApplicationContext ac = new StaticApplicationContext();
- ac.registerSingleton("mvpp", MethodValidationPostProcessor.class);
- MutablePropertyValues pvs = new MutablePropertyValues();
- pvs.add("beforeExistingAdvisors", false);
- ac.registerSingleton("aapp", AsyncAnnotationBeanPostProcessor.class, pvs);
- ac.registerSingleton("bean", MyValidBean.class);
- ac.refresh();
- doTestProxyValidation(ac.getBean("bean", MyValidInterface.class));
- ac.close();
- }
-
-
- @SuppressWarnings("unchecked")
- private void doTestProxyValidation(MyValidInterface proxy) {
- assertNotNull(proxy.myValidMethod("value", 5));
- try {
- assertNotNull(proxy.myValidMethod("value", 15));
- fail("Should have thrown ValidationException");
- }
- catch (javax.validation.ValidationException ex) {
- // expected
- }
- try {
- assertNotNull(proxy.myValidMethod(null, 5));
- fail("Should have thrown ValidationException");
- }
- catch (javax.validation.ValidationException ex) {
- // expected
- }
- try {
- assertNotNull(proxy.myValidMethod("value", 0));
- fail("Should have thrown ValidationException");
- }
- catch (javax.validation.ValidationException ex) {
- // expected
- }
-
- proxy.myValidAsyncMethod("value", 5);
- try {
- proxy.myValidAsyncMethod("value", 15);
- fail("Should have thrown ValidationException");
- }
- catch (javax.validation.ValidationException ex) {
- // expected
- }
- try {
- proxy.myValidAsyncMethod(null, 5);
- fail("Should have thrown ValidationException");
- }
- catch (javax.validation.ValidationException ex) {
- // expected
- }
-
- assertEquals("myValue", proxy.myGenericMethod("myValue"));
- try {
- proxy.myGenericMethod(null);
- fail("Should have thrown ValidationException");
- }
- catch (javax.validation.ValidationException ex) {
- // expected
- }
- }
-
-
- @MyStereotype
- public static class MyValidBean implements MyValidInterface {
-
- @Override
- public Object myValidMethod(String arg1, int arg2) {
- return (arg2 == 0 ? null : "value");
- }
-
- @Override
- public void myValidAsyncMethod(String arg1, int arg2) {
- }
-
- @Override
- public String myGenericMethod(String value) {
- return value;
- }
- }
-
-
- public interface MyValidInterface {
-
- @NotNull Object myValidMethod(@NotNull(groups = MyGroup.class) String arg1, @Max(10) int arg2);
-
- @MyValid
- @Async void myValidAsyncMethod(@NotNull(groups = OtherGroup.class) String arg1, @Max(10) int arg2);
-
- T myGenericMethod(@NotNull T value);
- }
-
-
- public interface MyGroup {
- }
-
-
- public interface OtherGroup {
- }
-
-
- @Validated({MyGroup.class, Default.class})
- @Retention(RetentionPolicy.RUNTIME)
- public @interface MyStereotype {
- }
-
-
- @Validated({OtherGroup.class, Default.class})
- @Retention(RetentionPolicy.RUNTIME)
- public @interface MyValid {
- }
-
-}
diff --git a/spring-orm-hibernate4/src/test/java/org/springframework/validation/hibernatevalidator5/ValidatorFactoryTests.java b/spring-orm-hibernate4/src/test/java/org/springframework/validation/hibernatevalidator5/ValidatorFactoryTests.java
deleted file mode 100644
index 4165836065..0000000000
--- a/spring-orm-hibernate4/src/test/java/org/springframework/validation/hibernatevalidator5/ValidatorFactoryTests.java
+++ /dev/null
@@ -1,430 +0,0 @@
-/*
- * Copyright 2002-2015 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.validation.hibernatevalidator5;
-
-import java.lang.annotation.ElementType;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.annotation.Target;
-import java.util.Arrays;
-import java.util.Iterator;
-import java.util.LinkedHashSet;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Optional;
-import java.util.Set;
-import javax.validation.Constraint;
-import javax.validation.ConstraintValidator;
-import javax.validation.ConstraintValidatorContext;
-import javax.validation.ConstraintViolation;
-import javax.validation.Payload;
-import javax.validation.Valid;
-import javax.validation.constraints.NotNull;
-
-import org.hibernate.validator.HibernateValidator;
-import org.junit.Test;
-
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.context.ConfigurableApplicationContext;
-import org.springframework.context.annotation.AnnotationConfigApplicationContext;
-import org.springframework.core.env.Environment;
-import org.springframework.validation.BeanPropertyBindingResult;
-import org.springframework.validation.Errors;
-import org.springframework.validation.FieldError;
-import org.springframework.validation.ObjectError;
-import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
-
-import static org.hamcrest.Matchers.*;
-import static org.junit.Assert.*;
-
-/**
- * Copy of {@link org.springframework.validation.beanvalidation.ValidatorFactoryTests},
- * here to be tested against Hibernate Validator 5.
- *
- * @author Juergen Hoeller
- * @since 4.1
- */
-public class ValidatorFactoryTests {
-
- @Test
- public void testSimpleValidation() throws Exception {
- LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
- validator.afterPropertiesSet();
-
- ValidPerson person = new ValidPerson();
- Set> result = validator.validate(person);
- assertEquals(2, result.size());
- for (ConstraintViolation cv : result) {
- String path = cv.getPropertyPath().toString();
- if ("name".equals(path) || "address.street".equals(path)) {
- assertTrue(cv.getConstraintDescriptor().getAnnotation() instanceof NotNull);
- }
- else {
- fail("Invalid constraint violation with path '" + path + "'");
- }
- }
- validator.destroy();
- }
-
- @Test
- public void testSimpleValidationWithCustomProvider() throws Exception {
- LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
- validator.setProviderClass(HibernateValidator.class);
- validator.afterPropertiesSet();
-
- ValidPerson person = new ValidPerson();
- Set> result = validator.validate(person);
- assertEquals(2, result.size());
- for (ConstraintViolation cv : result) {
- String path = cv.getPropertyPath().toString();
- if ("name".equals(path) || "address.street".equals(path)) {
- assertTrue(cv.getConstraintDescriptor().getAnnotation() instanceof NotNull);
- }
- else {
- fail("Invalid constraint violation with path '" + path + "'");
- }
- }
- validator.destroy();
- }
-
- @Test
- public void testSimpleValidationWithClassLevel() throws Exception {
- LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
- validator.afterPropertiesSet();
- ValidPerson person = new ValidPerson();
- person.setName("Juergen");
- person.getAddress().setStreet("Juergen's Street");
- Set> result = validator.validate(person);
- assertEquals(1, result.size());
- Iterator> iterator = result.iterator();
- ConstraintViolation> cv = iterator.next();
- assertEquals("", cv.getPropertyPath().toString());
- assertTrue(cv.getConstraintDescriptor().getAnnotation() instanceof NameAddressValid);
- }
-
- @Test
- public void testSpringValidationFieldType() throws Exception {
- LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
- validator.afterPropertiesSet();
-
- ValidPerson person = new ValidPerson();
- person.setName("Phil");
- person.getAddress().setStreet("Phil's Street");
- BeanPropertyBindingResult errors = new BeanPropertyBindingResult(person, "person");
- validator.validate(person, errors);
- assertEquals(1, errors.getErrorCount());
- assertThat("Field/Value type mismatch", errors.getFieldError("address").getRejectedValue(),
- instanceOf(ValidAddress.class));
- }
-
- @Test
- public void testSpringValidation() throws Exception {
- LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
- validator.afterPropertiesSet();
-
- ValidPerson person = new ValidPerson();
- BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
- validator.validate(person, result);
- assertEquals(2, result.getErrorCount());
- FieldError fieldError = result.getFieldError("name");
- assertEquals("name", fieldError.getField());
- List errorCodes = Arrays.asList(fieldError.getCodes());
- assertEquals(4, errorCodes.size());
- assertTrue(errorCodes.contains("NotNull.person.name"));
- assertTrue(errorCodes.contains("NotNull.name"));
- assertTrue(errorCodes.contains("NotNull.java.lang.String"));
- assertTrue(errorCodes.contains("NotNull"));
- fieldError = result.getFieldError("address.street");
- assertEquals("address.street", fieldError.getField());
- errorCodes = Arrays.asList(fieldError.getCodes());
- assertEquals(5, errorCodes.size());
- assertTrue(errorCodes.contains("NotNull.person.address.street"));
- assertTrue(errorCodes.contains("NotNull.address.street"));
- assertTrue(errorCodes.contains("NotNull.street"));
- assertTrue(errorCodes.contains("NotNull.java.lang.String"));
- assertTrue(errorCodes.contains("NotNull"));
- }
-
- @Test
- public void testSpringValidationWithClassLevel() throws Exception {
- LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
- validator.afterPropertiesSet();
-
- ValidPerson person = new ValidPerson();
- person.setName("Juergen");
- person.getAddress().setStreet("Juergen's Street");
- BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
- validator.validate(person, result);
- assertEquals(1, result.getErrorCount());
- ObjectError globalError = result.getGlobalError();
- List errorCodes = Arrays.asList(globalError.getCodes());
- assertEquals(2, errorCodes.size());
- assertTrue(errorCodes.contains("NameAddressValid.person"));
- assertTrue(errorCodes.contains("NameAddressValid"));
- }
-
- @Test
- public void testSpringValidationWithAutowiredValidator() throws Exception {
- ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(
- LocalValidatorFactoryBean.class);
- LocalValidatorFactoryBean validator = ctx.getBean(LocalValidatorFactoryBean.class);
-
- ValidPerson person = new ValidPerson();
- person.expectsAutowiredValidator = true;
- person.setName("Juergen");
- person.getAddress().setStreet("Juergen's Street");
- BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
- validator.validate(person, result);
- assertEquals(1, result.getErrorCount());
- ObjectError globalError = result.getGlobalError();
- List errorCodes = Arrays.asList(globalError.getCodes());
- assertEquals(2, errorCodes.size());
- assertTrue(errorCodes.contains("NameAddressValid.person"));
- assertTrue(errorCodes.contains("NameAddressValid"));
- ctx.close();
- }
-
- @Test
- public void testSpringValidationWithErrorInListElement() throws Exception {
- LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
- validator.afterPropertiesSet();
-
- ValidPerson person = new ValidPerson();
- person.getAddressList().add(new ValidAddress());
- BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
- validator.validate(person, result);
- assertEquals(3, result.getErrorCount());
- FieldError fieldError = result.getFieldError("name");
- assertEquals("name", fieldError.getField());
- fieldError = result.getFieldError("address.street");
- assertEquals("address.street", fieldError.getField());
- fieldError = result.getFieldError("addressList[0].street");
- assertEquals("addressList[0].street", fieldError.getField());
- }
-
- @Test
- public void testSpringValidationWithErrorInSetElement() throws Exception {
- LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
- validator.afterPropertiesSet();
-
- ValidPerson person = new ValidPerson();
- person.getAddressSet().add(new ValidAddress());
- BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
- validator.validate(person, result);
- assertEquals(3, result.getErrorCount());
- FieldError fieldError = result.getFieldError("name");
- assertEquals("name", fieldError.getField());
- fieldError = result.getFieldError("address.street");
- assertEquals("address.street", fieldError.getField());
- fieldError = result.getFieldError("addressSet[].street");
- assertEquals("addressSet[].street", fieldError.getField());
- }
-
- @Test
- public void testInnerBeanValidation() throws Exception {
- LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
- validator.afterPropertiesSet();
-
- MainBean mainBean = new MainBean();
- Errors errors = new BeanPropertyBindingResult(mainBean, "mainBean");
- validator.validate(mainBean, errors);
- Object rejected = errors.getFieldValue("inner.value");
- assertNull(rejected);
- }
-
- @Test
- public void testValidationWithOptionalField() throws Exception {
- LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
- validator.afterPropertiesSet();
-
- MainBeanWithOptional mainBean = new MainBeanWithOptional();
- Errors errors = new BeanPropertyBindingResult(mainBean, "mainBean");
- validator.validate(mainBean, errors);
- Object rejected = errors.getFieldValue("inner.value");
- assertNull(rejected);
- }
-
-
- @NameAddressValid
- public static class ValidPerson {
-
- @NotNull
- private String name;
-
- @Valid
- private ValidAddress address = new ValidAddress();
-
- @Valid
- private List addressList = new LinkedList();
-
- @Valid
- private Set addressSet = new LinkedHashSet();
-
- public boolean expectsAutowiredValidator = false;
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public ValidAddress getAddress() {
- return address;
- }
-
- public void setAddress(ValidAddress address) {
- this.address = address;
- }
-
- public List getAddressList() {
- return addressList;
- }
-
- public void setAddressList(List addressList) {
- this.addressList = addressList;
- }
-
- public Set getAddressSet() {
- return addressSet;
- }
-
- public void setAddressSet(Set addressSet) {
- this.addressSet = addressSet;
- }
- }
-
-
- public static class ValidAddress {
-
- @NotNull
- private String street;
-
- public String getStreet() {
- return street;
- }
-
- public void setStreet(String street) {
- this.street = street;
- }
- }
-
-
- @Target(ElementType.TYPE)
- @Retention(RetentionPolicy.RUNTIME)
- @Constraint(validatedBy = NameAddressValidator.class)
- public @interface NameAddressValid {
-
- String message() default "Street must not contain name";
-
- Class>[] groups() default {};
-
- Class>[] payload() default {};
- }
-
-
- public static class NameAddressValidator implements ConstraintValidator {
-
- @Autowired
- private Environment environment;
-
- @Override
- public void initialize(NameAddressValid constraintAnnotation) {
- }
-
- @Override
- public boolean isValid(ValidPerson value, ConstraintValidatorContext context) {
- if (value.expectsAutowiredValidator) {
- assertNotNull(this.environment);
- }
- boolean valid = (value.name == null || !value.address.street.contains(value.name));
- if (!valid && "Phil".equals(value.name)) {
- context.buildConstraintViolationWithTemplate(
- context.getDefaultConstraintMessageTemplate()).addPropertyNode("address").addConstraintViolation().disableDefaultConstraintViolation();
- }
- return valid;
- }
- }
-
-
- public static class MainBean {
-
- @InnerValid
- private InnerBean inner = new InnerBean();
-
- public InnerBean getInner() {
- return inner;
- }
- }
-
-
- public static class MainBeanWithOptional {
-
- @InnerValid
- private InnerBean inner = new InnerBean();
-
- public Optional getInner() {
- return Optional.ofNullable(inner);
- }
- }
-
-
- public static class InnerBean {
-
- private String value;
-
- public String getValue() {
- return value;
- }
- public void setValue(String value) {
- this.value = value;
- }
- }
-
-
- @Retention(RetentionPolicy.RUNTIME)
- @Target(ElementType.FIELD)
- @Constraint(validatedBy=InnerValidator.class)
- public static @interface InnerValid {
-
- String message() default "NOT VALID";
-
- Class>[] groups() default { };
-
- Class extends Payload>[] payload() default {};
- }
-
-
- public static class InnerValidator implements ConstraintValidator {
-
- @Override
- public void initialize(InnerValid constraintAnnotation) {
- }
-
- @Override
- public boolean isValid(InnerBean bean, ConstraintValidatorContext context) {
- context.disableDefaultConstraintViolation();
- if (bean.getValue() == null) {
- context.buildConstraintViolationWithTemplate("NULL").addPropertyNode("value").addConstraintViolation();
- return false;
- }
- return true;
- }
- }
-
-}
diff --git a/spring-orm-hibernate4/src/test/resources/log4j.properties b/spring-orm-hibernate4/src/test/resources/log4j.properties
deleted file mode 100644
index 9b7421a223..0000000000
--- a/spring-orm-hibernate4/src/test/resources/log4j.properties
+++ /dev/null
@@ -1,10 +0,0 @@
-log4j.appender.console=org.apache.log4j.ConsoleAppender
-log4j.appender.console.layout=org.apache.log4j.PatternLayout
-log4j.appender.console.layout.ConversionPattern=%d{HH:mm:ss,SSS} [%c] - %m%n
-
-log4j.rootCategory=WARN, console
-log4j.logger.org.springframework.beans=WARN
-log4j.logger.org.springframework.binding=DEBUG
-
-#log4j.logger.org.springframework.orm.hibernate4=TRACE
-
diff --git a/spring-orm/src/main/java/org/springframework/orm/hibernate3/AbstractSessionFactoryBean.java b/spring-orm/src/main/java/org/springframework/orm/hibernate3/AbstractSessionFactoryBean.java
deleted file mode 100644
index 61f731064b..0000000000
--- a/spring-orm/src/main/java/org/springframework/orm/hibernate3/AbstractSessionFactoryBean.java
+++ /dev/null
@@ -1,285 +0,0 @@
-/*
- * 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate3;
-
-import javax.sql.DataSource;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.hibernate.HibernateException;
-import org.hibernate.SessionFactory;
-
-import org.springframework.beans.factory.DisposableBean;
-import org.springframework.beans.factory.FactoryBean;
-import org.springframework.beans.factory.InitializingBean;
-
-/**
- * Abstract {@link org.springframework.beans.factory.FactoryBean} that creates
- * a Hibernate {@link org.hibernate.SessionFactory} within a Spring application
- * context, providing general infrastructure not related to Hibernate's
- * specific configuration API.
- *
- * This class implements the
- * {@link org.springframework.dao.support.PersistenceExceptionTranslator}
- * interface, as autodetected by Spring's
- * {@link org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor},
- * for AOP-based translation of native exceptions to Spring DataAccessExceptions.
- * Hence, the presence of e.g. LocalSessionFactoryBean automatically enables
- * a PersistenceExceptionTranslationPostProcessor to translate Hibernate exceptions.
- *
- *
This class mainly serves as common base class for {@link LocalSessionFactoryBean}.
- * For details on typical SessionFactory setup, see the LocalSessionFactoryBean javadoc.
- *
- * @author Juergen Hoeller
- * @since 2.0
- * @see #setExposeTransactionAwareSessionFactory
- * @see org.hibernate.SessionFactory#getCurrentSession()
- * @see org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor
- * @deprecated as of Spring 4.3, in favor of Hibernate 4.x/5.x
- */
-@Deprecated
-public abstract class AbstractSessionFactoryBean extends HibernateExceptionTranslator
- implements FactoryBean, InitializingBean, DisposableBean {
-
- /** Logger available to subclasses */
- protected final Log logger = LogFactory.getLog(getClass());
-
- private DataSource dataSource;
-
- private boolean useTransactionAwareDataSource = false;
-
- private boolean exposeTransactionAwareSessionFactory = true;
-
- private SessionFactory sessionFactory;
-
-
- /**
- * Set the DataSource to be used by the SessionFactory.
- * If set, this will override corresponding settings in Hibernate properties.
- * If this is set, the Hibernate settings should not define
- * a connection provider to avoid meaningless double configuration.
- *
If using HibernateTransactionManager as transaction strategy, consider
- * proxying your target DataSource with a LazyConnectionDataSourceProxy.
- * This defers fetching of an actual JDBC Connection until the first JDBC
- * Statement gets executed, even within JDBC transactions (as performed by
- * HibernateTransactionManager). Such lazy fetching is particularly beneficial
- * for read-only operations, in particular if the chances of resolving the
- * result in the second-level cache are high.
- *
As JTA and transactional JNDI DataSources already provide lazy enlistment
- * of JDBC Connections, LazyConnectionDataSourceProxy does not add value with
- * JTA (i.e. Spring's JtaTransactionManager) as transaction strategy.
- * @see #setUseTransactionAwareDataSource
- * @see HibernateTransactionManager
- * @see org.springframework.transaction.jta.JtaTransactionManager
- * @see org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy
- */
- public void setDataSource(DataSource dataSource) {
- this.dataSource = dataSource;
- }
-
- /**
- * Return the DataSource to be used by the SessionFactory.
- */
- public DataSource getDataSource() {
- return this.dataSource;
- }
-
- /**
- * Set whether to use a transaction-aware DataSource for the SessionFactory,
- * i.e. whether to automatically wrap the passed-in DataSource with Spring's
- * TransactionAwareDataSourceProxy.
- *
Default is "false": LocalSessionFactoryBean is usually used with Spring's
- * HibernateTransactionManager or JtaTransactionManager, both of which work nicely
- * on a plain JDBC DataSource. Hibernate Sessions and their JDBC Connections are
- * fully managed by the Hibernate/JTA transaction infrastructure in such a scenario.
- *
If you switch this flag to "true", Spring's Hibernate access will be able to
- * participate in JDBC-based transactions managed outside of Hibernate
- * (for example, by Spring's DataSourceTransactionManager). This can be convenient
- * if you need a different local transaction strategy for another O/R mapping tool,
- * for example, but still want Hibernate access to join into those transactions.
- *
A further benefit of this option is that plain Sessions opened directly
- * via the SessionFactory , outside of Spring's Hibernate support, will still
- * participate in active Spring-managed transactions. However, consider using
- * Hibernate's {@code getCurrentSession()} method instead (see javadoc of
- * "exposeTransactionAwareSessionFactory" property).
- *
WARNING: When using a transaction-aware JDBC DataSource in combination
- * with OpenSessionInViewFilter/Interceptor, whether participating in JTA or
- * external JDBC-based transactions, it is strongly recommended to set Hibernate's
- * Connection release mode to "after_transaction" or "after_statement", which
- * guarantees proper Connection handling in such a scenario. In contrast to that,
- * HibernateTransactionManager generally requires release mode "on_close".
- *
Note: If you want to use Hibernate's Connection release mode "after_statement"
- * with a DataSource specified on this LocalSessionFactoryBean (for example, a
- * JTA-aware DataSource fetched from JNDI), switch this setting to "true".
- * Otherwise, the ConnectionProvider used underneath will vote against aggressive
- * release and thus silently switch to release mode "after_transaction".
- * @see #setDataSource
- * @see #setExposeTransactionAwareSessionFactory
- * @see org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy
- * @see org.springframework.jdbc.datasource.DataSourceTransactionManager
- * @see org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
- * @see org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor
- * @see HibernateTransactionManager
- * @see org.springframework.transaction.jta.JtaTransactionManager
- */
- public void setUseTransactionAwareDataSource(boolean useTransactionAwareDataSource) {
- this.useTransactionAwareDataSource = useTransactionAwareDataSource;
- }
-
- /**
- * Return whether to use a transaction-aware DataSource for the SessionFactory.
- */
- protected boolean isUseTransactionAwareDataSource() {
- return this.useTransactionAwareDataSource;
- }
-
- /**
- * Set whether to expose a transaction-aware current Session from the
- * SessionFactory's {@code getCurrentSession()} method, returning the
- * Session that's associated with the current Spring-managed transaction, if any.
- *
Default is "true", letting data access code work with the plain
- * Hibernate SessionFactory and its {@code getCurrentSession()} method,
- * while still being able to participate in current Spring-managed transactions:
- * with any transaction management strategy, either local or JTA / EJB CMT,
- * and any transaction synchronization mechanism, either Spring or JTA.
- * Furthermore, {@code getCurrentSession()} will also seamlessly work with
- * a request-scoped Session managed by OpenSessionInViewFilter/Interceptor.
- *
Turn this flag off to expose the plain Hibernate SessionFactory with
- * Hibernate's default {@code getCurrentSession()} behavior, supporting
- * plain JTA synchronization only. Alternatively, simply override the
- * corresponding Hibernate property "hibernate.current_session_context_class".
- * @see SpringSessionContext
- * @see org.hibernate.SessionFactory#getCurrentSession()
- * @see org.springframework.transaction.jta.JtaTransactionManager
- * @see HibernateTransactionManager
- * @see org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
- * @see org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor
- */
- public void setExposeTransactionAwareSessionFactory(boolean exposeTransactionAwareSessionFactory) {
- this.exposeTransactionAwareSessionFactory = exposeTransactionAwareSessionFactory;
- }
-
- /**
- * Return whether to expose a transaction-aware proxy for the SessionFactory.
- */
- protected boolean isExposeTransactionAwareSessionFactory() {
- return this.exposeTransactionAwareSessionFactory;
- }
-
-
- /**
- * Build and expose the SessionFactory.
- * @see #buildSessionFactory()
- * @see #wrapSessionFactoryIfNecessary
- */
- @Override
- public void afterPropertiesSet() throws Exception {
- SessionFactory rawSf = buildSessionFactory();
- this.sessionFactory = wrapSessionFactoryIfNecessary(rawSf);
- afterSessionFactoryCreation();
- }
-
- /**
- * Wrap the given SessionFactory with a proxy, if demanded.
- *
The default implementation simply returns the given SessionFactory as-is.
- * Subclasses may override this to implement transaction awareness through
- * a SessionFactory proxy, for example.
- * @param rawSf the raw SessionFactory as built by {@link #buildSessionFactory()}
- * @return the SessionFactory reference to expose
- * @see #buildSessionFactory()
- */
- protected SessionFactory wrapSessionFactoryIfNecessary(SessionFactory rawSf) {
- return rawSf;
- }
-
- /**
- * Return the exposed SessionFactory.
- * Will throw an exception if not initialized yet.
- * @return the SessionFactory (never {@code null})
- * @throws IllegalStateException if the SessionFactory has not been initialized yet
- */
- protected final SessionFactory getSessionFactory() {
- if (this.sessionFactory == null) {
- throw new IllegalStateException("SessionFactory not initialized yet");
- }
- return this.sessionFactory;
- }
-
- /**
- * Close the SessionFactory on bean factory shutdown.
- */
- @Override
- public void destroy() throws HibernateException {
- logger.info("Closing Hibernate SessionFactory");
- try {
- beforeSessionFactoryDestruction();
- }
- finally {
- this.sessionFactory.close();
- }
- }
-
-
- /**
- * Return the singleton SessionFactory.
- */
- @Override
- public SessionFactory getObject() {
- return this.sessionFactory;
- }
-
- @Override
- public Class extends SessionFactory> getObjectType() {
- return (this.sessionFactory != null ? this.sessionFactory.getClass() : SessionFactory.class);
- }
-
- @Override
- public boolean isSingleton() {
- return true;
- }
-
-
- /**
- * Build the underlying Hibernate SessionFactory.
- * @return the raw SessionFactory (potentially to be wrapped with a
- * transaction-aware proxy before it is exposed to the application)
- * @throws Exception in case of initialization failure
- */
- protected abstract SessionFactory buildSessionFactory() throws Exception;
-
- /**
- * Hook that allows post-processing after the SessionFactory has been
- * successfully created. The SessionFactory is already available through
- * {@code getSessionFactory()} at this point.
- *
This implementation is empty.
- * @throws Exception in case of initialization failure
- * @see #getSessionFactory()
- */
- protected void afterSessionFactoryCreation() throws Exception {
- }
-
- /**
- * Hook that allows shutdown processing before the SessionFactory
- * will be closed. The SessionFactory is still available through
- * {@code getSessionFactory()} at this point.
- *
This implementation is empty.
- * @see #getSessionFactory()
- */
- protected void beforeSessionFactoryDestruction() {
- }
-
-}
diff --git a/spring-orm/src/main/java/org/springframework/orm/hibernate3/FilterDefinitionFactoryBean.java b/spring-orm/src/main/java/org/springframework/orm/hibernate3/FilterDefinitionFactoryBean.java
deleted file mode 100644
index 24f5c33a63..0000000000
--- a/spring-orm/src/main/java/org/springframework/orm/hibernate3/FilterDefinitionFactoryBean.java
+++ /dev/null
@@ -1,145 +0,0 @@
-/*
- * Copyright 2002-2013 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate3;
-
-import java.util.HashMap;
-import java.util.Map;
-
-import org.hibernate.engine.FilterDefinition;
-import org.hibernate.type.Type;
-import org.hibernate.type.TypeResolver;
-
-import org.springframework.beans.factory.BeanNameAware;
-import org.springframework.beans.factory.FactoryBean;
-import org.springframework.beans.factory.InitializingBean;
-
-/**
- * Convenient FactoryBean for defining Hibernate FilterDefinitions.
- * Exposes a corresponding Hibernate FilterDefinition object.
- *
- *
Typically defined as an inner bean within a LocalSessionFactoryBean
- * definition, as the list element for the "filterDefinitions" bean property.
- * For example:
- *
- *
- * <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- * ...
- * <property name="filterDefinitions">
- * <list>
- * <bean class="org.springframework.orm.hibernate3.FilterDefinitionFactoryBean">
- * <property name="filterName" value="myFilter"/>
- * <property name="parameterTypes">
- * <map>
- * <entry key="myParam" value="string"/>
- * <entry key="myOtherParam" value="long"/>
- * </map>
- * </property>
- * </bean>
- * </list>
- * </property>
- * ...
- * </bean>
- *
- * Alternatively, specify a bean id (or name) attribute for the inner bean,
- * instead of the "filterName" property.
- *
- * @author Juergen Hoeller
- * @since 1.2
- * @see org.hibernate.engine.FilterDefinition
- * @see LocalSessionFactoryBean#setFilterDefinitions
- * @deprecated as of Spring 4.3, in favor of Hibernate 4.x/5.x
- */
-@Deprecated
-public class FilterDefinitionFactoryBean implements FactoryBean, BeanNameAware, InitializingBean {
-
- private final TypeResolver typeResolver = new TypeResolver();
-
- private String filterName;
-
- private Map parameterTypeMap = new HashMap();
-
- private String defaultFilterCondition;
-
- private FilterDefinition filterDefinition;
-
-
- /**
- * Set the name of the filter.
- */
- public void setFilterName(String filterName) {
- this.filterName = filterName;
- }
-
- /**
- * Set the parameter types for the filter,
- * with parameter names as keys and type names as values.
- * See {@code org.hibernate.type.TypeResolver#heuristicType(String)}.
- */
- public void setParameterTypes(Map parameterTypes) {
- if (parameterTypes != null) {
- this.parameterTypeMap = new HashMap(parameterTypes.size());
- for (Map.Entry entry : parameterTypes.entrySet()) {
- this.parameterTypeMap.put(entry.getKey(), this.typeResolver.heuristicType(entry.getValue()));
- }
- }
- else {
- this.parameterTypeMap = new HashMap();
- }
- }
-
- /**
- * Specify a default filter condition for the filter, if any.
- */
- public void setDefaultFilterCondition(String defaultFilterCondition) {
- this.defaultFilterCondition = defaultFilterCondition;
- }
-
- /**
- * If no explicit filter name has been specified, the bean name of
- * the FilterDefinitionFactoryBean will be used.
- * @see #setFilterName
- */
- @Override
- public void setBeanName(String name) {
- if (this.filterName == null) {
- this.filterName = name;
- }
- }
-
- @Override
- public void afterPropertiesSet() {
- this.filterDefinition =
- new FilterDefinition(this.filterName, this.defaultFilterCondition, this.parameterTypeMap);
- }
-
-
- @Override
- public FilterDefinition getObject() {
- return this.filterDefinition;
- }
-
- @Override
- public Class getObjectType() {
- return FilterDefinition.class;
- }
-
- @Override
- public boolean isSingleton() {
- return true;
- }
-
-}
diff --git a/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateAccessor.java b/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateAccessor.java
deleted file mode 100644
index 8b5dc6647d..0000000000
--- a/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateAccessor.java
+++ /dev/null
@@ -1,492 +0,0 @@
-/*
- * Copyright 2002-2014 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate3;
-
-import java.sql.SQLException;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.hibernate.FlushMode;
-import org.hibernate.HibernateException;
-import org.hibernate.Interceptor;
-import org.hibernate.JDBCException;
-import org.hibernate.Session;
-import org.hibernate.SessionFactory;
-import org.hibernate.exception.GenericJDBCException;
-
-import org.springframework.beans.BeansException;
-import org.springframework.beans.factory.BeanFactory;
-import org.springframework.beans.factory.BeanFactoryAware;
-import org.springframework.beans.factory.InitializingBean;
-import org.springframework.core.Constants;
-import org.springframework.dao.DataAccessException;
-import org.springframework.jdbc.support.SQLExceptionTranslator;
-
-/**
- * Base class for {@link HibernateTemplate} and {@link HibernateInterceptor},
- * defining common properties such as SessionFactory and flushing behavior.
- *
- * Not intended to be used directly.
- * See {@link HibernateTemplate} and {@link HibernateInterceptor}.
- *
- * @author Juergen Hoeller
- * @since 1.2
- * @see HibernateTemplate
- * @see HibernateInterceptor
- * @see #setFlushMode
- * @deprecated as of Spring 4.3, in favor of Hibernate 4.x/5.x
- */
-@Deprecated
-public abstract class HibernateAccessor implements InitializingBean, BeanFactoryAware {
-
- /**
- * Never flush is a good strategy for read-only units of work.
- * Hibernate will not track and look for changes in this case,
- * avoiding any overhead of modification detection.
- *
In case of an existing Session, FLUSH_NEVER will turn the flush mode
- * to NEVER for the scope of the current operation, resetting the previous
- * flush mode afterwards.
- * @see #setFlushMode
- */
- public static final int FLUSH_NEVER = 0;
-
- /**
- * Automatic flushing is the default mode for a Hibernate Session.
- * A session will get flushed on transaction commit, and on certain find
- * operations that might involve already modified instances, but not
- * after each unit of work like with eager flushing.
- *
In case of an existing Session, FLUSH_AUTO will participate in the
- * existing flush mode, not modifying it for the current operation.
- * This in particular means that this setting will not modify an existing
- * flush mode NEVER, in contrast to FLUSH_EAGER.
- * @see #setFlushMode
- */
- public static final int FLUSH_AUTO = 1;
-
- /**
- * Eager flushing leads to immediate synchronization with the database,
- * even if in a transaction. This causes inconsistencies to show up and throw
- * a respective exception immediately, and JDBC access code that participates
- * in the same transaction will see the changes as the database is already
- * aware of them then. But the drawbacks are:
- *
- * additional communication roundtrips with the database, instead of a
- * single batch at transaction commit;
- * the fact that an actual database rollback is needed if the Hibernate
- * transaction rolls back (due to already submitted SQL statements).
- *
- * In case of an existing Session, FLUSH_EAGER will turn the flush mode
- * to AUTO for the scope of the current operation and issue a flush at the
- * end, resetting the previous flush mode afterwards.
- * @see #setFlushMode
- */
- public static final int FLUSH_EAGER = 2;
-
- /**
- * Flushing at commit only is intended for units of work where no
- * intermediate flushing is desired, not even for find operations
- * that might involve already modified instances.
- *
In case of an existing Session, FLUSH_COMMIT will turn the flush mode
- * to COMMIT for the scope of the current operation, resetting the previous
- * flush mode afterwards. The only exception is an existing flush mode
- * NEVER, which will not be modified through this setting.
- * @see #setFlushMode
- */
- public static final int FLUSH_COMMIT = 3;
-
- /**
- * Flushing before every query statement is rarely necessary.
- * It is only available for special needs.
- *
In case of an existing Session, FLUSH_ALWAYS will turn the flush mode
- * to ALWAYS for the scope of the current operation, resetting the previous
- * flush mode afterwards.
- * @see #setFlushMode
- */
- public static final int FLUSH_ALWAYS = 4;
-
-
- /** Constants instance for HibernateAccessor */
- private static final Constants constants = new Constants(HibernateAccessor.class);
-
- /** Logger available to subclasses */
- protected final Log logger = LogFactory.getLog(getClass());
-
- private SessionFactory sessionFactory;
-
- private Object entityInterceptor;
-
- private SQLExceptionTranslator jdbcExceptionTranslator;
-
- private SQLExceptionTranslator defaultJdbcExceptionTranslator;
-
- private int flushMode = FLUSH_AUTO;
-
- private String[] filterNames;
-
- /**
- * Just needed for entityInterceptorBeanName.
- * @see #setEntityInterceptorBeanName
- */
- private BeanFactory beanFactory;
-
-
- /**
- * Set the Hibernate SessionFactory that should be used to create
- * Hibernate Sessions.
- */
- public void setSessionFactory(SessionFactory sessionFactory) {
- this.sessionFactory = sessionFactory;
- }
-
- /**
- * Return the Hibernate SessionFactory that should be used to create
- * Hibernate Sessions.
- */
- public SessionFactory getSessionFactory() {
- return this.sessionFactory;
- }
-
- /**
- * Set the bean name of a Hibernate entity interceptor that allows to inspect
- * and change property values before writing to and reading from the database.
- * Will get applied to any new Session created by this transaction manager.
- *
Requires the bean factory to be known, to be able to resolve the bean
- * name to an interceptor instance on session creation. Typically used for
- * prototype interceptors, i.e. a new interceptor instance per session.
- *
Can also be used for shared interceptor instances, but it is recommended
- * to set the interceptor reference directly in such a scenario.
- * @param entityInterceptorBeanName the name of the entity interceptor in
- * the bean factory
- * @see #setBeanFactory
- * @see #setEntityInterceptor
- */
- public void setEntityInterceptorBeanName(String entityInterceptorBeanName) {
- this.entityInterceptor = entityInterceptorBeanName;
- }
-
- /**
- * Set a Hibernate entity interceptor that allows to inspect and change
- * property values before writing to and reading from the database.
- * Will get applied to any new Session created by this object.
- *
Such an interceptor can either be set at the SessionFactory level,
- * i.e. on LocalSessionFactoryBean, or at the Session level, i.e. on
- * HibernateTemplate, HibernateInterceptor, and HibernateTransactionManager.
- * It's preferable to set it on LocalSessionFactoryBean or HibernateTransactionManager
- * to avoid repeated configuration and guarantee consistent behavior in transactions.
- * @see #setEntityInterceptorBeanName
- * @see LocalSessionFactoryBean#setEntityInterceptor
- * @see HibernateTransactionManager#setEntityInterceptor
- */
- public void setEntityInterceptor(Interceptor entityInterceptor) {
- this.entityInterceptor = entityInterceptor;
- }
-
- /**
- * Return the current Hibernate entity interceptor, or {@code null} if none.
- * Resolves an entity interceptor bean name via the bean factory,
- * if necessary.
- * @throws IllegalStateException if bean name specified but no bean factory set
- * @throws org.springframework.beans.BeansException if bean name resolution via the bean factory failed
- * @see #setEntityInterceptor
- * @see #setEntityInterceptorBeanName
- * @see #setBeanFactory
- */
- public Interceptor getEntityInterceptor() throws IllegalStateException, BeansException {
- if (this.entityInterceptor instanceof String) {
- if (this.beanFactory == null) {
- throw new IllegalStateException("Cannot get entity interceptor via bean name if no bean factory set");
- }
- return this.beanFactory.getBean((String) this.entityInterceptor, Interceptor.class);
- }
- return (Interceptor) this.entityInterceptor;
- }
-
- /**
- * Set the JDBC exception translator for this instance.
- *
Applied to any SQLException root cause of a Hibernate JDBCException,
- * overriding Hibernate's default SQLException translation (which is
- * based on Hibernate's Dialect for a specific target database).
- * @param jdbcExceptionTranslator the exception translator
- * @see java.sql.SQLException
- * @see org.hibernate.JDBCException
- * @see org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator
- * @see org.springframework.jdbc.support.SQLStateSQLExceptionTranslator
- */
- public void setJdbcExceptionTranslator(SQLExceptionTranslator jdbcExceptionTranslator) {
- this.jdbcExceptionTranslator = jdbcExceptionTranslator;
- }
-
- /**
- * Return the JDBC exception translator for this instance, if any.
- */
- public SQLExceptionTranslator getJdbcExceptionTranslator() {
- return this.jdbcExceptionTranslator;
- }
-
- /**
- * Set the flush behavior by the name of the respective constant
- * in this class, e.g. "FLUSH_AUTO". Default is "FLUSH_AUTO".
- * @param constantName name of the constant
- * @see #setFlushMode
- * @see #FLUSH_AUTO
- */
- public void setFlushModeName(String constantName) {
- setFlushMode(constants.asNumber(constantName).intValue());
- }
-
- /**
- * Set the flush behavior to one of the constants in this class.
- * Default is FLUSH_AUTO.
- * @see #setFlushModeName
- * @see #FLUSH_AUTO
- */
- public void setFlushMode(int flushMode) {
- this.flushMode = flushMode;
- }
-
- /**
- * Return if a flush should be forced after executing the callback code.
- */
- public int getFlushMode() {
- return this.flushMode;
- }
-
- /**
- * Set the name of a Hibernate filter to be activated for all
- * Sessions that this accessor works with.
- *
This filter will be enabled at the beginning of each operation
- * and correspondingly disabled at the end of the operation.
- * This will work for newly opened Sessions as well as for existing
- * Sessions (for example, within a transaction).
- * @see #enableFilters(org.hibernate.Session)
- * @see org.hibernate.Session#enableFilter(String)
- * @see LocalSessionFactoryBean#setFilterDefinitions
- */
- public void setFilterName(String filter) {
- this.filterNames = new String[] {filter};
- }
-
- /**
- * Set one or more names of Hibernate filters to be activated for all
- * Sessions that this accessor works with.
- *
Each of those filters will be enabled at the beginning of each
- * operation and correspondingly disabled at the end of the operation.
- * This will work for newly opened Sessions as well as for existing
- * Sessions (for example, within a transaction).
- * @see #enableFilters(org.hibernate.Session)
- * @see org.hibernate.Session#enableFilter(String)
- * @see LocalSessionFactoryBean#setFilterDefinitions
- */
- public void setFilterNames(String... filterNames) {
- this.filterNames = filterNames;
- }
-
- /**
- * Return the names of Hibernate filters to be activated, if any.
- */
- public String[] getFilterNames() {
- return this.filterNames;
- }
-
- /**
- * The bean factory just needs to be known for resolving entity interceptor
- * bean names. It does not need to be set for any other mode of operation.
- * @see #setEntityInterceptorBeanName
- */
- @Override
- public void setBeanFactory(BeanFactory beanFactory) {
- this.beanFactory = beanFactory;
- }
-
- @Override
- public void afterPropertiesSet() {
- if (getSessionFactory() == null) {
- throw new IllegalArgumentException("Property 'sessionFactory' is required");
- }
- }
-
-
- /**
- * Apply the flush mode that's been specified for this accessor
- * to the given Session.
- * @param session the current Hibernate Session
- * @param existingTransaction if executing within an existing transaction
- * @return the previous flush mode to restore after the operation,
- * or {@code null} if none
- * @see #setFlushMode
- * @see org.hibernate.Session#setFlushMode
- */
- protected FlushMode applyFlushMode(Session session, boolean existingTransaction) {
- if (getFlushMode() == FLUSH_NEVER) {
- if (existingTransaction) {
- FlushMode previousFlushMode = session.getFlushMode();
- if (!previousFlushMode.lessThan(FlushMode.COMMIT)) {
- session.setFlushMode(FlushMode.MANUAL);
- return previousFlushMode;
- }
- }
- else {
- session.setFlushMode(FlushMode.MANUAL);
- }
- }
- else if (getFlushMode() == FLUSH_EAGER) {
- if (existingTransaction) {
- FlushMode previousFlushMode = session.getFlushMode();
- if (!previousFlushMode.equals(FlushMode.AUTO)) {
- session.setFlushMode(FlushMode.AUTO);
- return previousFlushMode;
- }
- }
- else {
- // rely on default FlushMode.AUTO
- }
- }
- else if (getFlushMode() == FLUSH_COMMIT) {
- if (existingTransaction) {
- FlushMode previousFlushMode = session.getFlushMode();
- if (previousFlushMode.equals(FlushMode.AUTO) || previousFlushMode.equals(FlushMode.ALWAYS)) {
- session.setFlushMode(FlushMode.COMMIT);
- return previousFlushMode;
- }
- }
- else {
- session.setFlushMode(FlushMode.COMMIT);
- }
- }
- else if (getFlushMode() == FLUSH_ALWAYS) {
- if (existingTransaction) {
- FlushMode previousFlushMode = session.getFlushMode();
- if (!previousFlushMode.equals(FlushMode.ALWAYS)) {
- session.setFlushMode(FlushMode.ALWAYS);
- return previousFlushMode;
- }
- }
- else {
- session.setFlushMode(FlushMode.ALWAYS);
- }
- }
- return null;
- }
-
- /**
- * Flush the given Hibernate Session if necessary.
- * @param session the current Hibernate Session
- * @param existingTransaction if executing within an existing transaction
- * @throws HibernateException in case of Hibernate flushing errors
- */
- protected void flushIfNecessary(Session session, boolean existingTransaction) throws HibernateException {
- if (getFlushMode() == FLUSH_EAGER || (!existingTransaction && getFlushMode() != FLUSH_NEVER)) {
- logger.debug("Eagerly flushing Hibernate session");
- session.flush();
- }
- }
-
-
- /**
- * Convert the given HibernateException to an appropriate exception
- * from the {@code org.springframework.dao} hierarchy.
- *
Will automatically apply a specified SQLExceptionTranslator to a
- * Hibernate JDBCException, else rely on Hibernate's default translation.
- * @param ex HibernateException that occured
- * @return a corresponding DataAccessException
- * @see SessionFactoryUtils#convertHibernateAccessException
- * @see #setJdbcExceptionTranslator
- */
- public DataAccessException convertHibernateAccessException(HibernateException ex) {
- if (getJdbcExceptionTranslator() != null && ex instanceof JDBCException) {
- return convertJdbcAccessException((JDBCException) ex, getJdbcExceptionTranslator());
- }
- else if (GenericJDBCException.class == ex.getClass()) {
- return convertJdbcAccessException((GenericJDBCException) ex, getDefaultJdbcExceptionTranslator());
- }
- return SessionFactoryUtils.convertHibernateAccessException(ex);
- }
-
- /**
- * Convert the given Hibernate JDBCException to an appropriate exception
- * from the {@code org.springframework.dao} hierarchy, using the
- * given SQLExceptionTranslator.
- * @param ex Hibernate JDBCException that occured
- * @param translator the SQLExceptionTranslator to use
- * @return a corresponding DataAccessException
- */
- protected DataAccessException convertJdbcAccessException(JDBCException ex, SQLExceptionTranslator translator) {
- return translator.translate("Hibernate operation: " + ex.getMessage(), ex.getSQL(), ex.getSQLException());
- }
-
- /**
- * Convert the given SQLException to an appropriate exception from the
- * {@code org.springframework.dao} hierarchy. Can be overridden in subclasses.
- *
Note that a direct SQLException can just occur when callback code
- * performs direct JDBC access via {@code Session.connection()}.
- * @param ex the SQLException
- * @return the corresponding DataAccessException instance
- * @see #setJdbcExceptionTranslator
- */
- protected DataAccessException convertJdbcAccessException(SQLException ex) {
- SQLExceptionTranslator translator = getJdbcExceptionTranslator();
- if (translator == null) {
- translator = getDefaultJdbcExceptionTranslator();
- }
- return translator.translate("Hibernate-related JDBC operation", null, ex);
- }
-
- /**
- * Obtain a default SQLExceptionTranslator, lazily creating it if necessary.
- *
Creates a default
- * {@link org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator}
- * for the SessionFactory's underlying DataSource.
- */
- protected synchronized SQLExceptionTranslator getDefaultJdbcExceptionTranslator() {
- if (this.defaultJdbcExceptionTranslator == null) {
- this.defaultJdbcExceptionTranslator = SessionFactoryUtils.newJdbcExceptionTranslator(getSessionFactory());
- }
- return this.defaultJdbcExceptionTranslator;
- }
-
-
- /**
- * Enable the specified filters on the given Session.
- * @param session the current Hibernate Session
- * @see #setFilterNames
- * @see org.hibernate.Session#enableFilter(String)
- */
- protected void enableFilters(Session session) {
- String[] filterNames = getFilterNames();
- if (filterNames != null) {
- for (String filterName : filterNames) {
- session.enableFilter(filterName);
- }
- }
- }
-
- /**
- * Disable the specified filters on the given Session.
- * @param session the current Hibernate Session
- * @see #setFilterNames
- * @see org.hibernate.Session#disableFilter(String)
- */
- protected void disableFilters(Session session) {
- String[] filterNames = getFilterNames();
- if (filterNames != null) {
- for (String filterName : filterNames) {
- session.disableFilter(filterName);
- }
- }
- }
-
-}
diff --git a/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateCallback.java b/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateCallback.java
deleted file mode 100644
index db081638d9..0000000000
--- a/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateCallback.java
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * Copyright 2002-2016 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate3;
-
-import java.sql.SQLException;
-
-import org.hibernate.HibernateException;
-import org.hibernate.Session;
-
-/**
- * Callback interface for Hibernate code. To be used with {@link HibernateTemplate}'s
- * execution methods, often as anonymous classes within a method implementation.
- * A typical implementation will call {@code Session.load/find/update} to perform
- * some operations on persistent objects. It can also perform direct JDBC operations
- * via Hibernate's {@code Session.connection()}, operating on a JDBC Connection.
- *
- *
Note that Hibernate works on unmodified plain Java objects, performing dirty
- * detection via copies made at load time. Returned objects can thus be used outside
- * of an active Hibernate Session without any hassle, e.g. for display in a web GUI.
- * Reassociating such instances with a new Session, e.g. for updates when coming
- * back from the GUI, is straightforward, as the instance has kept its identity.
- * You should care to reassociate them as early as possible though, to avoid having
- * already loaded a version from the database in the same Session.
- *
- * @author Juergen Hoeller
- * @since 1.2
- * @see HibernateTemplate
- * @see HibernateTransactionManager
- * @deprecated as of Spring 4.3, in favor of Hibernate 4.x/5.x
- */
-@Deprecated
-public interface HibernateCallback {
-
- /**
- * Gets called by {@code HibernateTemplate.execute} with an active
- * Hibernate {@code Session}. Does not need to care about activating
- * or closing the {@code Session}, or handling transactions.
- * If called without a thread-bound Hibernate transaction (initiated
- * by HibernateTransactionManager), the code will simply get executed on the
- * underlying JDBC connection with its transactional semantics. If Hibernate
- * is configured to use a JTA-aware DataSource, the JDBC connection and thus
- * the callback code will be transactional if a JTA transaction is active.
- *
Allows for returning a result object created within the callback,
- * i.e. a domain object or a collection of domain objects.
- * A thrown custom RuntimeException is treated as an application exception:
- * It gets propagated to the caller of the template.
- * @param session active Hibernate session
- * @return a result object, or {@code null} if none
- * @throws HibernateException if thrown by the Hibernate API
- * @throws SQLException if thrown by Hibernate-exposed JDBC API
- * @see HibernateTemplate#execute
- * @see HibernateTemplate#executeFind
- */
- T doInHibernate(Session session) throws HibernateException, SQLException;
-
-}
diff --git a/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateExceptionTranslator.java b/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateExceptionTranslator.java
deleted file mode 100644
index db7401743b..0000000000
--- a/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateExceptionTranslator.java
+++ /dev/null
@@ -1,95 +0,0 @@
-/*
- * 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate3;
-
-import org.hibernate.HibernateException;
-import org.hibernate.JDBCException;
-
-import org.springframework.dao.DataAccessException;
-import org.springframework.dao.support.PersistenceExceptionTranslator;
-import org.springframework.jdbc.support.SQLExceptionTranslator;
-
-/**
- * {@link PersistenceExceptionTranslator} capable of translating {@link HibernateException}
- * instances to Spring's {@link DataAccessException} hierarchy.
- *
- *
Extended by {@link LocalSessionFactoryBean}, so there is no need to declare this
- * translator in addition to a {@code LocalSessionFactoryBean}.
- *
- *
When configuring the container with {@code @Configuration} classes, a {@code @Bean}
- * of this type must be registered manually.
- *
- * @author Juergen Hoeller
- * @author Chris Beams
- * @since 3.1
- * @see org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor
- * @see SessionFactoryUtils#convertHibernateAccessException(HibernateException)
- * @see SQLExceptionTranslator
- * @deprecated as of Spring 4.3, in favor of Hibernate 4.x/5.x
- */
-@Deprecated
-public class HibernateExceptionTranslator implements PersistenceExceptionTranslator {
-
- private SQLExceptionTranslator jdbcExceptionTranslator;
-
-
- /**
- * Set the JDBC exception translator for the SessionFactory,
- * exposed via the PersistenceExceptionTranslator interface.
- *
Applied to any SQLException root cause of a Hibernate JDBCException,
- * overriding Hibernate's default SQLException translation (which is
- * based on Hibernate's Dialect for a specific target database).
- * @param jdbcExceptionTranslator the exception translator
- * @see java.sql.SQLException
- * @see org.hibernate.JDBCException
- * @see org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator
- * @see org.springframework.jdbc.support.SQLStateSQLExceptionTranslator
- * @see org.springframework.dao.support.PersistenceExceptionTranslator
- */
- public void setJdbcExceptionTranslator(SQLExceptionTranslator jdbcExceptionTranslator) {
- this.jdbcExceptionTranslator = jdbcExceptionTranslator;
- }
-
-
- @Override
- public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
- if (ex instanceof HibernateException) {
- return convertHibernateAccessException((HibernateException) ex);
- }
- return null;
- }
-
- /**
- * Convert the given HibernateException to an appropriate exception from the
- * {@code org.springframework.dao} hierarchy.
- *
Will automatically apply a specified SQLExceptionTranslator to a
- * Hibernate JDBCException, else rely on Hibernate's default translation.
- * @param ex HibernateException that occured
- * @return a corresponding DataAccessException
- * @see SessionFactoryUtils#convertHibernateAccessException
- * @see #setJdbcExceptionTranslator
- */
- protected DataAccessException convertHibernateAccessException(HibernateException ex) {
- if (this.jdbcExceptionTranslator != null && ex instanceof JDBCException) {
- JDBCException jdbcEx = (JDBCException) ex;
- return this.jdbcExceptionTranslator.translate(
- "Hibernate operation: " + jdbcEx.getMessage(), jdbcEx.getSQL(), jdbcEx.getSQLException());
- }
- return SessionFactoryUtils.convertHibernateAccessException(ex);
- }
-
-}
diff --git a/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateInterceptor.java b/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateInterceptor.java
deleted file mode 100644
index 8feba6af36..0000000000
--- a/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateInterceptor.java
+++ /dev/null
@@ -1,157 +0,0 @@
-/*
- * Copyright 2002-2014 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate3;
-
-import org.aopalliance.intercept.MethodInterceptor;
-import org.aopalliance.intercept.MethodInvocation;
-import org.hibernate.FlushMode;
-import org.hibernate.HibernateException;
-import org.hibernate.Session;
-
-import org.springframework.transaction.support.TransactionSynchronizationManager;
-
-/**
- * This interceptor binds a new Hibernate Session to the thread before a method
- * call, closing and removing it afterwards in case of any method outcome.
- * If there already is a pre-bound Session (e.g. from HibernateTransactionManager,
- * or from a surrounding Hibernate-intercepted method), the interceptor simply
- * participates in it.
- *
- *
Application code must retrieve a Hibernate Session via the
- * {@code SessionFactoryUtils.getSession} method or - preferably -
- * Hibernate's own {@code SessionFactory.getCurrentSession()} method, to be
- * able to detect a thread-bound Session. Typically, the code will look like as follows:
- *
- *
- * public void doSomeDataAccessAction() {
- * Session session = this.sessionFactory.getCurrentSession();
- * ...
- * // No need to close the Session or translate exceptions!
- * }
- *
- * Note that this interceptor automatically translates HibernateExceptions,
- * via delegating to the {@code SessionFactoryUtils.convertHibernateAccessException}
- * method that converts them to exceptions that are compatible with the
- * {@code org.springframework.dao} exception hierarchy (like HibernateTemplate does).
- * This can be turned off if the raw exceptions are preferred.
- *
- * This class can be considered a declarative alternative to HibernateTemplate's
- * callback approach. The advantages are:
- *
- * no anonymous classes necessary for callback implementations;
- * the possibility to throw any application exceptions from within data access code.
- *
- *
- * The drawback is the dependency on interceptor configuration. However, note
- * that this interceptor is usually not necessary in scenarios where the
- * data access code always executes within transactions. A transaction will always
- * have a thread-bound Session in the first place, so adding this interceptor to the
- * configuration just adds value when fine-tuning Session settings like the flush mode
- * - or when relying on exception translation.
- *
- * @author Juergen Hoeller
- * @since 1.2
- * @see org.hibernate.SessionFactory#getCurrentSession()
- * @see HibernateTransactionManager
- * @see HibernateTemplate
- * @deprecated as of Spring 3.2.7, in favor of either HibernateTemplate usage or
- * native Hibernate API usage within transactions, in combination with a general
- * {@link org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor}.
- * Note: This class does not have an equivalent replacement in {@code orm.hibernate4}.
- * If you desperately need a scoped Session bound through AOP, consider the newly
- * introduced {@link org.springframework.orm.hibernate3.support.OpenSessionInterceptor}.
- */
-@Deprecated
-public class HibernateInterceptor extends HibernateAccessor implements MethodInterceptor {
-
- private boolean exceptionConversionEnabled = true;
-
-
- /**
- * Set whether to convert any HibernateException raised to a Spring DataAccessException,
- * compatible with the {@code org.springframework.dao} exception hierarchy.
- *
Default is "true". Turn this flag off to let the caller receive raw exceptions
- * as-is, without any wrapping.
- * @see org.springframework.dao.DataAccessException
- */
- public void setExceptionConversionEnabled(boolean exceptionConversionEnabled) {
- this.exceptionConversionEnabled = exceptionConversionEnabled;
- }
-
-
- @Override
- public Object invoke(MethodInvocation methodInvocation) throws Throwable {
- Session session = getSession();
- SessionHolder sessionHolder =
- (SessionHolder) TransactionSynchronizationManager.getResource(getSessionFactory());
-
- boolean existingTransaction = (sessionHolder != null && sessionHolder.containsSession(session));
- if (existingTransaction) {
- logger.debug("Found thread-bound Session for HibernateInterceptor");
- }
- else {
- if (sessionHolder != null) {
- sessionHolder.addSession(session);
- }
- else {
- TransactionSynchronizationManager.bindResource(getSessionFactory(), new SessionHolder(session));
- }
- }
-
- FlushMode previousFlushMode = null;
- try {
- previousFlushMode = applyFlushMode(session, existingTransaction);
- enableFilters(session);
- Object retVal = methodInvocation.proceed();
- flushIfNecessary(session, existingTransaction);
- return retVal;
- }
- catch (HibernateException ex) {
- if (this.exceptionConversionEnabled) {
- throw convertHibernateAccessException(ex);
- }
- else {
- throw ex;
- }
- }
- finally {
- if (existingTransaction) {
- logger.debug("Not closing pre-bound Hibernate Session after HibernateInterceptor");
- disableFilters(session);
- if (previousFlushMode != null) {
- session.setFlushMode(previousFlushMode);
- }
- }
- else {
- SessionFactoryUtils.closeSessionOrRegisterDeferredClose(session, getSessionFactory());
- if (sessionHolder == null || sessionHolder.doesNotHoldNonDefaultSession()) {
- TransactionSynchronizationManager.unbindResource(getSessionFactory());
- }
- }
- }
- }
-
- /**
- * Return a Session for use by this interceptor.
- * @see SessionFactoryUtils#getSession
- */
- protected Session getSession() {
- return SessionFactoryUtils.getSession(
- getSessionFactory(), getEntityInterceptor(), getJdbcExceptionTranslator());
- }
-
-}
diff --git a/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateJdbcException.java b/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateJdbcException.java
deleted file mode 100644
index 24af697090..0000000000
--- a/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateJdbcException.java
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
- * 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate3;
-
-import java.sql.SQLException;
-
-import org.hibernate.JDBCException;
-
-import org.springframework.dao.UncategorizedDataAccessException;
-
-/**
- * Hibernate-specific subclass of UncategorizedDataAccessException,
- * for JDBC exceptions that Hibernate wrapped.
- *
- * @author Juergen Hoeller
- * @since 1.2
- * @see SessionFactoryUtils#convertHibernateAccessException
- * @deprecated as of Spring 4.3, in favor of Hibernate 4.x/5.x
- */
-@Deprecated
-@SuppressWarnings("serial")
-public class HibernateJdbcException extends UncategorizedDataAccessException {
-
- public HibernateJdbcException(JDBCException ex) {
- super("JDBC exception on Hibernate data access: SQLException for SQL [" + ex.getSQL() + "]; SQL state [" +
- ex.getSQLState() + "]; error code [" + ex.getErrorCode() + "]; " + ex.getMessage(), ex);
- }
-
- /**
- * Return the underlying SQLException.
- */
- public SQLException getSQLException() {
- return ((JDBCException) getCause()).getSQLException();
- }
-
- /**
- * Return the SQL that led to the problem.
- */
- public String getSql() {
- return ((JDBCException) getCause()).getSQL();
- }
-
-}
diff --git a/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateObjectRetrievalFailureException.java b/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateObjectRetrievalFailureException.java
deleted file mode 100644
index 9a4cdf9a2e..0000000000
--- a/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateObjectRetrievalFailureException.java
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- * 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate3;
-
-import org.hibernate.UnresolvableObjectException;
-import org.hibernate.WrongClassException;
-
-import org.springframework.orm.ObjectRetrievalFailureException;
-
-/**
- * Hibernate-specific subclass of ObjectRetrievalFailureException.
- * Converts Hibernate's UnresolvableObjectException and WrongClassException.
- *
- * @author Juergen Hoeller
- * @since 1.2
- * @see SessionFactoryUtils#convertHibernateAccessException
- * @deprecated as of Spring 4.3, in favor of Hibernate 4.x/5.x
- */
-@Deprecated
-@SuppressWarnings("serial")
-public class HibernateObjectRetrievalFailureException extends ObjectRetrievalFailureException {
-
- public HibernateObjectRetrievalFailureException(UnresolvableObjectException ex) {
- super(ex.getEntityName(), ex.getIdentifier(), ex.getMessage(), ex);
- }
-
- public HibernateObjectRetrievalFailureException(WrongClassException ex) {
- super(ex.getEntityName(), ex.getIdentifier(), ex.getMessage(), ex);
- }
-
-}
diff --git a/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateOperations.java b/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateOperations.java
deleted file mode 100644
index 6facdbec07..0000000000
--- a/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateOperations.java
+++ /dev/null
@@ -1,921 +0,0 @@
-/*
- * Copyright 2002-2014 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate3;
-
-import java.io.Serializable;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.List;
-
-import org.hibernate.Filter;
-import org.hibernate.LockMode;
-import org.hibernate.ReplicationMode;
-import org.hibernate.criterion.DetachedCriteria;
-
-import org.springframework.dao.DataAccessException;
-
-/**
- * Interface that specifies a basic set of Hibernate operations,
- * implemented by {@link HibernateTemplate}. Not often used, but a useful
- * option to enhance testability, as it can easily be mocked or stubbed.
- *
- *
Defines {@code HibernateTemplate}'s data access methods that
- * mirror various {@link org.hibernate.Session} methods. Users are
- * strongly encouraged to read the Hibernate {@code Session} javadocs
- * for details on the semantics of those methods.
- *
- *
Note that operations that return an {@link java.util.Iterator} (i.e.
- * {@code iterate(..)}) are supposed to be used within Spring-driven
- * or JTA-driven transactions (with {@link HibernateTransactionManager},
- * {@link org.springframework.transaction.jta.JtaTransactionManager},
- * or EJB CMT). Else, the {@code Iterator} won't be able to read
- * results from its {@link java.sql.ResultSet} anymore, as the underlying
- * Hibernate {@code Session} will already have been closed.
- *
- *
Note that lazy loading will just work with an open Hibernate
- * {@code Session}, either within a transaction or within
- * {@link org.springframework.orm.hibernate3.support.OpenSessionInViewFilter}/
- * {@link org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor}.
- * Furthermore, some operations just make sense within transactions,
- * for example: {@code contains}, {@code evict}, {@code lock},
- * {@code flush}, {@code clear}.
- *
- * @author Juergen Hoeller
- * @since 1.2
- * @see HibernateTemplate
- * @see org.hibernate.Session
- * @see HibernateTransactionManager
- * @see org.springframework.transaction.jta.JtaTransactionManager
- * @see org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
- * @see org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor
- * @deprecated as of Spring 4.3, in favor of Hibernate 4.x/5.x
- */
-@Deprecated
-public interface HibernateOperations {
-
- /**
- * Execute the action specified by the given action object within a
- * {@link org.hibernate.Session}.
- *
Application exceptions thrown by the action object get propagated
- * to the caller (can only be unchecked). Hibernate exceptions are
- * transformed into appropriate DAO ones. Allows for returning a result
- * object, that is a domain object or a collection of domain objects.
- *
Note: Callback code is not supposed to handle transactions itself!
- * Use an appropriate transaction manager like
- * {@link HibernateTransactionManager}. Generally, callback code must not
- * touch any {@code Session} lifecycle methods, like close,
- * disconnect, or reconnect, to let the template do its work.
- * @param action callback object that specifies the Hibernate action
- * @return a result object returned by the action, or {@code null}
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see HibernateTransactionManager
- * @see org.hibernate.Session
- */
- T execute(HibernateCallback action) throws DataAccessException;
-
- /**
- * Execute the specified action assuming that the result object is a
- * {@link List}.
- * This is a convenience method for executing Hibernate find calls or
- * queries within an action.
- * @param action callback object that specifies the Hibernate action
- * @return a List result returned by the action, or {@code null}
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @deprecated as of Spring 3.2.7, in favor of using a regular {@link #execute}
- * call with a generic List type declared
- */
- @Deprecated
- List> executeFind(HibernateCallback> action) throws DataAccessException;
-
-
- //-------------------------------------------------------------------------
- // Convenience methods for loading individual objects
- //-------------------------------------------------------------------------
-
- /**
- * Return the persistent instance of the given entity class
- * with the given identifier, or {@code null} if not found.
- *
This method is a thin wrapper around
- * {@link org.hibernate.Session#get(Class, java.io.Serializable)} for convenience.
- * For an explanation of the exact semantics of this method, please do refer to
- * the Hibernate API documentation in the first instance.
- * @param entityClass a persistent class
- * @param id the identifier of the persistent instance
- * @return the persistent instance, or {@code null} if not found
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#get(Class, java.io.Serializable)
- */
- T get(Class entityClass, Serializable id) throws DataAccessException;
-
- /**
- * Return the persistent instance of the given entity class
- * with the given identifier, or {@code null} if not found.
- * Obtains the specified lock mode if the instance exists.
- *
This method is a thin wrapper around
- * {@link org.hibernate.Session#get(Class, java.io.Serializable, LockMode)} for convenience.
- * For an explanation of the exact semantics of this method, please do refer to
- * the Hibernate API documentation in the first instance.
- * @param entityClass a persistent class
- * @param id the identifier of the persistent instance
- * @param lockMode the lock mode to obtain
- * @return the persistent instance, or {@code null} if not found
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#get(Class, java.io.Serializable, org.hibernate.LockMode)
- */
- T get(Class entityClass, Serializable id, LockMode lockMode) throws DataAccessException;
-
- /**
- * Return the persistent instance of the given entity class
- * with the given identifier, or {@code null} if not found.
- * This method is a thin wrapper around
- * {@link org.hibernate.Session#get(String, java.io.Serializable)} for convenience.
- * For an explanation of the exact semantics of this method, please do refer to
- * the Hibernate API documentation in the first instance.
- * @param entityName the name of the persistent entity
- * @param id the identifier of the persistent instance
- * @return the persistent instance, or {@code null} if not found
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#get(Class, java.io.Serializable)
- */
- Object get(String entityName, Serializable id) throws DataAccessException;
-
- /**
- * Return the persistent instance of the given entity class
- * with the given identifier, or {@code null} if not found.
- * Obtains the specified lock mode if the instance exists.
- *
This method is a thin wrapper around
- * {@link org.hibernate.Session#get(String, java.io.Serializable, LockMode)} for convenience.
- * For an explanation of the exact semantics of this method, please do refer to
- * the Hibernate API documentation in the first instance.
- * @param entityName the name of the persistent entity
- * @param id the identifier of the persistent instance
- * @param lockMode the lock mode to obtain
- * @return the persistent instance, or {@code null} if not found
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#get(Class, java.io.Serializable, org.hibernate.LockMode)
- */
- Object get(String entityName, Serializable id, LockMode lockMode) throws DataAccessException;
-
- /**
- * Return the persistent instance of the given entity class
- * with the given identifier, throwing an exception if not found.
- *
This method is a thin wrapper around
- * {@link org.hibernate.Session#load(Class, java.io.Serializable)} for convenience.
- * For an explanation of the exact semantics of this method, please do refer to
- * the Hibernate API documentation in the first instance.
- * @param entityClass a persistent class
- * @param id the identifier of the persistent instance
- * @return the persistent instance
- * @throws org.springframework.orm.ObjectRetrievalFailureException if not found
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#load(Class, java.io.Serializable)
- */
- T load(Class entityClass, Serializable id) throws DataAccessException;
-
- /**
- * Return the persistent instance of the given entity class
- * with the given identifier, throwing an exception if not found.
- * Obtains the specified lock mode if the instance exists.
- * This method is a thin wrapper around
- * {@link org.hibernate.Session#load(Class, java.io.Serializable, LockMode)} for convenience.
- * For an explanation of the exact semantics of this method, please do refer to
- * the Hibernate API documentation in the first instance.
- * @param entityClass a persistent class
- * @param id the identifier of the persistent instance
- * @param lockMode the lock mode to obtain
- * @return the persistent instance
- * @throws org.springframework.orm.ObjectRetrievalFailureException if not found
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#load(Class, java.io.Serializable)
- */
- T load(Class entityClass, Serializable id, LockMode lockMode) throws DataAccessException;
-
- /**
- * Return the persistent instance of the given entity class
- * with the given identifier, throwing an exception if not found.
- * This method is a thin wrapper around
- * {@link org.hibernate.Session#load(String, java.io.Serializable)} for convenience.
- * For an explanation of the exact semantics of this method, please do refer to
- * the Hibernate API documentation in the first instance.
- * @param entityName the name of the persistent entity
- * @param id the identifier of the persistent instance
- * @return the persistent instance
- * @throws org.springframework.orm.ObjectRetrievalFailureException if not found
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#load(Class, java.io.Serializable)
- */
- Object load(String entityName, Serializable id) throws DataAccessException;
-
- /**
- * Return the persistent instance of the given entity class
- * with the given identifier, throwing an exception if not found.
- *
Obtains the specified lock mode if the instance exists.
- *
This method is a thin wrapper around
- * {@link org.hibernate.Session#load(String, java.io.Serializable, LockMode)} for convenience.
- * For an explanation of the exact semantics of this method, please do refer to
- * the Hibernate API documentation in the first instance.
- * @param entityName the name of the persistent entity
- * @param id the identifier of the persistent instance
- * @param lockMode the lock mode to obtain
- * @return the persistent instance
- * @throws org.springframework.orm.ObjectRetrievalFailureException if not found
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#load(Class, java.io.Serializable)
- */
- Object load(String entityName, Serializable id, LockMode lockMode) throws DataAccessException;
-
- /**
- * Return all persistent instances of the given entity class.
- * Note: Use queries or criteria for retrieving a specific subset.
- * @param entityClass a persistent class
- * @return a {@link List} containing 0 or more persistent instances
- * @throws org.springframework.dao.DataAccessException if there is a Hibernate error
- * @see org.hibernate.Session#createCriteria
- */
- List loadAll(Class entityClass) throws DataAccessException;
-
- /**
- * Load the persistent instance with the given identifier
- * into the given object, throwing an exception if not found.
- * This method is a thin wrapper around
- * {@link org.hibernate.Session#load(Object, java.io.Serializable)} for convenience.
- * For an explanation of the exact semantics of this method, please do refer to
- * the Hibernate API documentation in the first instance.
- * @param entity the object (of the target class) to load into
- * @param id the identifier of the persistent instance
- * @throws org.springframework.orm.ObjectRetrievalFailureException if not found
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#load(Object, java.io.Serializable)
- */
- void load(Object entity, Serializable id) throws DataAccessException;
-
- /**
- * Re-read the state of the given persistent instance.
- * @param entity the persistent instance to re-read
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#refresh(Object)
- */
- void refresh(Object entity) throws DataAccessException;
-
- /**
- * Re-read the state of the given persistent instance.
- * Obtains the specified lock mode for the instance.
- * @param entity the persistent instance to re-read
- * @param lockMode the lock mode to obtain
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#refresh(Object, org.hibernate.LockMode)
- */
- void refresh(Object entity, LockMode lockMode) throws DataAccessException;
-
- /**
- * Check whether the given object is in the Session cache.
- * @param entity the persistence instance to check
- * @return whether the given object is in the Session cache
- * @throws org.springframework.dao.DataAccessException if there is a Hibernate error
- * @see org.hibernate.Session#contains
- */
- boolean contains(Object entity) throws DataAccessException;
-
- /**
- * Remove the given object from the {@link org.hibernate.Session} cache.
- * @param entity the persistent instance to evict
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#evict
- */
- void evict(Object entity) throws DataAccessException;
-
- /**
- * Force initialization of a Hibernate proxy or persistent collection.
- * @param proxy a proxy for a persistent object or a persistent collection
- * @throws DataAccessException if we can't initialize the proxy, for example
- * because it is not associated with an active Session
- * @see org.hibernate.Hibernate#initialize
- */
- void initialize(Object proxy) throws DataAccessException;
-
- /**
- * Return an enabled Hibernate {@link Filter} for the given filter name.
- * The returned {@code Filter} instance can be used to set filter parameters.
- * @param filterName the name of the filter
- * @return the enabled Hibernate {@code Filter} (either already
- * enabled or enabled on the fly by this operation)
- * @throws IllegalStateException if we are not running within a
- * transactional Session (in which case this operation does not make sense)
- */
- Filter enableFilter(String filterName) throws IllegalStateException;
-
-
- //-------------------------------------------------------------------------
- // Convenience methods for storing individual objects
- //-------------------------------------------------------------------------
-
- /**
- * Obtain the specified lock level upon the given object, implicitly
- * checking whether the corresponding database entry still exists.
- * @param entity the persistent instance to lock
- * @param lockMode the lock mode to obtain
- * @throws org.springframework.orm.ObjectOptimisticLockingFailureException if not found
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#lock(Object, org.hibernate.LockMode)
- */
- void lock(Object entity, LockMode lockMode) throws DataAccessException;
-
- /**
- * Obtain the specified lock level upon the given object, implicitly
- * checking whether the corresponding database entry still exists.
- * @param entityName the name of the persistent entity
- * @param entity the persistent instance to lock
- * @param lockMode the lock mode to obtain
- * @throws org.springframework.orm.ObjectOptimisticLockingFailureException if not found
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#lock(String, Object, org.hibernate.LockMode)
- */
- void lock(String entityName, Object entity, LockMode lockMode) throws DataAccessException;
-
- /**
- * Persist the given transient instance.
- * @param entity the transient instance to persist
- * @return the generated identifier
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#save(Object)
- */
- Serializable save(Object entity) throws DataAccessException;
-
- /**
- * Persist the given transient instance.
- * @param entityName the name of the persistent entity
- * @param entity the transient instance to persist
- * @return the generated identifier
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#save(String, Object)
- */
- Serializable save(String entityName, Object entity) throws DataAccessException;
-
- /**
- * Update the given persistent instance,
- * associating it with the current Hibernate {@link org.hibernate.Session}.
- * @param entity the persistent instance to update
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#update(Object)
- */
- void update(Object entity) throws DataAccessException;
-
- /**
- * Update the given persistent instance,
- * associating it with the current Hibernate {@link org.hibernate.Session}.
- *
Obtains the specified lock mode if the instance exists, implicitly
- * checking whether the corresponding database entry still exists.
- * @param entity the persistent instance to update
- * @param lockMode the lock mode to obtain
- * @throws org.springframework.orm.ObjectOptimisticLockingFailureException if not found
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#update(Object)
- */
- void update(Object entity, LockMode lockMode) throws DataAccessException;
-
- /**
- * Update the given persistent instance,
- * associating it with the current Hibernate {@link org.hibernate.Session}.
- * @param entityName the name of the persistent entity
- * @param entity the persistent instance to update
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#update(String, Object)
- */
- void update(String entityName, Object entity) throws DataAccessException;
-
- /**
- * Update the given persistent instance,
- * associating it with the current Hibernate {@link org.hibernate.Session}.
- *
Obtains the specified lock mode if the instance exists, implicitly
- * checking whether the corresponding database entry still exists.
- * @param entityName the name of the persistent entity
- * @param entity the persistent instance to update
- * @param lockMode the lock mode to obtain
- * @throws org.springframework.orm.ObjectOptimisticLockingFailureException if not found
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#update(String, Object)
- */
- void update(String entityName, Object entity, LockMode lockMode) throws DataAccessException;
-
- /**
- * Save or update the given persistent instance,
- * according to its id (matching the configured "unsaved-value"?).
- * Associates the instance with the current Hibernate {@link org.hibernate.Session}.
- * @param entity the persistent instance to save or update
- * (to be associated with the Hibernate {@code Session})
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#saveOrUpdate(Object)
- */
- void saveOrUpdate(Object entity) throws DataAccessException;
-
- /**
- * Save or update the given persistent instance,
- * according to its id (matching the configured "unsaved-value"?).
- * Associates the instance with the current Hibernate {@code Session}.
- * @param entityName the name of the persistent entity
- * @param entity the persistent instance to save or update
- * (to be associated with the Hibernate {@code Session})
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#saveOrUpdate(String, Object)
- */
- void saveOrUpdate(String entityName, Object entity) throws DataAccessException;
-
- /**
- * Persist the state of the given detached instance according to the
- * given replication mode, reusing the current identifier value.
- * @param entity the persistent object to replicate
- * @param replicationMode the Hibernate ReplicationMode
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#replicate(Object, org.hibernate.ReplicationMode)
- */
- void replicate(Object entity, ReplicationMode replicationMode) throws DataAccessException;
-
- /**
- * Persist the state of the given detached instance according to the
- * given replication mode, reusing the current identifier value.
- * @param entityName the name of the persistent entity
- * @param entity the persistent object to replicate
- * @param replicationMode the Hibernate ReplicationMode
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#replicate(String, Object, org.hibernate.ReplicationMode)
- */
- void replicate(String entityName, Object entity, ReplicationMode replicationMode) throws DataAccessException;
-
- /**
- * Persist the given transient instance. Follows JSR-220 semantics.
- *
Similar to {@code save}, associating the given object
- * with the current Hibernate {@link org.hibernate.Session}.
- * @param entity the persistent instance to persist
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#persist(Object)
- * @see #save
- */
- void persist(Object entity) throws DataAccessException;
-
- /**
- * Persist the given transient instance. Follows JSR-220 semantics.
- *
Similar to {@code save}, associating the given object
- * with the current Hibernate {@link org.hibernate.Session}.
- * @param entityName the name of the persistent entity
- * @param entity the persistent instance to persist
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#persist(String, Object)
- * @see #save
- */
- void persist(String entityName, Object entity) throws DataAccessException;
-
- /**
- * Copy the state of the given object onto the persistent object
- * with the same identifier. Follows JSR-220 semantics.
- *
Similar to {@code saveOrUpdate}, but never associates the given
- * object with the current Hibernate Session. In case of a new entity,
- * the state will be copied over as well.
- *
Note that {@code merge} will not update the identifiers
- * in the passed-in object graph (in contrast to TopLink)! Consider
- * registering Spring's {@code IdTransferringMergeEventListener} if
- * you would like to have newly assigned ids transferred to the original
- * object graph too.
- * @param entity the object to merge with the corresponding persistence instance
- * @return the updated, registered persistent instance
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#merge(Object)
- * @see #saveOrUpdate
- * @see org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener
- */
- T merge(T entity) throws DataAccessException;
-
- /**
- * Copy the state of the given object onto the persistent object
- * with the same identifier. Follows JSR-220 semantics.
- * Similar to {@code saveOrUpdate}, but never associates the given
- * object with the current Hibernate {@link org.hibernate.Session}. In
- * the case of a new entity, the state will be copied over as well.
- *
Note that {@code merge} will not update the identifiers
- * in the passed-in object graph (in contrast to TopLink)! Consider
- * registering Spring's {@code IdTransferringMergeEventListener}
- * if you would like to have newly assigned ids transferred to the
- * original object graph too.
- * @param entityName the name of the persistent entity
- * @param entity the object to merge with the corresponding persistence instance
- * @return the updated, registered persistent instance
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#merge(String, Object)
- * @see #saveOrUpdate
- */
- T merge(String entityName, T entity) throws DataAccessException;
-
- /**
- * Delete the given persistent instance.
- * @param entity the persistent instance to delete
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#delete(Object)
- */
- void delete(Object entity) throws DataAccessException;
-
- /**
- * Delete the given persistent instance.
- * Obtains the specified lock mode if the instance exists, implicitly
- * checking whether the corresponding database entry still exists.
- * @param entity the persistent instance to delete
- * @param lockMode the lock mode to obtain
- * @throws org.springframework.orm.ObjectOptimisticLockingFailureException if not found
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#delete(Object)
- */
- void delete(Object entity, LockMode lockMode) throws DataAccessException;
-
- /**
- * Delete the given persistent instance.
- * @param entityName the name of the persistent entity
- * @param entity the persistent instance to delete
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#delete(Object)
- */
- void delete(String entityName, Object entity) throws DataAccessException;
-
- /**
- * Delete the given persistent instance.
- *
Obtains the specified lock mode if the instance exists, implicitly
- * checking whether the corresponding database entry still exists.
- * @param entityName the name of the persistent entity
- * @param entity the persistent instance to delete
- * @param lockMode the lock mode to obtain
- * @throws org.springframework.orm.ObjectOptimisticLockingFailureException if not found
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#delete(Object)
- */
- void delete(String entityName, Object entity, LockMode lockMode) throws DataAccessException;
-
- /**
- * Delete all given persistent instances.
- *
This can be combined with any of the find methods to delete by query
- * in two lines of code.
- * @param entities the persistent instances to delete
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#delete(Object)
- */
- void deleteAll(Collection> entities) throws DataAccessException;
-
- /**
- * Flush all pending saves, updates and deletes to the database.
- *
Only invoke this for selective eager flushing, for example when
- * JDBC code needs to see certain changes within the same transaction.
- * Else, it is preferable to rely on auto-flushing at transaction
- * completion.
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#flush
- */
- void flush() throws DataAccessException;
-
- /**
- * Remove all objects from the {@link org.hibernate.Session} cache, and
- * cancel all pending saves, updates and deletes.
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#clear
- */
- void clear() throws DataAccessException;
-
-
- //-------------------------------------------------------------------------
- // Convenience finder methods for HQL strings
- //-------------------------------------------------------------------------
-
- /**
- * Execute an HQL query.
- * @param queryString a query expressed in Hibernate's query language
- * @return a {@link List} containing the results of the query execution
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#createQuery
- */
- List> find(String queryString) throws DataAccessException;
-
- /**
- * Execute an HQL query, binding one value to a "?" parameter in the
- * query string.
- * @param queryString a query expressed in Hibernate's query language
- * @param value the value of the parameter
- * @return a {@link List} containing the results of the query execution
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#createQuery
- */
- List> find(String queryString, Object value) throws DataAccessException;
-
- /**
- * Execute an HQL query, binding a number of values to "?" parameters
- * in the query string.
- * @param queryString a query expressed in Hibernate's query language
- * @param values the values of the parameters
- * @return a {@link List} containing the results of the query execution
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#createQuery
- */
- List> find(String queryString, Object... values) throws DataAccessException;
-
- /**
- * Execute an HQL query, binding one value to a ":" named parameter
- * in the query string.
- * @param queryString a query expressed in Hibernate's query language
- * @param paramName the name of the parameter
- * @param value the value of the parameter
- * @return a {@link List} containing the results of the query execution
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#getNamedQuery(String)
- */
- List> findByNamedParam(String queryString, String paramName, Object value) throws DataAccessException;
-
- /**
- * Execute an HQL query, binding a number of values to ":" named
- * parameters in the query string.
- * @param queryString a query expressed in Hibernate's query language
- * @param paramNames the names of the parameters
- * @param values the values of the parameters
- * @return a {@link List} containing the results of the query execution
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#getNamedQuery(String)
- */
- List> findByNamedParam(String queryString, String[] paramNames, Object[] values) throws DataAccessException;
-
- /**
- * Execute an HQL query, binding the properties of the given bean to
- * named parameters in the query string.
- * @param queryString a query expressed in Hibernate's query language
- * @param valueBean the values of the parameters
- * @return a {@link List} containing the results of the query execution
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Query#setProperties
- * @see org.hibernate.Session#createQuery
- */
- List> findByValueBean(String queryString, Object valueBean) throws DataAccessException;
-
-
- //-------------------------------------------------------------------------
- // Convenience finder methods for named queries
- //-------------------------------------------------------------------------
-
- /**
- * Execute a named query.
- *
A named query is defined in a Hibernate mapping file.
- * @param queryName the name of a Hibernate query in a mapping file
- * @return a {@link List} containing the results of the query execution
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#getNamedQuery(String)
- */
- List> findByNamedQuery(String queryName) throws DataAccessException;
-
- /**
- * Execute a named query, binding one value to a "?" parameter in
- * the query string.
- *
A named query is defined in a Hibernate mapping file.
- * @param queryName the name of a Hibernate query in a mapping file
- * @param value the value of the parameter
- * @return a {@link List} containing the results of the query execution
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#getNamedQuery(String)
- */
- List> findByNamedQuery(String queryName, Object value) throws DataAccessException;
-
- /**
- * Execute a named query binding a number of values to "?" parameters
- * in the query string.
- *
A named query is defined in a Hibernate mapping file.
- * @param queryName the name of a Hibernate query in a mapping file
- * @param values the values of the parameters
- * @return a {@link List} containing the results of the query execution
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#getNamedQuery(String)
- */
- List> findByNamedQuery(String queryName, Object... values) throws DataAccessException;
-
- /**
- * Execute a named query, binding one value to a ":" named parameter
- * in the query string.
- *
A named query is defined in a Hibernate mapping file.
- * @param queryName the name of a Hibernate query in a mapping file
- * @param paramName the name of parameter
- * @param value the value of the parameter
- * @return a {@link List} containing the results of the query execution
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#getNamedQuery(String)
- */
- List> findByNamedQueryAndNamedParam(String queryName, String paramName, Object value)
- throws DataAccessException;
-
- /**
- * Execute a named query, binding a number of values to ":" named
- * parameters in the query string.
- *
A named query is defined in a Hibernate mapping file.
- * @param queryName the name of a Hibernate query in a mapping file
- * @param paramNames the names of the parameters
- * @param values the values of the parameters
- * @return a {@link List} containing the results of the query execution
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#getNamedQuery(String)
- */
- List> findByNamedQueryAndNamedParam(String queryName, String[] paramNames, Object[] values)
- throws DataAccessException;
-
- /**
- * Execute a named query, binding the properties of the given bean to
- * ":" named parameters in the query string.
- *
A named query is defined in a Hibernate mapping file.
- * @param queryName the name of a Hibernate query in a mapping file
- * @param valueBean the values of the parameters
- * @return a {@link List} containing the results of the query execution
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Query#setProperties
- * @see org.hibernate.Session#getNamedQuery(String)
- */
- List> findByNamedQueryAndValueBean(String queryName, Object valueBean) throws DataAccessException;
-
-
- //-------------------------------------------------------------------------
- // Convenience finder methods for detached criteria
- //-------------------------------------------------------------------------
-
- /**
- * Execute a query based on a given Hibernate criteria object.
- * @param criteria the detached Hibernate criteria object.
- * Note: Do not reuse criteria objects! They need to recreated per execution,
- * due to the suboptimal design of Hibernate's criteria facility.
- * @return a {@link List} containing 0 or more persistent instances
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.criterion.DetachedCriteria#getExecutableCriteria(org.hibernate.Session)
- */
- List> findByCriteria(DetachedCriteria criteria) throws DataAccessException;
-
- /**
- * Execute a query based on the given Hibernate criteria object.
- * @param criteria the detached Hibernate criteria object.
- * Note: Do not reuse criteria objects! They need to recreated per execution,
- * due to the suboptimal design of Hibernate's criteria facility.
- * @param firstResult the index of the first result object to be retrieved
- * (numbered from 0)
- * @param maxResults the maximum number of result objects to retrieve
- * (or <=0 for no limit)
- * @return a {@link List} containing 0 or more persistent instances
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.criterion.DetachedCriteria#getExecutableCriteria(org.hibernate.Session)
- * @see org.hibernate.Criteria#setFirstResult(int)
- * @see org.hibernate.Criteria#setMaxResults(int)
- */
- List> findByCriteria(DetachedCriteria criteria, int firstResult, int maxResults) throws DataAccessException;
-
- /**
- * Execute a query based on the given example entity object.
- * @param exampleEntity an instance of the desired entity,
- * serving as example for "query-by-example"
- * @return a {@link List} containing 0 or more persistent instances
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.criterion.Example#create(Object)
- */
- List findByExample(T exampleEntity) throws DataAccessException;
-
- /**
- * Execute a query based on the given example entity object.
- * @param entityName the name of the persistent entity
- * @param exampleEntity an instance of the desired entity,
- * serving as example for "query-by-example"
- * @return a {@link List} containing 0 or more persistent instances
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.criterion.Example#create(Object)
- */
- List findByExample(String entityName, T exampleEntity) throws DataAccessException;
-
- /**
- * Execute a query based on a given example entity object.
- * @param exampleEntity an instance of the desired entity,
- * serving as example for "query-by-example"
- * @param firstResult the index of the first result object to be retrieved
- * (numbered from 0)
- * @param maxResults the maximum number of result objects to retrieve
- * (or <=0 for no limit)
- * @return a {@link List} containing 0 or more persistent instances
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.criterion.Example#create(Object)
- * @see org.hibernate.Criteria#setFirstResult(int)
- * @see org.hibernate.Criteria#setMaxResults(int)
- */
- List findByExample(T exampleEntity, int firstResult, int maxResults) throws DataAccessException;
-
- /**
- * Execute a query based on a given example entity object.
- * @param entityName the name of the persistent entity
- * @param exampleEntity an instance of the desired entity,
- * serving as example for "query-by-example"
- * @param firstResult the index of the first result object to be retrieved
- * (numbered from 0)
- * @param maxResults the maximum number of result objects to retrieve
- * (or <=0 for no limit)
- * @return a {@link List} containing 0 or more persistent instances
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.criterion.Example#create(Object)
- * @see org.hibernate.Criteria#setFirstResult(int)
- * @see org.hibernate.Criteria#setMaxResults(int)
- */
- List findByExample(String entityName, T exampleEntity, int firstResult, int maxResults)
- throws DataAccessException;
-
-
- //-------------------------------------------------------------------------
- // Convenience query methods for iteration and bulk updates/deletes
- //-------------------------------------------------------------------------
-
- /**
- * Execute a query for persistent instances.
- * Returns the results as an {@link Iterator}. Entities returned are
- * initialized on demand. See the Hibernate API documentation for details.
- * @param queryString a query expressed in Hibernate's query language
- * @return an {@link Iterator} containing 0 or more persistent instances
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#createQuery
- * @see org.hibernate.Query#iterate
- */
- Iterator> iterate(String queryString) throws DataAccessException;
-
- /**
- * Execute a query for persistent instances, binding one value
- * to a "?" parameter in the query string.
- *
Returns the results as an {@link Iterator}. Entities returned are
- * initialized on demand. See the Hibernate API documentation for details.
- * @param queryString a query expressed in Hibernate's query language
- * @param value the value of the parameter
- * @return an {@link Iterator} containing 0 or more persistent instances
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#createQuery
- * @see org.hibernate.Query#iterate
- */
- Iterator> iterate(String queryString, Object value) throws DataAccessException;
-
- /**
- * Execute a query for persistent instances, binding a number of
- * values to "?" parameters in the query string.
- *
Returns the results as an {@link Iterator}. Entities returned are
- * initialized on demand. See the Hibernate API documentation for details.
- * @param queryString a query expressed in Hibernate's query language
- * @param values the values of the parameters
- * @return an {@link Iterator} containing 0 or more persistent instances
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#createQuery
- * @see org.hibernate.Query#iterate
- */
- Iterator> iterate(String queryString, Object... values) throws DataAccessException;
-
- /**
- * Immediately close an {@link Iterator} created by any of the various
- * {@code iterate(..)} operations, instead of waiting until the
- * session is closed or disconnected.
- * @param it the {@code Iterator} to close
- * @throws DataAccessException if the {@code Iterator} could not be closed
- * @see org.hibernate.Hibernate#close
- */
- void closeIterator(Iterator> it) throws DataAccessException;
-
- /**
- * Update/delete all objects according to the given query.
- * @param queryString an update/delete query expressed in Hibernate's query language
- * @return the number of instances updated/deleted
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#createQuery
- * @see org.hibernate.Query#executeUpdate
- */
- int bulkUpdate(String queryString) throws DataAccessException;
-
- /**
- * Update/delete all objects according to the given query, binding one value
- * to a "?" parameter in the query string.
- * @param queryString an update/delete query expressed in Hibernate's query language
- * @param value the value of the parameter
- * @return the number of instances updated/deleted
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#createQuery
- * @see org.hibernate.Query#executeUpdate
- */
- int bulkUpdate(String queryString, Object value) throws DataAccessException;
-
- /**
- * Update/delete all objects according to the given query, binding a number of
- * values to "?" parameters in the query string.
- * @param queryString an update/delete query expressed in Hibernate's query language
- * @param values the values of the parameters
- * @return the number of instances updated/deleted
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#createQuery
- * @see org.hibernate.Query#executeUpdate
- */
- int bulkUpdate(String queryString, Object... values) throws DataAccessException;
-
-}
diff --git a/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateOptimisticLockingFailureException.java b/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateOptimisticLockingFailureException.java
deleted file mode 100644
index 47ff23a281..0000000000
--- a/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateOptimisticLockingFailureException.java
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * Copyright 2002-2013 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate3;
-
-import org.hibernate.OptimisticLockException;
-import org.hibernate.StaleObjectStateException;
-import org.hibernate.StaleStateException;
-
-import org.springframework.orm.ObjectOptimisticLockingFailureException;
-
-/**
- * Hibernate-specific subclass of ObjectOptimisticLockingFailureException.
- * Converts Hibernate's StaleObjectStateException, StaleStateException
- * and OptimisticLockException.
- *
- * @author Juergen Hoeller
- * @since 1.2
- * @see SessionFactoryUtils#convertHibernateAccessException
- * @deprecated as of Spring 4.3, in favor of Hibernate 4.x/5.x
- */
-@Deprecated
-@SuppressWarnings("serial")
-public class HibernateOptimisticLockingFailureException extends ObjectOptimisticLockingFailureException {
-
- public HibernateOptimisticLockingFailureException(StaleObjectStateException ex) {
- super(ex.getEntityName(), ex.getIdentifier(), ex);
- }
-
- public HibernateOptimisticLockingFailureException(StaleStateException ex) {
- super(ex.getMessage(), ex);
- }
-
- public HibernateOptimisticLockingFailureException(OptimisticLockException ex) {
- super(ex.getMessage(), ex);
- }
-
-}
diff --git a/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateQueryException.java b/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateQueryException.java
deleted file mode 100644
index 81b82ffff5..0000000000
--- a/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateQueryException.java
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate3;
-
-import org.hibernate.QueryException;
-
-import org.springframework.dao.InvalidDataAccessResourceUsageException;
-
-/**
- * Hibernate-specific subclass of InvalidDataAccessResourceUsageException,
- * thrown on invalid HQL query syntax.
- *
- * @author Juergen Hoeller
- * @since 1.2
- * @see SessionFactoryUtils#convertHibernateAccessException
- * @deprecated as of Spring 4.3, in favor of Hibernate 4.x/5.x
- */
-@Deprecated
-@SuppressWarnings("serial")
-public class HibernateQueryException extends InvalidDataAccessResourceUsageException {
-
- public HibernateQueryException(QueryException ex) {
- super(ex.getMessage(), ex);
- }
-
- /**
- * Return the HQL query string that was invalid.
- */
- public String getQueryString() {
- return ((QueryException) getCause()).getQueryString();
- }
-
-}
diff --git a/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateSystemException.java b/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateSystemException.java
deleted file mode 100644
index ddc5a2e17f..0000000000
--- a/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateSystemException.java
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate3;
-
-import org.hibernate.HibernateException;
-
-import org.springframework.dao.UncategorizedDataAccessException;
-
-/**
- * Hibernate-specific subclass of UncategorizedDataAccessException,
- * for Hibernate system errors that do not match any concrete
- * {@code org.springframework.dao} exceptions.
- *
- * @author Juergen Hoeller
- * @since 1.2
- * @see SessionFactoryUtils#convertHibernateAccessException
- * @deprecated as of Spring 4.3, in favor of Hibernate 4.x/5.x
- */
-@Deprecated
-@SuppressWarnings("serial")
-public class HibernateSystemException extends UncategorizedDataAccessException {
-
- /**
- * Create a new HibernateSystemException,
- * wrapping an arbitrary HibernateException.
- * @param cause the HibernateException thrown
- */
- public HibernateSystemException(HibernateException cause) {
- super(cause != null ? cause.getMessage() : null, cause);
- }
-
-}
diff --git a/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateTemplate.java b/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateTemplate.java
deleted file mode 100644
index 1c1d0ad562..0000000000
--- a/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateTemplate.java
+++ /dev/null
@@ -1,1398 +0,0 @@
-/*
- * Copyright 2002-2015 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate3;
-
-import java.io.Serializable;
-import java.lang.reflect.InvocationHandler;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.lang.reflect.Proxy;
-import java.sql.SQLException;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.List;
-
-import org.hibernate.Criteria;
-import org.hibernate.Filter;
-import org.hibernate.FlushMode;
-import org.hibernate.Hibernate;
-import org.hibernate.HibernateException;
-import org.hibernate.LockMode;
-import org.hibernate.Query;
-import org.hibernate.ReplicationMode;
-import org.hibernate.Session;
-import org.hibernate.SessionFactory;
-import org.hibernate.criterion.DetachedCriteria;
-import org.hibernate.criterion.Example;
-import org.hibernate.engine.SessionImplementor;
-import org.hibernate.event.EventSource;
-
-import org.springframework.dao.DataAccessException;
-import org.springframework.dao.DataAccessResourceFailureException;
-import org.springframework.dao.InvalidDataAccessApiUsageException;
-import org.springframework.util.Assert;
-
-/**
- * Helper class that simplifies Hibernate data access code. Automatically
- * converts HibernateExceptions into DataAccessExceptions, following the
- * {@code org.springframework.dao} exception hierarchy.
- *
- *
The central method is {@code execute}, supporting Hibernate access code
- * implementing the {@link HibernateCallback} interface. It provides Hibernate Session
- * handling such that neither the HibernateCallback implementation nor the calling
- * code needs to explicitly care about retrieving/closing Hibernate Sessions,
- * or handling Session lifecycle exceptions. For typical single step actions,
- * there are various convenience methods (find, load, saveOrUpdate, delete).
- *
- *
Can be used within a service implementation via direct instantiation
- * with a SessionFactory reference, or get prepared in an application context
- * and given to services as bean reference. Note: The SessionFactory should
- * always be configured as bean in the application context, in the first case
- * given to the service directly, in the second case to the prepared template.
- *
- *
NOTE: As of Hibernate 3.0.1, transactional Hibernate access code can
- * also be coded in plain Hibernate style. Hence, for newly started projects,
- * consider adopting the standard Hibernate3 style of coding data access objects
- * instead, based on {@link org.hibernate.SessionFactory#getCurrentSession()}.
- *
- *
This class can be considered as direct alternative to working with the raw
- * Hibernate3 Session API (through {@code SessionFactory.getCurrentSession()}).
- * The major advantage is its automatic conversion to DataAccessExceptions as well
- * as its capability to fall back to 'auto-commit' style behavior when used outside
- * of transactions. Note that HibernateTemplate will perform its own Session
- * management, not participating in a custom Hibernate CurrentSessionContext
- * unless you explicitly switch {@link #setAllowCreate "allowCreate"} to "false".
- *
- *
{@link LocalSessionFactoryBean} is the preferred way of obtaining a reference
- * to a specific Hibernate SessionFactory, at least in a non-EJB environment.
- * The Spring application context will manage its lifecycle, initializing and
- * shutting down the factory as part of the application.
- *
- *
Note that operations that return an Iterator (i.e. {@code iterate})
- * are supposed to be used within Spring-driven or JTA-driven transactions
- * (with HibernateTransactionManager, JtaTransactionManager, or EJB CMT).
- * Else, the Iterator won't be able to read results from its ResultSet anymore,
- * as the underlying Hibernate Session will already have been closed.
- *
- *
Lazy loading will also just work with an open Hibernate Session,
- * either within a transaction or within OpenSessionInViewFilter/Interceptor.
- * Furthermore, some operations just make sense within transactions,
- * for example: {@code contains}, {@code evict}, {@code lock},
- * {@code flush}, {@code clear}.
- *
- * @author Juergen Hoeller
- * @since 1.2
- * @see #setSessionFactory
- * @see HibernateCallback
- * @see org.hibernate.Session
- * @see LocalSessionFactoryBean
- * @see HibernateTransactionManager
- * @see org.springframework.transaction.jta.JtaTransactionManager
- * @see org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
- * @see org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor
- * @deprecated as of Spring 4.3, in favor of Hibernate 4.x/5.x
- */
-@Deprecated
-public class HibernateTemplate extends HibernateAccessor implements HibernateOperations {
-
- private boolean allowCreate = true;
-
- private boolean alwaysUseNewSession = false;
-
- private boolean exposeNativeSession = false;
-
- private boolean checkWriteOperations = true;
-
- private boolean cacheQueries = false;
-
- private String queryCacheRegion;
-
- private int fetchSize = 0;
-
- private int maxResults = 0;
-
-
- /**
- * Create a new HibernateTemplate instance.
- */
- public HibernateTemplate() {
- }
-
- /**
- * Create a new HibernateTemplate instance.
- * @param sessionFactory the SessionFactory to create Sessions with
- */
- public HibernateTemplate(SessionFactory sessionFactory) {
- setSessionFactory(sessionFactory);
- afterPropertiesSet();
- }
-
- /**
- * Create a new HibernateTemplate instance.
- * @param sessionFactory the SessionFactory to create Sessions with
- * @param allowCreate if a non-transactional Session should be created when no
- * transactional Session can be found for the current thread
- */
- public HibernateTemplate(SessionFactory sessionFactory, boolean allowCreate) {
- setSessionFactory(sessionFactory);
- setAllowCreate(allowCreate);
- afterPropertiesSet();
- }
-
-
- /**
- * Set if a new {@link Session} should be created when no transactional
- * {@code Session} can be found for the current thread.
- * The default value is {@code true}.
- *
{@code HibernateTemplate} is aware of a corresponding
- * {@code Session} bound to the current thread, for example when using
- * {@link HibernateTransactionManager}. If {@code allowCreate} is
- * {@code true}, a new non-transactional {@code Session} will be
- * created if none is found, which needs to be closed at the end of the operation.
- * If {@code false}, an {@link IllegalStateException} will get thrown in
- * this case.
- *
NOTE: As of Spring 2.5, switching {@code allowCreate}
- * to {@code false} will delegate to Hibernate's
- * {@link org.hibernate.SessionFactory#getCurrentSession()} method,
- * which - with Spring-based setup - will by default delegate to Spring's
- * {@code SessionFactoryUtils.getSession(sessionFactory, false)}.
- * This mode also allows for custom Hibernate CurrentSessionContext strategies
- * to be plugged in, whereas {@code allowCreate} set to {@code true}
- * will always use a Spring-managed Hibernate Session.
- * @see SessionFactoryUtils#getSession(SessionFactory, boolean)
- */
- public void setAllowCreate(boolean allowCreate) {
- this.allowCreate = allowCreate;
- }
-
- /**
- * Return if a new Session should be created if no thread-bound found.
- */
- public boolean isAllowCreate() {
- return this.allowCreate;
- }
-
- /**
- * Set whether to always use a new Hibernate Session for this template.
- * Default is "false"; if activated, all operations on this template will
- * work on a new Hibernate Session even in case of a pre-bound Session
- * (for example, within a transaction or OpenSessionInViewFilter).
- *
Within a transaction, a new Hibernate Session used by this template
- * will participate in the transaction through using the same JDBC
- * Connection. In such a scenario, multiple Sessions will participate
- * in the same database transaction.
- *
Turn this on for operations that are supposed to always execute
- * independently, without side effects caused by a shared Hibernate Session.
- */
- public void setAlwaysUseNewSession(boolean alwaysUseNewSession) {
- this.alwaysUseNewSession = alwaysUseNewSession;
- }
-
- /**
- * Return whether to always use a new Hibernate Session for this template.
- */
- public boolean isAlwaysUseNewSession() {
- return this.alwaysUseNewSession;
- }
-
- /**
- * Set whether to expose the native Hibernate Session to
- * HibernateCallback code.
- *
Default is "false": a Session proxy will be returned, suppressing
- * {@code close} calls and automatically applying query cache
- * settings and transaction timeouts.
- * @see HibernateCallback
- * @see org.hibernate.Session
- * @see #setCacheQueries
- * @see #setQueryCacheRegion
- * @see #prepareQuery
- * @see #prepareCriteria
- */
- public void setExposeNativeSession(boolean exposeNativeSession) {
- this.exposeNativeSession = exposeNativeSession;
- }
-
- /**
- * Return whether to expose the native Hibernate Session to
- * HibernateCallback code, or rather a Session proxy.
- */
- public boolean isExposeNativeSession() {
- return this.exposeNativeSession;
- }
-
- /**
- * Set whether to check that the Hibernate Session is not in read-only mode
- * in case of write operations (save/update/delete).
- *
Default is "true", for fail-fast behavior when attempting write operations
- * within a read-only transaction. Turn this off to allow save/update/delete
- * on a Session with flush mode NEVER.
- * @see #setFlushMode
- * @see #checkWriteOperationAllowed
- * @see org.springframework.transaction.TransactionDefinition#isReadOnly
- */
- public void setCheckWriteOperations(boolean checkWriteOperations) {
- this.checkWriteOperations = checkWriteOperations;
- }
-
- /**
- * Return whether to check that the Hibernate Session is not in read-only
- * mode in case of write operations (save/update/delete).
- */
- public boolean isCheckWriteOperations() {
- return this.checkWriteOperations;
- }
-
- /**
- * Set whether to cache all queries executed by this template.
- *
If this is "true", all Query and Criteria objects created by
- * this template will be marked as cacheable (including all
- * queries through find methods).
- *
To specify the query region to be used for queries cached
- * by this template, set the "queryCacheRegion" property.
- * @see #setQueryCacheRegion
- * @see org.hibernate.Query#setCacheable
- * @see org.hibernate.Criteria#setCacheable
- */
- public void setCacheQueries(boolean cacheQueries) {
- this.cacheQueries = cacheQueries;
- }
-
- /**
- * Return whether to cache all queries executed by this template.
- */
- public boolean isCacheQueries() {
- return this.cacheQueries;
- }
-
- /**
- * Set the name of the cache region for queries executed by this template.
- *
If this is specified, it will be applied to all Query and Criteria objects
- * created by this template (including all queries through find methods).
- *
The cache region will not take effect unless queries created by this
- * template are configured to be cached via the "cacheQueries" property.
- * @see #setCacheQueries
- * @see org.hibernate.Query#setCacheRegion
- * @see org.hibernate.Criteria#setCacheRegion
- */
- public void setQueryCacheRegion(String queryCacheRegion) {
- this.queryCacheRegion = queryCacheRegion;
- }
-
- /**
- * Return the name of the cache region for queries executed by this template.
- */
- public String getQueryCacheRegion() {
- return this.queryCacheRegion;
- }
-
- /**
- * Set the fetch size for this HibernateTemplate. This is important for processing
- * large result sets: Setting this higher than the default value will increase
- * processing speed at the cost of memory consumption; setting this lower can
- * avoid transferring row data that will never be read by the application.
- *
Default is 0, indicating to use the JDBC driver's default.
- */
- public void setFetchSize(int fetchSize) {
- this.fetchSize = fetchSize;
- }
-
- /**
- * Return the fetch size specified for this HibernateTemplate.
- */
- public int getFetchSize() {
- return this.fetchSize;
- }
-
- /**
- * Set the maximum number of rows for this HibernateTemplate. This is important
- * for processing subsets of large result sets, avoiding to read and hold
- * the entire result set in the database or in the JDBC driver if we're
- * never interested in the entire result in the first place (for example,
- * when performing searches that might return a large number of matches).
- *
Default is 0, indicating to use the JDBC driver's default.
- */
- public void setMaxResults(int maxResults) {
- this.maxResults = maxResults;
- }
-
- /**
- * Return the maximum number of rows specified for this HibernateTemplate.
- */
- public int getMaxResults() {
- return this.maxResults;
- }
-
-
- @Override
- public T execute(HibernateCallback action) throws DataAccessException {
- return doExecute(action, false, false);
- }
-
- @Override
- @Deprecated
- public List> executeFind(HibernateCallback> action) throws DataAccessException {
- Object result = doExecute(action, false, false);
- if (result != null && !(result instanceof List)) {
- throw new InvalidDataAccessApiUsageException(
- "Result object returned from HibernateCallback isn't a List: [" + result + "]");
- }
- return (List>) result;
- }
-
- /**
- * Execute the action specified by the given action object within a
- * new {@link org.hibernate.Session}.
- * This execute variant overrides the template-wide
- * {@link #isAlwaysUseNewSession() "alwaysUseNewSession"} setting.
- * @param action callback object that specifies the Hibernate action
- * @return a result object returned by the action, or {@code null}
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- */
- public T executeWithNewSession(HibernateCallback action) {
- return doExecute(action, true, false);
- }
-
- /**
- * Execute the action specified by the given action object within a
- * native {@link org.hibernate.Session}.
- * This execute variant overrides the template-wide
- * {@link #isExposeNativeSession() "exposeNativeSession"} setting.
- * @param action callback object that specifies the Hibernate action
- * @return a result object returned by the action, or {@code null}
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- */
- public T executeWithNativeSession(HibernateCallback action) {
- return doExecute(action, false, true);
- }
-
- /**
- * Execute the action specified by the given action object within a Session.
- * @param action callback object that specifies the Hibernate action
- * @param enforceNewSession whether to enforce a new Session for this template
- * even if there is a pre-bound transactional Session
- * @param enforceNativeSession whether to enforce exposure of the native
- * Hibernate Session to callback code
- * @return a result object returned by the action, or {@code null}
- * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
- */
- protected