【问题标题】:Relationship between two entities of different schemes - Spring Boot Data JPA不同方案的两个实体之间的关系——Spring Boot Data JPA
【发布时间】:2020-12-09 13:09:25
【问题描述】:

是否可以在 Spring Boot 中使用 JPA 交叉连接两个不同的数据库表(2 个数据源)?如果是这样,您将如何配置此配置?在 application.properties 中,已经定义了 4 个数据库,并创建了 4 个文件来表示数据源的配置。场景是有一个 DataSource 1 表,它有一个 FOREIGN KEY 和一个 DataSource 2 表,但是当应用程序被引发时会生成一个异常:

@OneToOne or @ManyToOne on com.smart4.bdcentral.domain.files.IndividuoFoto .persons references an unknown entity: com.smart4.bdcentral.domain.main.IndividuoPessoa

数据源 1

    package com.smart4.configBases;

import java.util.HashMap;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;

import com.google.common.base.Preconditions;

@Configuration
@PropertySource({ "classpath:application.properties" })
@EnableJpaRepositories(basePackages             = {"com.smart4.bdcentral.repository.main"}, 
                       entityManagerFactoryRef  = "bdCentralMainEntityManager", 
                       transactionManagerRef    = "bdCentralMainTransactionManager")
public class BdCentralMain {

    @Autowired
    private Environment env;

    public BdCentralMain() {
        super();
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean bdCentralMainEntityManager() {
        final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(bdCentralMainDataSource());
        em.setPackagesToScan(new String[] { "com.smart4.bdcentral.domain.main" });
        em.setPersistenceUnitName("bdcentralmain");

        final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setDatabase(Database.MYSQL);
        vendorAdapter.setShowSql(false);
        vendorAdapter.setGenerateDdl(true);
        vendorAdapter.setDatabasePlatform("org.hibernate.dialect.MySQL5Dialect");

        em.setJpaVendorAdapter(vendorAdapter);
        final HashMap<String, Object> properties = new HashMap<String, Object>();

        properties.put("hibernate.hbm2ddl.auto", env.getProperty("bdcentralmain.hibernate.hbm2ddl.auto"));
        properties.put("hibernate.dialect", env.getProperty("bdcentralmain.hibernate.dialect"));

        em.setJpaPropertyMap(properties);

        return em;
    }

    @Bean
    public DataSource bdCentralMainDataSource() {
        final DriverManagerDataSource dataSource = new DriverManagerDataSource();

        dataSource
                .setDriverClassName(Preconditions.checkNotNull(env.getProperty("bdcentralmain.jdbc.driverClassName")));
        dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("bdcentralmain.jdbc.url")));
        dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("bdcentralmain.jdbc.user")));
        dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("bdcentralmain.jdbc.pass")));

        return dataSource;
    }

    @Bean
    public PlatformTransactionManager bdCentralMainTransactionManager() {
        final JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(bdCentralMainEntityManager().getObject());
        return transactionManager;
    }

}

数据源 2

package com.smart4.configBases;

import java.util.HashMap;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;

import com.google.common.base.Preconditions;

@Configuration
@PropertySource({ "classpath:application.properties" })
@EnableJpaRepositories(basePackages             = {"com.smart4.bdcentral.repository.files"}, 
                       entityManagerFactoryRef  = "bdCentralFilesEntityManager", 
                       transactionManagerRef    = "bdCentralFilesTransactionManager")
public class BdCentralFiles {
    
    @Autowired
    private Environment env;

    public BdCentralFiles() {
        super();
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean bdCentralFilesEntityManager() {
        final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(bdCentralFilesDataSource());
        em.setPackagesToScan(new String[] { "com.smart4.bdcentral.domain.files" });
        em.setPersistenceUnitName("bdcentralfiles");

        final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setDatabase(Database.MYSQL);
        vendorAdapter.setShowSql(false);
        vendorAdapter.setGenerateDdl(true);
        vendorAdapter.setDatabasePlatform("org.hibernate.dialect.MySQL5Dialect");
        em.setJpaVendorAdapter(vendorAdapter);

        final HashMap<String, Object> properties = new HashMap<String, Object>();
        properties.put("hibernate.hbm2ddl.auto", env.getProperty("bdcentralfiles.hibernate.hbm2ddl.auto"));
        properties.put("hibernate.dialect", env.getProperty("bdcentralfiles.hibernate.dialect"));

        em.setJpaPropertyMap(properties);

        return em;
    }

    @Bean
    public DataSource bdCentralFilesDataSource() {
        final DriverManagerDataSource dataSource = new DriverManagerDataSource();

        dataSource
                .setDriverClassName(Preconditions.checkNotNull(env.getProperty("bdcentralfiles.jdbc.driverClassName")));
        dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("bdcentralfiles.jdbc.url")));
        dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("bdcentralfiles.jdbc.user")));
        dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("bdcentralfiles.jdbc.pass")));

        return dataSource;
    }

    @Bean
    public PlatformTransactionManager bdCentralFilesTransactionManager() {
        final JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(bdCentralFilesEntityManager().getObject());
        return transactionManager;
    }

}

【问题讨论】:

    标签: java spring spring-boot hibernate spring-data-jpa


    【解决方案1】:

    仅对数据源没什么好说的,检查注解配置并确保您的对象是@Entity 注解的。听起来您实际上并不想要“交叉连接”,而且您正在查看 Hibernate,我一直将 Spring Boot JPA 视为 spring“数据”JPA 并且对为什么它不是数据 jpa 大声笑感到困惑。

    【讨论】:

      猜你喜欢
      • 2017-07-12
      • 2021-02-25
      • 1970-01-01
      • 2014-10-04
      • 2021-05-22
      • 1970-01-01
      • 1970-01-01
      • 2016-12-03
      相关资源
      最近更新 更多