16
16
17
17
package org .springframework .orm .jpa .hibernate ;
18
18
19
+ import javax .persistence .EntityManager ;
20
+
19
21
import org .hibernate .Session ;
20
22
import org .hibernate .SessionFactory ;
23
+ import org .hibernate .jpa .HibernateEntityManager ;
24
+ import org .hibernate .jpa .HibernateEntityManagerFactory ;
21
25
import org .junit .Test ;
22
26
27
+ import org .springframework .aop .framework .ProxyFactory ;
28
+ import org .springframework .aop .target .SingletonTargetSource ;
23
29
import org .springframework .orm .jpa .AbstractContainerEntityManagerFactoryIntegrationTests ;
24
30
import org .springframework .orm .jpa .EntityManagerFactoryInfo ;
25
31
import org .springframework .orm .jpa .EntityManagerProxy ;
32
38
* @author Juergen Hoeller
33
39
* @author Rod Johnson
34
40
*/
41
+ @ SuppressWarnings ("deprecation" )
35
42
public class HibernateEntityManagerFactoryIntegrationTests extends AbstractContainerEntityManagerFactoryIntegrationTests {
36
43
37
44
@ Override
@@ -43,12 +50,25 @@ protected String[] getConfigLocations() {
43
50
@ Test
44
51
public void testCanCastNativeEntityManagerFactoryToHibernateEntityManagerFactoryImpl () {
45
52
EntityManagerFactoryInfo emfi = (EntityManagerFactoryInfo ) entityManagerFactory ;
53
+ assertTrue (emfi .getNativeEntityManagerFactory () instanceof HibernateEntityManagerFactory );
46
54
assertTrue (emfi .getNativeEntityManagerFactory () instanceof SessionFactory ); // as of Hibernate 5.2
47
55
}
48
56
49
57
@ Test
50
58
public void testCanCastSharedEntityManagerProxyToHibernateEntityManager () {
59
+ assertTrue (sharedEntityManager instanceof HibernateEntityManager );
51
60
assertTrue (((EntityManagerProxy ) sharedEntityManager ).getTargetEntityManager () instanceof Session ); // as of Hibernate 5.2
52
61
}
53
62
63
+ @ Test
64
+ public void testCanUnwrapAopProxy () {
65
+ EntityManager em = entityManagerFactory .createEntityManager ();
66
+ EntityManager proxy = ProxyFactory .getProxy (EntityManager .class , new SingletonTargetSource (em ));
67
+ assertTrue (em instanceof HibernateEntityManager );
68
+ assertFalse (proxy instanceof HibernateEntityManager );
69
+ assertTrue (proxy .unwrap (HibernateEntityManager .class ) instanceof HibernateEntityManager );
70
+ assertSame (em , proxy .unwrap (HibernateEntityManager .class ));
71
+ assertSame (em .getDelegate (), proxy .getDelegate ());
72
+ }
73
+
54
74
}
0 commit comments