Shared EntityManager Query unwrapping before passing it to addNamedQuery

Issue: SPR-13957
(cherry picked from commit 7fcb277)
This commit is contained in:
Juergen Hoeller 2016-02-18 16:19:09 +01:00
parent 659e44146c
commit 4320161172
2 changed files with 24 additions and 2 deletions

View File

@ -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");
* you may not use this file except in compliance with the License.
@ -33,6 +33,7 @@ import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceException;
import javax.persistence.Query;
import javax.persistence.spi.PersistenceProvider;
import javax.persistence.spi.PersistenceUnitInfo;
import javax.sql.DataSource;
@ -384,6 +385,24 @@ public abstract class AbstractEntityManagerFactoryBean implements
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
Object retVal = method.invoke(this.nativeEntityManagerFactory, args);
if (retVal instanceof EntityManager) {

View File

@ -355,7 +355,10 @@ public abstract class SharedEntityManagerCreator {
else if (method.getName().equals("unwrap")) {
// Handle JPA 2.0 unwrap method - could be a proxy match.
Class<?> targetClass = (Class<?>) args[0];
if (targetClass == null || targetClass.isInstance(proxy)) {
if (targetClass == null) {
return this.target;
}
else if (targetClass.isInstance(proxy)) {
return proxy;
}
}