JPA和Hibernate:ERROR 10844 无法确定类型为:java.util.Set的列publishers在表book中。
JPA和Hibernate:ERROR 10844 无法确定类型为:java.util.Set的列publishers在表book中。
加入了带有注释@manytoone的Publisher后,它就可以正常工作了。\nBook.java的代码\n
package com.example.TDSSpringTest.domain; import javax.persistence.*; import java.util.HashSet; import java.util.Set; @Entity public class Book { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String title; private String isbn; @ManyToOne private Publisher publisher; @ManyToMany @JoinTable(name = "author_book", joinColumns = @JoinColumn(name = "book_id"), inverseJoinColumns = @JoinColumn(name = "author_id")) private Setauthors = new HashSet<>(); private Set publishers = new HashSet<>(); public Book (){ } public Set getPublishers() { return publishers; } public void setPublishers(Set publishers) { this.publishers = publishers; } public Book(String title, String isbn) { this.title = title; this.isbn = isbn; } public Publisher getPublisher() { return publisher; } public void setPublisher(Publisher publisher) { this.publisher = publisher; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getIsbn() { return isbn; } public void setIsbn(String isbn) { this.isbn = isbn; } public Set getAuthors() { return authors; } public void setAuthors(Set authors) { this.authors = authors; } @Override public String toString() { return "Book{" + "id=" + id + ", title='" + title + '\'' + ", isbn='" + isbn + '\'' + ", authors=" + authors + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Book book = (Book) o; return id != null ? id.equals(book.id) : book.id == null; } @Override public int hashCode() { return id != null ? id.hashCode() : 0; } }
\nPublisher.java的代码\n
package com.example.TDSSpringTest.domain; import javax.persistence.*; import java.util.HashSet; import java.util.Set; @Entity public class Publisher { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; private String adressLine1; private String city; private String state; private String zip; @OneToMany @JoinColumn(name = "publisher_id") private Setbooks = new HashSet<>(); public Publisher(){ } @Override public String toString() { return "Publisher{" + "id=" + id + ", name='" + name + '\'' + ", adressLine1='" + adressLine1 + '\'' + ", city='" + city + '\'' + ", state='" + state + '\'' + ", zip='" + zip + '\'' + '}'; } public Set getBooks() { return books; } public void setBooks(Set books) { this.books = books; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Publisher publisher = (Publisher) o; return id != null ? id.equals(publisher.id) : publisher.id == null; } @Override public int hashCode() { return id != null ? id.hashCode() : 0; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAdressLine1() { return adressLine1; } public void setAdressLine1(String adressLine1) { this.adressLine1 = adressLine1; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getState() { return state; } public void setState(String state) { this.state = state; } public String getZip() { return zip; } public void setZip(String zip) { this.zip = zip; } }
\n错误:\n2021-09-03 11:34:21.454 ERROR 10844--- [ main] o.s.boot.SpringApplication : 应用程序运行失败\norg.springframework.beans.factory.BeanCreationException: 在类路径资源[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]中定义的名为\'entityManagerFactory\'的bean创建失败: 初始化方法的调用失败; 嵌套异常是javax.persistence.PersistenceException: [PersistenceUnit: default] 无法构建Hibernate SessionFactory; 嵌套异常是org.hibernate.MappingException: 无法确定类型: java.util.Set,对应表: book,列: [org.hibernate.mapping.Column(publishers)]。