Java 对象持久化后Hibernate assertEquals失败
我正在使用Hibernate实现DAO。我创建了映射并重写了equals方法 当我使用Java 对象持久化后Hibernate assertEquals失败,java,hibernate,gradle,junit,assert,Java,Hibernate,Gradle,Junit,Assert,我正在使用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 @Decimal
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 +
'}';
}
}
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和hashcode。我正在使用gradle构建项目。问题已解决。映射排序的
列表时,我需要指定一列用于排序。最后的结果是这样的
@ManyToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
@OrderColumn(name="INDEX")
@NotEmpty
private List<Author> authors = new ArrayList<Author>();
@ManyToMany(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
@OrderColumn(name=“INDEX”)
@空空如也
private List authors=new ArrayList();
或者,我也可以使用未排序的集合。请尝试打印两个对象并比较每个字段的值。可能是空字符串变为空字符串…我想集合也可能是个问题。Hibernate不保证集合中项目的顺序,除非指定顺序。。。集合中的项也必须重写equals
和hashcode
。我已经打印了这些对象,它们是相同的。@Augusto所有类都实现了equals
和hashcode
。我尝试比较没有持久性的对象,equals方法总是返回true。调试equals方法看看有什么不同?
@ManyToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
@OrderColumn(name="INDEX")
@NotEmpty
private List<Author> authors = new ArrayList<Author>();