我试图在Hibernate和Spring上实现复合主键。
目录具有catalogId,Book具有复合ID(catalogId和bookId),因此我已经使用@Embeddable注释实现了BookId类。
Catalog.java
@Entity
@Table(name = "catalog")
public class Catalog {
private Integer id;
private Timestamp date;
private List<Book> books = new ArrayList<>();
public Catalog() {
}
public Catalog(Timestamp date) {
this.date = date;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "date")
public Timestamp getDate() {
return date;
}
public void setDate(Timestamp date) {
this.date = date;
}
@OneToMany(mappedBy="catalog")
public List<Book> getBooks() {
return books;
}
public void setBooks(List<Book> lineasCatalog) {
this.books = books;
}
}
BookId.java
@Embeddable
public class BookId implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "id_catalog")
private Integer idCatalog;
@Column(name = "id_book")
private Integer idBook;
public BookId() {
}
public BookId(Integer idCatalog, Integer idBook) {
this.idCatalog = idCatalog;
this.idBook = idBook;
}
public Integer getIdCatalog() {
return idCatalog;
}
public void setIdCatalog(Integer idCatalog) {
this.idCatalog = idCatalog;
}
public Integer getIdBook() {
return idBook;
}
public void setIdBook(Integer idBook) {
this.idBook = idBook;
}
@Override
public int hashCode() {
return Objects.hash(getIdCatalog(), getIdBook());
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof BookId)) return false;
BookId that = (BookId) o;
return Objects.equals(getIdCatalog(), that.getIdCatalog()) &&
Objects.equals(getIdBook(), that.getIdBook());
}
}
Book.java
@Entity
@Table(name = "book")
public class Book {
private BookId idBook;
private Integer pages;
private String title;
private Catalog catalog;
public Book(Integer pages, Catalog catalog) {
this.pages = pages;
this.catalog = catalog;
}
public Book() {
}
@EmbeddedId
public BookId getIdBook() {
return idBook;
}
public void setIdBook(BookId idBook) {
this.idBook = idBook;
}
@Column(name = "pages")
public Integer getPages() {
return pages;
}
public void setPages(Integer pages) {
this.pages = pages;
}
@Column(name = "title")
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@ManyToOne
@JoinColumn(name="id_catalog")
public Catalog getCatalog() {
return catalog;
}
public void setCatalog(Catalog catalog) {
this.catalog = catalog;
}
}
服务:
@Transactional
public void addCatalog(Catalog catalog, List<Book> books) {
catalogDao.insert(catalog);
for(int i=0; i<books.size(); i++) {
BookId bid = new BookId();
bid.setIdBook(i+1);
bid.setIdCatalog(catalog.getId());
Book b = new Book();
b.setIdBook(bid);
b.setPages(books.get(i).getPages());
b.setTitle(books.get(i).getTitle());
b.setCatalog(catalog);
catalog.getBooks().add(b);
bookDao.insert(b);
}
}
我遇到下一个错误:
oct 06, 2018 9:27:59 PM org.hibernate.mapping.RootClass checkCompositeIdentifier
WARN: HHH000038: Composite-id class does not override equals(): com.myapp.BookId
oct 06, 2018 9:27:59 PM org.hibernate.mapping.RootClass checkCompositeIdentifier
WARN: HHH000039: Composite-id class does not override hashCode(): com.myapp.BookId
我已经实现了equals()和hashCode()重写方法,所以我不明白为什么会出问题。你能帮我吗?
答案 0 :(得分:0)
有时,映射中的其他问题会触发休眠警告。我发现您遇到的一个问题是Book.catalog
映射。您应该使用“派生身份”并添加@MapsId
批注:
@ManyToOne
@MapsId("idCatalog") // maps idCatalog attribute of embedded id
@JoinColumn(name="id_catalog")
public Catalog getCatalog() {
return catalog;
}