Spring 启动多个数据库的应用程序问题

Spring boot application problems with multiple databases

我正在尝试在我的持久性中进行 OneToMany 和 ManyToOne 映射 类。我有以下表格:

@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "file")
public class File {
    @Id
    @GeneratedValue(strategy = AUTO)
    private Long fileId;
    @NotBlank(message = "File Name not empty or null")
    private String fileName;
    private Instant createdDate;
    private Long customerId;
    @ManyToOne(fetch = FetchType.EAGER, targetEntity = Customer.class)
    @Column(name = "files")
    private Customer customer; 
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "customerDB")
public class Customer {
    @Id
    @GeneratedValue(strategy = AUTO)
    private Long id;
    @NotBlank(message = "Customer name is not null")
    private String customerName;
    @NotBlank(message = "İnfo is not null")
    private String info;
    private Instant createdDate;
    private Long createdUser;


    @OneToMany(targetEntity = File.class, mappedBy = "customer", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private Set<File> files;

application.properties

app.datasource.file.url=jdbc:mysql://localhost:3306/filedb?createDatabaseIfNotExist=true
app.datasource.file.username=root
app.datasource.file.password=password
app.datasource.file.driverClassName=com.mysql.cj.jdbc.Driver

app.datasource.customer.url=jdbc:mysql://localhost:3306/customerdb?createDatabaseIfNotExist=true
app.datasource.customer.username=root
app.datasource.customer.password=password
app.datasource.customer.driverClassName=com.mysql.cj.jdbc.Driver

spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
spring.jpa.database=mysql
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect 

也就是说,我因为没有尝试许多在线问题的一对多操作而收到错误。在一个软件中的版本。在具有多个版本的应用程序中。我找不到 solution.I 在我尝试的每个解决方案中得到几乎相同的错误。试了很多方法结果总是报错


org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerEntityManagerFactory' defined in class path resource [com/nishbs/cas/configuration/CustomerSourceConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.nishbs.cas.model.customer.Customer.files[com.nishbs.cas.model.file.File]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=14=](AbstractBeanFactory.java:335) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1154) ~[spring-context-5.3.18.jar:5.3.18]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:908) ~[spring-context-5.3.18.jar:5.3.18]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.18.jar:5.3.18]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.6.jar:2.6.6]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:740) [spring-boot-2.6.6.jar:2.6.6]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:415) [spring-boot-2.6.6.jar:2.6.6]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-2.6.6.jar:2.6.6]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1312) [spring-boot-2.6.6.jar:2.6.6]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301) [spring-boot-2.6.6.jar:2.6.6]

首先确保您已经为 @Entity 注释导入了这个 javax.persistence.Entity 包。

当您加入两个 table 时,将再创建一个 table。所以,我应该在映射时指定新 table 的名称,并提供 referencedColumnName.

@OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "post_id", referencedColumnName = "id")
    private List<Comment> comments;

上面就是我想说的一个这样的例子。尝试这样实现。

您似乎正在尝试连接不同数据库中的两个表。 我不是 100% 确定,但我会说你这样做是不可能的。

即使没有 spring 作为框架,您也必须先 link 您的数据库。我想如果你做到了这一点,那么你可以只用 spring.

中的一个数据源访问数据库

首先 link 访问您的数据库并尝试 运行 手动 SQl 查询,如果可行,请使用代码重试。我不是这方面的专家,但我会开始寻找一些东西 linke 这个 Oracle Database Link - MySQL Equivalent?