title: Composite-id on hibernate and spring
body: <p>I was trying to implement a composite primary key on Hibernate and Spring.</p>
  
  <p>Catalog has a catalogId, Book has a composite id (catalogId and bookId) so I've implemented BookId class using @Embeddable annotation.</p>
  
  <p>Catalog.java</p>
  
  <pre><code>@Entity
  @Table(name = "catalog")
  public class Catalog {
  
      private Integer id;
      private Timestamp date;
  
  
      private List&lt;Book&gt; books = new ArrayList&lt;&gt;();
  
      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&lt;Book&gt; getBooks() {
          return books;
      }
  
      public void setBooks(List&lt;Book&gt; lineasCatalog) {
          this.books = books;
      }
  
  }
  </code></pre>
  
  <p>BookId.java</p>
  
  <pre><code>@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()) &amp;&amp;
                  Objects.equals(getIdBook(), that.getIdBook());
  
      }
  
  }
  </code></pre>
  
  <p>Book.java</p>
  
  <pre><code>@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;
      }
  
  }
  </code></pre>
  
  <p>Service:</p>
  
  <pre><code>@Transactional
      public void addCatalog(Catalog catalog, List&lt;Book&gt; books) {
          catalogDao.insert(catalog);
  
          for(int i=0; i&lt;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);
          }
      }
  </code></pre>
  
  <p>I get the next error:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I have implemented equals() and hashCode() override methods so I can't understand why is the problem. Can you help me?</p>