Skip to content

Commit 198e17f

Browse files
committed
Add AbstractEntityManagerFactoryBean proxy hints for Hibernate
Closes gh-29138
1 parent 948c9b8 commit 198e17f

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.orm.jpa;
18+
19+
import org.springframework.aot.hint.RuntimeHints;
20+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
21+
import org.springframework.aot.hint.TypeReference;
22+
import org.springframework.util.ClassUtils;
23+
24+
/**
25+
* {@link RuntimeHintsRegistrar} implementation that makes sure JDK proxy hints related to
26+
* {@link AbstractEntityManagerFactoryBean} are registered.
27+
*
28+
* @author Sebastien Deleuze
29+
* @since 6.0
30+
*/
31+
class EntityManagerRuntimeHints implements RuntimeHintsRegistrar {
32+
33+
private static final String HIBERNATE_SESSION_FACTORY_CLASS_NAME = "org.hibernate.SessionFactory";
34+
35+
@Override
36+
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
37+
if (ClassUtils.isPresent(HIBERNATE_SESSION_FACTORY_CLASS_NAME, classLoader)) {
38+
hints.proxies().registerJdkProxy(TypeReference.of(HIBERNATE_SESSION_FACTORY_CLASS_NAME),
39+
TypeReference.of(EntityManagerFactoryInfo.class));
40+
hints.proxies().registerJdkProxy(TypeReference.of("org.hibernate.Session"),
41+
TypeReference.of(EntityManagerProxy.class));
42+
}
43+
}
44+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
org.springframework.beans.factory.aot.BeanRegistrationAotProcessor=\
22
org.springframework.orm.jpa.persistenceunit.PersistenceManagedTypesBeanRegistrationAotProcessor
3+
4+
org.springframework.aot.hint.RuntimeHintsRegistrar=\
5+
org.springframework.orm.jpa.EntityManagerRuntimeHints
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.orm.jpa;
18+
19+
import org.hibernate.Session;
20+
import org.hibernate.SessionFactory;
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Test;
23+
24+
import org.springframework.aot.hint.RuntimeHints;
25+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
26+
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
27+
import org.springframework.beans.factory.aot.AotServices;
28+
import org.springframework.util.ClassUtils;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
32+
/**
33+
* Tests for {@link EntityManagerRuntimeHints}.
34+
*
35+
* @author Sebastien Deleuze
36+
*/
37+
class EntityManagerRuntimeHintsTests {
38+
39+
private final RuntimeHints hints = new RuntimeHints();
40+
41+
@BeforeEach
42+
void setup() {
43+
AotServices.factories().load(RuntimeHintsRegistrar.class)
44+
.forEach(registrar -> registrar.registerHints(this.hints,
45+
ClassUtils.getDefaultClassLoader()));
46+
}
47+
48+
@Test
49+
void entityManagerFactoryInfoHasHibernateHints() {
50+
assertThat(RuntimeHintsPredicates.proxies().forInterfaces(SessionFactory.class, EntityManagerFactoryInfo.class))
51+
.accepts(this.hints);
52+
}
53+
54+
@Test
55+
void entityManagerProxyHasHibernateHints() {
56+
assertThat(RuntimeHintsPredicates.proxies().forInterfaces(Session.class, EntityManagerProxy.class))
57+
.accepts(this.hints);
58+
}
59+
}

0 commit comments

Comments
 (0)