java不同方案的两个实体之间的关系spring boot data jpa

11dmarpk  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(367)

在spring引导中使用jpa是否可以交叉连接两个不同的数据库表(2个数据源)?如果是,您将如何配置此配置?在application.properties中,已经定义了4个数据库,并创建了4个表示数据源配置的文件。这种情况是,有一个datasource 1表的外键是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;
    }

}
ftf50wuq

ftf50wuq1#

对于数据源没什么好说的,检查注解配置并确保您的对象是@entity注解的。听起来你并不是真的想要一个“交叉连接”,同时你也在看hibernate,我一直把spring boot jpa读成spring“data”jpa,我很困惑为什么它不是data jpa lol。

相关问题