简体   繁体   中英

Generating entities from tables from two different schemas with JPA 2.1

I'm currently trying to figure out how to work with two different schemas on my project: currently I can successfully generate my entities from two different Datasources that I created on Eclipse

数据库连接

Basically it's the same server, but I am forced to use two different connection strings in order to access both schemas. The problem is that by switching connection in order to generate the entities from the other schema, the previous entities cannot be recognized: 无法识别的实体

Is there a way to solve this problem? Is there a way for me to make my entities recognized no matter what?

EDIT: I ended up creating 2 additional JPA projects where I generate my entities, then I added those 2 projects into the main project's POM but still it only reads one persistence unit and the entities coming from the other schema are not recognized.

Forgot to answer this question, I finally found a solution for this issue. In the application.properties we added the properties for the schemas modules and supplychain , and I created 2 JPA projects for both schemas, and generated the entities there. After that, we created the beans in the SpringServletInitializer:

    private Map<String, String> jpaProperties() {

    Map<String, String> p = new HashMap<String, String>();

    p.put("hibernate.dialect", env.getProperty("spring.jpa.properties.hibernate.dialect"));
    p.put("hibernate.id.new_generator_mappings",
            env.getProperty("spring.jpa.properties.hibernate.id.new_generator_mappings"));
    p.put("hibernate.format_sql", env.getProperty("spring.jpa.properties.hibernate.format_sql"));
    p.put("hibernate.naming.physical-strategy", env.getProperty("spring.jpa.hibernate.naming.physical-strategy"));
    p.put("hibernate.ddl-auto", env.getProperty("spring.jpa.hibernate.ddl-auto"));

    return p;
}



@Bean
@ConfigurationProperties(prefix = "modules.datasource")
public DataSource modulesDataSource() {
    return DataSourceBuilder.create().build();
}

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

@Bean(name="modules")
public LocalContainerEntityManagerFactoryBean modulesEntityManagerFactory(
        org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder builder) throws IOException {
    return builder.dataSource(modulesDataSource()).packages(Moduli.class).properties(jpaProperties()).persistenceUnit("JPAModules").build();
}

@Bean(name="supplychain")
@Primary
public LocalContainerEntityManagerFactoryBean supplychainEntityManagerFactory(EntityManagerFactoryBuilder builder)
        throws IOException {
    return builder.dataSource(supplychainDataSource()).packages(Rent.class).properties(jpaProperties()).persistenceUnit("JPASupplychain").build();
}

@Bean(name = "modulesTransactionManager")
public PlatformTransactionManager modulesTransactionManager(EntityManagerFactoryBuilder builder)
        throws IOException {
    JpaTransactionManager tm = new JpaTransactionManager();
    tm.setEntityManagerFactory(modulesEntityManagerFactory(builder).getObject());
    tm.setDataSource(modulesDataSource());
    return tm;
}

@Bean(name = "supplychainTransactionManager")
@Primary
public PlatformTransactionManager supplychainTransactionManager(EntityManagerFactoryBuilder builder)
        throws IOException {
    JpaTransactionManager tm = new JpaTransactionManager();
    tm.setEntityManagerFactory(supplychainEntityManagerFactory(builder).getObject());
    tm.setDataSource(supplychainDataSource());
    return tm;
}

Adding both datasources, I can finally use both persistences!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM