0

I am using Hibernate for my DAO implementation. I created the mappings and I overriden the equals method.

When I use assertEquals(object,object) it fails even though the objects are identical. Book.java

package tn.jebouquine.pojo;

@Entity
public class Book {
    @Id
    private String ISBN;
    @NotEmpty
    private String name;
    @NotNull
    @DecimalMin(value = "0.00")
    private BigDecimal price;
    private String description;
    @ManyToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
    @NotEmpty
    private List<Author> authors = new ArrayList<Author>();
    @ManyToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
    private List<Category> categories = new ArrayList<Category>();
    @ElementCollection(fetch = FetchType.EAGER)
    private List<BookComment> bookComments = new ArrayList<BookComment>();

    public String getISBN() {
        return ISBN;
    }

    public void setISBN(String ISBN) {
        this.ISBN = ISBN;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public List<Author> getAuthors() {
        return authors;
    }

    public void setAuthors(List<Author> authors) {
        this.authors = authors;
    }

    public List<Category> getCategories() {
        return categories;
    }

    public void setCategories(List<Category> categories) {
        this.categories = categories;
    }

    public List<BookComment> getBookComments() {
        return bookComments;
    }

    public void setBookComments(List<BookComment> bookComments) {
        this.bookComments = bookComments;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Book)) return false;

        Book book = (Book) o;

        if (!ISBN.equals(book.ISBN)) return false;
        if (!authors.equals(book.authors)) return false;
        if (bookComments != null ? !bookComments.equals(book.bookComments) : book.bookComments != null) return false;
        if (categories != null ? !categories.equals(book.categories) : book.categories != null) return false;
        if (description != null ? !description.equals(book.description) : book.description != null) return false;
        if (!name.equals(book.name)) return false;
        if (!price.equals(book.price)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = ISBN.hashCode();
        result = 31 * result + name.hashCode();
        result = 31 * result + price.hashCode();
        result = 31 * result + (description != null ? description.hashCode() : 0);
        result = 31 * result + (authors != null ? authors.hashCode() : 0);
        result = 31 * result + (categories != null ? categories.hashCode() : 0);
        result = 31 * result + (bookComments != null ? bookComments.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "Book{" +
                "ISBN='" + ISBN + '\'' +
                ", name='" + name + '\'' +
                ", price=" + price +
                ", description='" + description + '\'' +
                ", authors=" + authors +
                ", categories=" + categories +
                ", bookComments=" + bookComments +
                '}';
    }
}

HibernateBookDAO.java

package tn.jebouquine.dao.hibernate;
public class HibernateBookDAO implements BookDAO {

    @Override
    public void create(Book book) {
        Session session = SessionUtilities.openSessionAndBeginTransaction();

        session.save(book);

        SessionUtilities.commitAndCloseSession(session);
    }

    @Override
    public Book retrieve(String ISBN) {
        Session session = SessionUtilities.openSessionAndBeginTransaction();

        Book book = session.get(Book.class , ISBN);

        SessionUtilities.commitAndCloseSession(session);
        return book;
    }

    @Override
    public void update(Book book) {
        Session session = SessionUtilities.openSessionAndBeginTransaction();

        session.update(book);

        SessionUtilities.commitAndCloseSession(session);
    }

    @Override
    public void delete(Book book) {
        Session session = SessionUtilities.openSessionAndBeginTransaction();

        session.delete(book);

        SessionUtilities.commitAndCloseSession(session);
    }
}

BookTest.java

public class BookTest {


   private Book book;
    private static BookDAO bookDAO;
    private static Logger logger;
    @BeforeClass
    public static void beforeClass() {
        bookDAO = new HibernateBookDAO();
        logger = LoggerFactory.logger(BookTest.class);
    }
    @AfterClass
    public static void afterClass(){
        bookDAO = null;
        logger = null;
    }

    @Before
    public void setUp() {
        Author author = new Author();
        author.setName("The lonely developer");
        book = new Book();
        book.setISBN("isbn:9780137081073");
        book.setName("The Adventures of the little guy in Java persistance world: Testing At midnight for the next saturday :(");
        book.setPrice(new BigDecimal("10.00"));
        book.getAuthors().add(author);

    }

    @Test
    public void createTest() {
        bookDAO.create(book);
    }

    @Test
    public void retrieveTest() {
        book.setISBN("isbn:9780137081074");
        bookDAO.create(book);
        Book book1 = bookDAO.retrieve(book.getISBN());
        assertEquals(book, book1);
    }

All the other classes have the equals and the hashcode implemented. I am using gradle to build the project.

fabian
  • 67,623
  • 12
  • 74
  • 102
LonsomeHell
  • 543
  • 1
  • 7
  • 26
  • please try printing the two objects and compare the values of each field. May be a null changed to empty string.... – Jos Oct 25 '15 at 17:08
  • 2
    I reckon the collections might also be a problem. Hibernate doesn't guarantee the order of the items in a collection, unless you specify the order... And the items in the collection must also override `equals` and `hashcode`. – Augusto Oct 25 '15 at 17:12
  • I already printed the objects and they are the same. – LonsomeHell Oct 25 '15 at 18:19
  • @Augusto All the classes implement `equals` and `hashcode`. I tried comparing the objects without persistence and the equals method always returns true. – LonsomeHell Oct 25 '15 at 18:22
  • Debug your equals method to see what's different? – Florian Schaetz Oct 25 '15 at 19:24

1 Answers1

0

Problem solved. While mapping a List which is ordered it turns out I need to specify a column to be used for the ordering. So the final result looks something like this.

@ManyToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
@OrderColumn(name="INDEX")
@NotEmpty
private List<Author> authors = new ArrayList<Author>();

Or alternatively I could have used a Set which is not ordered.

LonsomeHell
  • 543
  • 1
  • 7
  • 26