2015-10-25 152 views
0

我爲我的DAO實現使用Hibernate。我創建了映射,並重寫了equals方法。對象持久化後Hibernate assertEquals失敗

當我使用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來構建項目。

+0

請嘗試打印兩個對象並比較每個字段的值。可能是null更改爲空字符串.... –

+2

我認爲集合也可能是一個問題。 Hibernate不保證集合中項目的順序,除非你指定了順序......集合中的項目也必須重寫'equals'和'hashcode'。 – Augusto

+0

我已經打印了對象,它們是一樣的。 – LonsomeHell

回答

0

問題解決。雖然映射List這是有序的,事實證明我需要指定一個列用於排序。所以最終的結果看起來像這樣。

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

或者我可以使用未訂購的Set