Я пытался реализовать составной первичный ключ в Hibernate и Spring.
В каталоге есть catalogId, в Book - составной id (catalogId и bookId), поэтому я реализовал класс BookId с помощью аннотации @Embeddable.
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 (), поэтому не могу понять, в чем проблема.Вы можете мне помочь?