我試圖在我的持久性類中有一個 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;
應用程式屬性
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
也就是說,我因為沒有嘗試很多在線問題的一對多操作而遇到錯誤。在一個軟體中的版本。在具有多個版本的應用程式中。我找不到解決方案。我嘗試的每個解決方案都遇到幾乎相同的錯誤。我嘗試了很多方法,但結果總是錯誤。
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$0(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]
uj5u.com熱心網友回復:
您似乎正在嘗試連接不同資料庫中的兩個表。我不是 100% 確定,但我會說你這樣做是不可能的。
即使沒有 spring 作為框架,您也必須首先鏈接資料庫。我猜如果你已經實作了這一點,那么你可以在 spring 中只使用一個資料源來訪問資料庫。
首先鏈接您的資料庫并嘗試運行手動 SQl 查詢,如果這可行,請使用代碼重試。我不是這方面的專家,但我會從尋找鏈接這個Oracle 資料庫鏈接 - MySQL 等效項開始?
uj5u.com熱心網友回復:
首先確保@Entity你已經匯入了這個javax.persistence.Entity包的注解。
當您連接兩個表時,將再創建一個表。所以,我應該在映射時指定新表的名稱,并提供referencedColumnName.
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "post_id", referencedColumnName = "id")
private List<Comment> comments;
以上就是我想說的一個例子。嘗試以這種方式實作。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/459053.html
