gpt4 book ai didi

spring - 如何通过 @PersistenceUnit 注释通过构造函数设置 EntityManagerFactory

转载 作者:行者123 更新时间:2023-12-04 06:43:26 26 4
gpt4 key购买 nike

有谁知道如何通过 @PersistenceUnit 注释通过构造函数设置 EntityManagerFactory 。我可以通过xml配置来做到这一点。但是我不知道对应的注解配置。

<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter">
<bean
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml" />
<property name="persistenceUnitName" value="myUnit"></property>
<bean id="handler" class="com.handler.LocalHTHandler">
<constructor-arg ref="entityManagerFactory"></constructor-arg>

它工作正常。我们可以通过我的处理程序 bean 的注释来做到这一点吗?我听说过 @persistanceunit ,但它不能放在构造函数中来实现它。那是对的吗?

最佳答案

正如@Dherik 所说,您可以通过构造函数执行此操作。下面的多数据源示例(在 SpringBoot 2.0.4 上测试):

SomeRepository.java

@Repository
public class SomeRepository {

private final EntityManager entityManager;

public TestService(@Qualifier("someEntityManagerFactory") EntityManager entityManager) {
this.entityManager = entityManager;
}
}

SomeDatabaseConfig.java
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.orm.jpa.vendor.HibernateJpaDialect;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "someEntityManagerFactory")
@EnableConfigurationProperties(JpaProperties.class)
class SomeDatabaseConfig {

private static final String PERSISTENCE_UNIT = "some";
private static final String[] PACKAGES_TO_SCAN = {"package.where.you.store.your.entities"};

@Bean(name = "someDataSourceProps")
@ConfigurationProperties("some.datasource")
DataSourceProperties dataSourceProperties() {
return new DataSourceProperties();
}

@Bean(name = "someDataSource")
DataSource dataSource(@Qualifier("someDataSourceProps") DataSourceProperties properties) {
return properties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
}

@Bean(name = "someEntityManagerFactory")
LocalContainerEntityManagerFactoryBean entityManagerFactory(
JpaProperties jpaProperties,
@Qualifier("someDataSource") DataSource dataSource,
@Qualifier("someJpaVendorAdapter") JpaVendorAdapter jpaVendorAdapter) {
return createEntityManagerFactory(jpaProperties, dataSource, jpaVendorAdapter, PERSISTENCE_UNIT, PACKAGES_TO_SCAN);
}

@Bean("someJpaVendorAdapter")
JpaVendorAdapter jpaVendorAdapter(JpaProperties jpaProperties) {
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setShowSql(jpaProperties.isShowSql());
return vendorAdapter;
}

@Bean(name = "someTransactionManager")
PlatformTransactionManager transactionManager(@Qualifier("someEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}

static LocalContainerEntityManagerFactoryBean createEntityManagerFactory(JpaProperties jpaProperties, DataSource dataSource, JpaVendorAdapter jpaVendorAdapter, String persistenceUnit, String[] packagesToScan) {
final LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
factoryBean.setJpaDialect(new HibernateJpaDialect());
factoryBean.setJpaPropertyMap(getVendorProperties(jpaProperties));
factoryBean.setPersistenceUnitName(persistenceUnit);
factoryBean.setPackagesToScan(packagesToScan);
return factoryBean;
}

private static Map<String, Object> getVendorProperties(JpaProperties jpaProperties) {
return jpaProperties.getHibernateProperties(new HibernateSettings());
}
}

应用程序.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration;

@SpringBootApplication(
exclude = {
DataSourceAutoConfiguration.class,
HibernateJpaAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class
}
)
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

关于spring - 如何通过 @PersistenceUnit 注释通过构造函数设置 EntityManagerFactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17917260/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com