MVC Webアプリケーションの通常のテストをいくつか書き、findById()テストで停止しました。 私のモデルクラス:hibernateエンティティに正しくどのように注釈を付けるか
@Entity
public class Product {
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String description;
private double purchasePrice;
private double retailPrice;
private double quantity;
@ManyToOne
@JoinColumn (name = "supplier_id")
private Supplier supplier;
@ManyToOne
@JoinColumn (name = "category_id")
private Category category;
@Entity
public class Category {
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String description;
@LazyCollection(LazyCollectionOption.FALSE)
@OneToMany
@Cascade(org.hibernate.annotations.CascadeType.ALL)
private List<Product> products;
@Entity
public class Supplier {
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@LazyCollection(LazyCollectionOption.FALSE)
@Cascade(org.hibernate.annotations.CascadeType.ALL)
@OneToOne
private Contact contact;
@LazyCollection(LazyCollectionOption.FALSE)
@OneToMany
private List<Product> products;
そして、私のテストコード:
private Product productTest;
private Category categoryTest;
private Supplier supplierTest;
@Before
public void setUp() throws Exception {
categoryTest = new Category("Test category", "", null);
supplierTest = new Supplier("Test supplier", null, null);
productTest = new Product("Test product","", 10, 20, 5, supplierTest, categoryTest);
categoryService.save(categoryTest);
supplierService.save(supplierTest);
productService.save(productTest);
}
@Test
public void findById() throws Exception {
Product retrieved = productService.findById(productTest.getId());
assertEquals(productTest, retrieved);
}
あなたが見ることができるだけでなく、アサーションは、理由差product.category.productsとproduct.supplier.productsの特性のために、失敗しました写真: 1つの製品にはnullがあり、もう1つは{PersistentBag}です。 確かに私は、これらのプロパティを無視するカスタムequalsメソッドを書いて簡単にハックすることができますが、それが最良の方法ではないことは確かです。
なぜ、これらのフィールドが異なるのですか? エンティティフィールドの注釈が正しく解決されていることを確信しています。