简体   繁体   English

使用JPA 2.1从来自两个不同模式的表中生成实体

[英]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 我目前正在尝试弄清如何在项目上使用两种不同的模式:目前,我可以从在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. 编辑:我最终在生成我的实体的同时创建了两个其他的JPA项目,然后将这两个项目添加到主项目的POM中,但它仍然只读取一个持久性单元,并且无法识别来自另一个架构的实体。

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. application.properties我们添加了模式modules和供应supplychain的属性,我为两个模式创建了2个JPA项目,并在那里生成了实体。 After that, we created the beans in the SpringServletInitializer: 之后,我们在SpringServletInitializer中创建了bean:

    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! 添加两个数据源后,我终于可以使用两个持久性了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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