gpt4 book ai didi

Entity Autocreation not working after entityManagerFactory definition(在entityManagerFactory定义之后,实体自动创建不起作用)

转载 作者:bug小助手 更新时间:2023-10-28 22:37:12 25 4
gpt4 key购买 nike



I had a working code with Spring -Jpa . Recently I had a requirement where I had to add multiple datasource and for the same I had to define the entityManagerFactory(Code given below) . Since the time I added this entity(Table) is not created automatically or is not recognized by appication. My Project is using Spring parent 2.7.13 . I tried it with newer version of Spring(3.1.3) and it works fine. Can someone help me out what is the issue and is there something I am missing. I have validated the packages and it is fine. Also before this new class being added it was working fine.

我有一个用Spring-JPA运行的代码。最近我有一个要求,我必须添加多个数据源,并且为了同样的目的,我必须定义entityManager工厂(代码如下所示)。自从我添加该实体(表)以来,该实体(表)不是自动创建的,或者不被应用程序识别。我的项目使用的是Spring Parent 2.7.13。我在较新版本的Spring(3.1.3)上尝试了一下,它工作得很好。有人能帮我解决一下问题吗?我有什么地方遗漏了什么吗?我已经验证了包,它是好的。同样,在添加这个新类之前,它运行得很好。


@Configuration
public class DataSourceConfig {

// Primary datasource configuration

@Primary
@Bean(name = "primaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}

@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("primaryDataSource") DataSource dataSource) {
return builder
.dataSource(dataSource)
.packages("com.example.demo.model") // base package for your JPA entities
.persistenceUnit("primary")
.build();
}

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

// Secondary Datasource 1 Configuration

@Bean(name = "secondaryDataSource")
@ConfigurationProperties(prefix = "spring.ds1")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}

@Bean(name = "secondaryJdbcTemplate")
public JdbcTemplate secondaryJdbcTemplate(@Qualifier("secondaryDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}

// Secondary Datasource 2 Configuration

@Bean(name = "thirdDataSource")
@ConfigurationProperties(prefix = "spring.ds2")
public DataSource thirdDataSource() {
return DataSourceBuilder.create().build();
}

@Bean(name = "thirdJdbcTemplate")
public JdbcTemplate thirdJdbcTemplate(@Qualifier("thirdDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}

更多回答

so if i understand right you need some code for multiple datasources in your spring app?

所以,如果我理解正确的话,您需要在您的Spring应用程序中为多个数据源编写一些代码?

Yes, I need to execute queries on different datasources so only need jdbc template for multiple datasources

是的,我需要在不同的数据源上执行查询,所以只需要为多个数据源使用JDBC模板

There are a lot of similar questions, for example: this and this. Try to find it yourself. Aterword: Spring Boot version <3.x is not designed to configure multiple data sources. You need to put in some effort to achieve this.

有很多类似的问题,例如:这个和这个。试着自己去找。Aterword:Spring Boot版本低于3.x不是为配置多个数据源而设计的。你需要付出一些努力才能做到这一点。

优秀答案推荐

The way that worked for me is:

对我有效的方法是:



  1. I created a folder for each database/user that i want my app to connect.For example if you want to connect to 2 databases (db1, db2) i created 2 folders db1 and db2.



  2. Inside those folders i created an Entity folder (where i put the corresponding to the database entities), a Repository folder, a Service folder and a Config folder.



  3. In the Config folder i created the DB1_Config.java file (DB1_Config for db1, DB2_Config for db2)



  4. The DB1_Config.java file looks like this


    @Configuration
    @EnableTransactionManagement
    @EnableJpaRepositories(
    entityManagerFactoryRef = "DB1userEntityManagerFactory",
    transactionManagerRef = "DB1userTransactionManager",
    basePackages = {"com.DataBase.DB1.user.Repository"}) //put the actual path to your Repository folder
    public class DB1_Config{

    static final String JNDI = "jdbc/DB1user"; //your actual jndi name

    @Bean(name = "DB1userDataSource", destroyMethod = "")
    @ConfigurationProperties(prefix = "DB1user.datasource")
    public DataSource dataSource() throws DataSourceLookupFailureException {
    JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
    return dataSourceLookup.getDataSource(JNDI);
    }

    @Bean(name = "DB1userEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean
    entityManagerFactory(
    EntityManagerFactoryBuilder builder,
    @Qualifier("DB1userDataSource") DataSource dataSource) {
    return builder.dataSource(dataSource).packages("com.DataBase.DB1.user.Entity").persistenceUnit("db1user").build();
    } //in com.DataBase.DB1.user.Entity put your actual Entity path

    @Bean(name = "DB1userTransactionManager")
    public PlatformTransactionManager entityTransactionManager(
    @Qualifier("DB1userEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
    return new JpaTransactionManager(entityManagerFactory);
    }
    }



And DB2_Config the same but wherever it writes DB1 you put DB2. I think the main problem is that you have to group your classes depending on the different databases or if you are on the same database on the different schema users.

和DB2Configer相同,但是它写入DB1的任何地方,您都放置了DB2.我认为主要问题是您必须根据不同的数据库对类进行分组,或者如果您位于同一数据库上的不同模式用户上。


更多回答

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