Browse Source

Consistent behavior for unwrap(null) on JPA and Bean Validation wrappers

Includes a fix for Query unwrapping before passing it to addNamedQuery.

Issue: SPR-13960
Issue: SPR-13957
pull/970/head
Juergen Hoeller 10 years ago
parent
commit
7fcb277de9
  1. 13
      spring-context/src/main/java/org/springframework/validation/beanvalidation/SpringValidatorAdapter.java
  2. 26
      spring-orm/src/main/java/org/springframework/orm/jpa/AbstractEntityManagerFactoryBean.java
  3. 7
      spring-orm/src/main/java/org/springframework/orm/jpa/ExtendedEntityManagerCreator.java
  4. 11
      spring-orm/src/main/java/org/springframework/orm/jpa/SharedEntityManagerCreator.java

13
spring-context/src/main/java/org/springframework/validation/beanvalidation/SpringValidatorAdapter.java

@ -253,13 +253,13 @@ public class SpringValidatorAdapter implements SmartValidator, javax.validation.
@Override @Override
public <T> Set<ConstraintViolation<T>> validate(T object, Class<?>... groups) { public <T> Set<ConstraintViolation<T>> validate(T object, Class<?>... groups) {
Assert.notNull(this.targetValidator, "No target Validator set"); Assert.state(this.targetValidator != null, "No target Validator set");
return this.targetValidator.validate(object, groups); return this.targetValidator.validate(object, groups);
} }
@Override @Override
public <T> Set<ConstraintViolation<T>> validateProperty(T object, String propertyName, Class<?>... groups) { public <T> Set<ConstraintViolation<T>> validateProperty(T object, String propertyName, Class<?>... groups) {
Assert.notNull(this.targetValidator, "No target Validator set"); Assert.state(this.targetValidator != null, "No target Validator set");
return this.targetValidator.validateProperty(object, propertyName, groups); return this.targetValidator.validateProperty(object, propertyName, groups);
} }
@ -267,20 +267,21 @@ public class SpringValidatorAdapter implements SmartValidator, javax.validation.
public <T> Set<ConstraintViolation<T>> validateValue( public <T> Set<ConstraintViolation<T>> validateValue(
Class<T> beanType, String propertyName, Object value, Class<?>... groups) { Class<T> beanType, String propertyName, Object value, Class<?>... groups) {
Assert.notNull(this.targetValidator, "No target Validator set"); Assert.state(this.targetValidator != null, "No target Validator set");
return this.targetValidator.validateValue(beanType, propertyName, value, groups); return this.targetValidator.validateValue(beanType, propertyName, value, groups);
} }
@Override @Override
public BeanDescriptor getConstraintsForClass(Class<?> clazz) { public BeanDescriptor getConstraintsForClass(Class<?> clazz) {
Assert.notNull(this.targetValidator, "No target Validator set"); Assert.state(this.targetValidator != null, "No target Validator set");
return this.targetValidator.getConstraintsForClass(clazz); return this.targetValidator.getConstraintsForClass(clazz);
} }
@Override @Override
@SuppressWarnings("unchecked")
public <T> T unwrap(Class<T> type) { public <T> T unwrap(Class<T> type) {
Assert.notNull(this.targetValidator, "No target Validator set"); Assert.state(this.targetValidator != null, "No target Validator set");
return this.targetValidator.unwrap(type); return (type != null ? this.targetValidator.unwrap(type) : (T) this.targetValidator);
} }

26
spring-orm/src/main/java/org/springframework/orm/jpa/AbstractEntityManagerFactoryBean.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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -36,6 +36,7 @@ import java.util.concurrent.Future;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory; import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceException; import javax.persistence.PersistenceException;
import javax.persistence.Query;
import javax.persistence.spi.PersistenceProvider; import javax.persistence.spi.PersistenceProvider;
import javax.persistence.spi.PersistenceUnitInfo; import javax.persistence.spi.PersistenceUnitInfo;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -439,6 +440,24 @@ public abstract class AbstractEntityManagerFactoryBean implements
return ExtendedEntityManagerCreator.createApplicationManagedEntityManager(rawEntityManager, this, true); return ExtendedEntityManagerCreator.createApplicationManagedEntityManager(rawEntityManager, this, true);
} }
// Look for Query arguments, primarily JPA 2.1's addNamedQuery(String, Query)
if (args != null) {
for (int i = 0; i < args.length; i++) {
Object arg = args[i];
if (arg instanceof Query && Proxy.isProxyClass(arg.getClass())) {
// Assumably a Spring-generated proxy from SharedEntityManagerCreator:
// since we're passing it back to the native EntityManagerFactory,
// let's unwrap it to the original Query object from the provider.
try {
args[i] = ((Query) arg).unwrap(null);
}
catch (RuntimeException ex) {
// Ignore - simply proceed with given Query object then
}
}
}
}
// Standard delegation to the native factory, just post-processing EntityManager return values // Standard delegation to the native factory, just post-processing EntityManager return values
Object retVal = method.invoke(getNativeEntityManagerFactory(), args); Object retVal = method.invoke(getNativeEntityManagerFactory(), args);
if (retVal instanceof EntityManager) { if (retVal instanceof EntityManager) {
@ -604,7 +623,10 @@ public abstract class AbstractEntityManagerFactoryBean implements
else if (method.getName().equals("unwrap")) { else if (method.getName().equals("unwrap")) {
// Handle JPA 2.1 unwrap method - could be a proxy match. // Handle JPA 2.1 unwrap method - could be a proxy match.
Class<?> targetClass = (Class<?>) args[0]; Class<?> targetClass = (Class<?>) args[0];
if (targetClass == null || targetClass.isInstance(proxy)) { if (targetClass == null) {
return this.entityManagerFactoryBean.getNativeEntityManagerFactory();
}
else if (targetClass.isInstance(proxy)) {
return proxy; return proxy;
} }
} }

7
spring-orm/src/main/java/org/springframework/orm/jpa/ExtendedEntityManagerCreator.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -296,7 +296,10 @@ public abstract class ExtendedEntityManagerCreator {
else if (method.getName().equals("unwrap")) { else if (method.getName().equals("unwrap")) {
// Handle JPA 2.0 unwrap method - could be a proxy match. // Handle JPA 2.0 unwrap method - could be a proxy match.
Class<?> targetClass = (Class<?>) args[0]; Class<?> targetClass = (Class<?>) args[0];
if (targetClass == null || targetClass.isInstance(proxy)) { if (targetClass == null) {
return this.target;
}
else if (targetClass.isInstance(proxy)) {
return proxy; return proxy;
} }
} }

11
spring-orm/src/main/java/org/springframework/orm/jpa/SharedEntityManagerCreator.java

@ -232,7 +232,7 @@ public abstract class SharedEntityManagerCreator {
else if (method.getName().equals("unwrap")) { else if (method.getName().equals("unwrap")) {
// JPA 2.0: handle unwrap method - could be a proxy match. // JPA 2.0: handle unwrap method - could be a proxy match.
Class<?> targetClass = (Class<?>) args[0]; Class<?> targetClass = (Class<?>) args[0];
if (targetClass == null || targetClass.isInstance(proxy)) { if (targetClass != null && targetClass.isInstance(proxy)) {
return proxy; return proxy;
} }
} }
@ -263,6 +263,10 @@ public abstract class SharedEntityManagerCreator {
return target; return target;
} }
else if (method.getName().equals("unwrap")) { else if (method.getName().equals("unwrap")) {
Class<?> targetClass = (Class<?>) args[0];
if (targetClass == null) {
return (target != null ? target : proxy);
}
// We need a transactional target now. // We need a transactional target now.
if (target == null) { if (target == null) {
throw new IllegalStateException("No transactional EntityManager available"); throw new IllegalStateException("No transactional EntityManager available");
@ -355,7 +359,10 @@ public abstract class SharedEntityManagerCreator {
else if (method.getName().equals("unwrap")) { else if (method.getName().equals("unwrap")) {
// Handle JPA 2.0 unwrap method - could be a proxy match. // Handle JPA 2.0 unwrap method - could be a proxy match.
Class<?> targetClass = (Class<?>) args[0]; Class<?> targetClass = (Class<?>) args[0];
if (targetClass == null || targetClass.isInstance(proxy)) { if (targetClass == null) {
return this.target;
}
else if (targetClass.isInstance(proxy)) {
return proxy; return proxy;
} }
} }

Loading…
Cancel
Save