Skip to content

Commit adbac41

Browse files
committed
Use named and typed RuntimeBeanReference for shared EntityManager injection.
We now declare the target type for RuntimeBeanReference to indicate the desired target type so that SmartFactoryBeans can be used for property value injection of the shared EntityManager. See #3926
1 parent 1678aba commit adbac41

File tree

2 files changed

+6
-20
lines changed

2 files changed

+6
-20
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/config/JpaRepositoryConfigExtension.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static org.springframework.data.jpa.repository.config.BeanDefinitionNames.*;
1919

2020
import jakarta.persistence.Entity;
21+
import jakarta.persistence.EntityManager;
2122
import jakarta.persistence.EntityManagerFactory;
2223
import jakarta.persistence.MappedSuperclass;
2324
import jakarta.persistence.PersistenceContext;
@@ -45,6 +46,7 @@
4546
import org.springframework.beans.factory.config.BeanDefinition;
4647
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
4748
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
49+
import org.springframework.beans.factory.config.RuntimeBeanReference;
4850
import org.springframework.beans.factory.support.AbstractBeanDefinition;
4951
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
5052
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
@@ -138,7 +140,8 @@ public void postProcess(BeanDefinitionBuilder builder, RepositoryConfigurationSo
138140
Optional<String> transactionManagerRef = source.getAttribute("transactionManagerRef");
139141
builder.addPropertyValue("transactionManager", transactionManagerRef.orElse(DEFAULT_TRANSACTION_MANAGER_BEAN_NAME));
140142
if (entityManagerRefs.containsKey(source)) {
141-
builder.addPropertyReference("entityManager", entityManagerRefs.get(source));
143+
builder.addPropertyValue("entityManager",
144+
new RuntimeBeanReference(entityManagerRefs.get(source), EntityManager.class));
142145
}
143146
builder.addPropertyValue(ESCAPE_CHARACTER_PROPERTY, getEscapeCharacter(source).orElse('\\'));
144147
builder.addPropertyReference("mappingContext", JPA_MAPPING_CONTEXT_BEAN_NAME);
@@ -235,14 +238,10 @@ private void registerSharedEntityMangerIfNotAlreadyRegistered(BeanDefinitionRegi
235238
String entityManagerBeanRef = getEntityManagerBeanRef(config);
236239
String sharedEntityManagerBeanRef = lookupSharedEntityManagerBeanRef(entityManagerBeanRef, registry);
237240

238-
// TODO: Remove once Cannot convert value of type 'jdk.proxy2.$Proxy129 implementing
239-
// org.hibernate.SessionFactory,org.springframework.orm.jpa.EntityManagerFactoryInfo' to required type
240-
// 'jakarta.persistence.EntityManager' for property 'entityManager': no matching editors or conversion strategy
241-
// found is fixed.
242-
/*if (sharedEntityManagerBeanRef != null) {
241+
if (sharedEntityManagerBeanRef != null) {
243242
entityManagerRefs.put(config, sharedEntityManagerBeanRef);
244243
return;
245-
} */
244+
}
246245

247246
String entityManagerBeanName = "jpaSharedEM_" + entityManagerBeanRef;
248247

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/aot/AotFragmentTestConfigurationSupport.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@
3232
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
3333
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
3434
import org.springframework.beans.factory.support.DefaultBeanNameGenerator;
35-
import org.springframework.context.annotation.Bean;
3635
import org.springframework.context.annotation.ImportResource;
37-
import org.springframework.context.annotation.Primary;
3836
import org.springframework.core.env.StandardEnvironment;
3937
import org.springframework.core.io.DefaultResourceLoader;
4038
import org.springframework.core.test.tools.TestCompiler;
@@ -47,7 +45,6 @@
4745
import org.springframework.data.repository.core.RepositoryMetadata;
4846
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
4947
import org.springframework.data.repository.query.ValueExpressionDelegate;
50-
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
5148
import org.springframework.util.ReflectionUtils;
5249

5350
/**
@@ -77,16 +74,6 @@ EnableJpaRepositories.class, new DefaultResourceLoader(), new StandardEnvironmen
7774
Mockito.mock(BeanDefinitionRegistry.class), DefaultBeanNameGenerator.INSTANCE));
7875
}
7976

80-
// TODO: Remove once Cannot convert value of type 'jdk.proxy2.$Proxy129 implementing
81-
// org.hibernate.SessionFactory,org.springframework.orm.jpa.EntityManagerFactoryInfo' to required type
82-
// 'jakarta.persistence.EntityManager' for property 'entityManager': no matching editors or conversion strategy found
83-
// is fixed.
84-
@Bean
85-
@Primary
86-
EntityManager entityManager(LocalContainerEntityManagerFactoryBean factoryBean) throws Exception {
87-
return factoryBean.getObject(EntityManager.class);
88-
}
89-
9077
@Override
9178
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
9279

0 commit comments

Comments
 (0)