持久化对象后 Hibernate assertEquals 失败

Hibernate assertEquals fails after objects are persisted

我正在使用 Hibernate 实现我的 DAO。我创建了映射并覆盖了 equals 方法。

当我使用 assertEquals(object,object) 时,即使对象相同,它也会失败。 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);
    }

所有其他 类 都实现了 equals 和哈希码。我正在使用 gradle 构建项目。

问题已解决。在映射已排序的 List 时,事实证明我需要指定一个用于排序的列。所以最终结果看起来像这样。

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

或者我可以使用未排序的 Set