gpt4 book ai didi

java - hibernate更改多个数据源后读取不同的关系表名

转载 作者:行者123 更新时间:2023-12-01 22:01:49 28 4
gpt4 key购买 nike

我正在开发一个使用 spring boot 版本 1.3.5.RELEASE 的项目,它使用 spring data jpa 作为持久层。我的任务是在这个项目上实现多个数据源。

在实现代码后,我们得到了关于这两个实体类的错误:

@Entity
@Table(name = "TBL_MERCHANT")
public class Merchant implements Serializable {

@Id
@Column(name = "ID", length = 14, precision = 14, nullable = false)
private BigInteger id;

@Column(name = "NAME", length = 100, nullable = false)
private String name;

@JoinColumn(name = "CATEGORY_ID")
@ManyToOne(fetch = FetchType.LAZY)
private Categorization categorization;

@OrderBy("id ASC")
@ManyToMany(mappedBy = "merchants", fetch = FetchType.LAZY)
private Set<PaymentMethod> paymentMethods = new HashSet<>();

@JoinColumn(name = "USER_ID")
@OneToOne(fetch = FetchType.LAZY)
private WalletUser user;

// getter setter

@Entity
@Table(name = "TBL_PAYMENT_METHOD")
public class PaymentMethod implements Serializable {

@Id
@Column(name = "ID", length = 24)
private String id;

@Column(name = "DESCRIPTION", length = 255, nullable = true)
private String desc;

@ManyToMany
private Set<Merchant> merchants = new HashSet<>();

// getter setter
}

之前,这两个实体之间的关系生成一个名为tbl_ payment_method_merchants的表但更改后出现这种错误错误:关系“tbl_ payment_method_tbl_merchant”不存在

以下是我的更改:

之前

@EnableAsync
@EnableAutoConfiguration(exclude = {org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration.class})
@EntityScan("com.somepackage")
@ComponentScan({"com.somepackage})

@EnableJpaRepositories("com.somepackage")
@PropertySource(value = "classpath:stringMessage.properties")
public class MainApplication {

public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}

}

我将其分为两个不同的数据源,如下:

之后:

第一个数据源:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
basePackages = RepositoryMetadataProvider.REPOSITORY_BASE_PACKAGE,
includeFilters = @ComponentScan.Filter(ReadOnlyConnection.class),
entityManagerFactoryRef = ReadOnlyRepositoryConfig.READ_ONLY_ENTITY_MANAGER_FACTORY,
transactionManagerRef = ReadOnlyRepositoryConfig.READ_ONLY_TRX_MANAGER
)
public class ReadOnlyRepositoryConfig {

public static final String READ_ONLY_TRX_MANAGER ="readOnlyTransactionManager";
public static final String READ_ONLY_ENTITY_MANAGER ="readOnlyEntityManager";
public static final String READ_ONLY_ENTITY_MANAGER_FACTORY ="readOnlyEntityManagerFactory";

@Bean(name = READ_ONLY_TRX_MANAGER)
PlatformTransactionManager readOnlyTransactionManager(
@Qualifier(READ_ONLY_ENTITY_MANAGER_FACTORY) EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}

@Bean(name = READ_ONLY_ENTITY_MANAGER)
EntityManager readOnlyEntityManager(@Qualifier(READ_ONLY_ENTITY_MANAGER_FACTORY) LocalContainerEntityManagerFactoryBean masterEntityFactory) {
return masterEntityFactory.getObject().createEntityManager();
}

@Singleton
@Bean(name = READ_ONLY_ENTITY_MANAGER_FACTORY)
LocalContainerEntityManagerFactoryBean readOnlyEntityFactory(
EntityManagerFactoryBuilder builder,
@Qualifier(DataSourceConfig.WALLET_SLAVE_DATASOURCE) DataSource dataSource) {

LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean = builder
.dataSource(dataSource)
.packages(RepositoryMetadataProvider.REPOSITORY_BASE_PACKAGE)
.build();
localContainerEntityManagerFactoryBean.setMappingResources(new String[]{"META-INF/orm.xml", "META-INF/bv-orm.xml", "META-INF/infinitium-orm.xml", "META-INF/cam-orm.xml"});
return localContainerEntityManagerFactoryBean;

}
}

第二个数据源:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
basePackages = RepositoryMetadataProvider.REPOSITORY_BASE_PACKAGE,
excludeFilters =@ComponentScan.Filter(ReadOnlyConnection.class),
entityManagerFactoryRef = TransactionalRepositoryConfig.TRANSACTIONAL_ENTITY_MANAGER_FACTORY,
transactionManagerRef = TransactionalRepositoryConfig.TRANSACTIONAL_TRX_MANAGER
)
public class TransactionalRepositoryConfig {

public static final String TRANSACTIONAL_TRX_MANAGER ="transactionalTransactionManager";
public static final String TRANSACTIONAL_ENTITY_MANAGER ="transactionalEntityManager";
public static final String TRANSACTIONAL_ENTITY_MANAGER_FACTORY ="transactionalEntityManagerFactory";

@Primary
@Bean(name = TRANSACTIONAL_TRX_MANAGER)
PlatformTransactionManager masterTransactionManager(
@Qualifier(TRANSACTIONAL_ENTITY_MANAGER_FACTORY) EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}


@Primary
@Bean(name = TRANSACTIONAL_ENTITY_MANAGER)
EntityManager masterEntityManager(@Qualifier(TRANSACTIONAL_ENTITY_MANAGER_FACTORY) LocalContainerEntityManagerFactoryBean masterEntityFactory){
return masterEntityFactory.getObject().createEntityManager();
}


@Singleton
@Primary
@Bean(name = TRANSACTIONAL_ENTITY_MANAGER_FACTORY)
LocalContainerEntityManagerFactoryBean masterEntityFactory(
EntityManagerFactoryBuilder builder,
@Qualifier(DataSourceConfig.WALLET_MASTER_DATASOURCE) DataSource dataSource) {

LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean = builder
.dataSource(dataSource)
.packages(RepositoryMetadataProvider.REPOSITORY_BASE_PACKAGE)
.build();

localContainerEntityManagerFactoryBean.setMappingResources(new String[]{"META-INF/orm.xml", "META-INF/bv-orm.xml", "META-INF/infinitium-orm.xml", "META-INF/cam-orm.xml"});
return localContainerEntityManagerFactoryBean;

}

}

org.hibernate.engine.jdbc.spi.SqlExceptionHelper:错误:使用多个数据源时,关系“tbl_ payment_method_tbl_merchant”不存在

最佳答案

@Entity
@Table(name = "TBL_MERCHANT")
public class Merchant implements Serializable {

@OrderBy("id ASC")
@ManyToMany(mappedBy = "merchants", fetch = FetchType.LAZY)
@JoinTable(name = "tbl_payment_method_merchants",
joinColumns = @JoinColumn(name = "idMerchant", referencedColumnName = "idMerchant"),
inverseJoinColumns = @JoinColumn(name = "idPaymentMethod", referencedColumnName = "idPaymentMethod"))
private Set<PaymentMethod> paymentMethods = new HashSet<>();

// getter setter

@Entity
@Table(name = "TBL_PAYMENT_METHOD")
public class PaymentMethod implements Serializable {

@ManyToMany
@JoinTable(name = "tbl_payment_method_merchants",
joinColumns = @JoinColumn(name = "idPaymentMethod", referencedColumnName = "idPaymentMethod"),
inverseJoinColumns = @JoinColumn(name = "idMerchant", referencedColumnName = "idMerchant"))
private Set<Merchant> merchants = new HashSet<>();

// getter setter
}

你可以试试上面的方法吗?这将确保多对多关系的元数据在两个实体上都可用。

我还建议将 FetchType.EAGER 替换为 FetchType.LAZY。

关于java - hibernate更改多个数据源后读取不同的关系表名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58707387/

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