我有一些类似以下的关系:
@Entity
@Table(name="customer")
public class Customer
{
@Id
@Column(name="id")
private int id;
@OneToMany(fetch=FetchType.EAGER, mappedBy="customer", cascade=CascadeType.ALL)
private List<OrderedItem> items;
public List<OrderedItem> getItems ()
{
return items;
}
public void setItems (List<OrderedItem> _items)
{
items = _items;
}
}
@Entity
@Table(name="ordereditem")
@IdClass(OrderedItemPK.class)
public class OrderedItem
{
@Id
@ManyToOne
@JoinColumn(name="id_customer")
private Customer customer;
@Id
@ManyToOne
@JoinColumn(name="id_item")
private Item item;
public Customer getCustomer ()
{
return customer;
}
public void setCustomer (Customer _cust)
{
customer = _cust;
}
public Item getItem ()
{
return item;
}
public void setItem (Item _item)
{
item = _item;
}
}
@Entity
@Table(name="item")
public class Item
{
@Id
@Column(name="id")
private int id ;
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(name="itemcategory",
joinColumns=
@JoinColumn(name="id_item", referencedColumnName="id"),
inverseJoinColumns=
@JoinColumn(name="id_category", referencedColumnName="id")
)
private List<Category> categories ;
public int getId ()
{
return id;
}
public void setId (int _id)
{
id = _id;
}
public List<Categories> getCategories ()
{
return categories;
}
public void setCategories (List<Category> _categories)
{
categories = _categories;
}
}
@Entity
@Table(name="category")
public class Category
{
@Id
@Column(name="id")
private int id;
public int getId ()
{
return id;
}
public void setId (int _id)
{
id = _id;
}
}
public class OrderedItemPK implements Serializable
{
private int customer ;
private int item ;
public int getCustomer ()
{
return a;
}
public void setCustomer (int _cust)
{
customer = _cust;
}
public int getItem ()
{
return item;
}
public void setItem (int _item)
{
item = _item;
}
public int hashCode() {
return customer + item;
}
public boolean equals(Object _o) {
OrderedItemPK other = (OrderedItemPK) _o;
return (customer == other.customer) && (item == other.item);
}
}
仓库:
public interface CustomerRepo extends JpaRepository<Customer, Integer> {
}
服务:
@Service
@Transactional
public class CustomerSvc
{
@Autowired
CustomerRepo repo;
public Customer findCustomer(int _id) {
return repo.findOne(_id);
}
public List<Customer> findAllCustomer() {
return repo.findAll();
}
}
这是数据库中的内容:
customer table:
id
-----
1
2
3
ordereditem table:
id_customer id_item
-------------------------
1 1
1 2
1 3
item table:
id
-----
1
2
3
category table:
id
----
1
2
3
itemcategory table:
id_item id_category
-----------------------
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
我如何使用服务:
Customer c = custSvc.findOne(1);
// this prints Number of ordered items: 9
System.out.println("Number of ordered items: " + c.getItems().size());
List<Customer> custs = custSvc.findAll();
c = custs.get(0);
// this prints Number of ordered items: 3
System.out.println("Number of ordered items: " + c.getItems().size());
正确的数量应为3,ID为1的客户有3个OrderedItems,这3个OrderedItems每个都有1个Item,每个Item都有3个类别。
我想知道为什么如果我使用Spring Data中的findOne之类的方法和EntityManager中的find方法,为什么它只会给错误的子级;如果我使用Spring Data中的findAll或findByAttribute之类的方法,为什么它正确地给出了正确的子级。
最佳答案
我已经在stackoverflow中找到了类似情况的答案,但是要找到它并不容易,因为我需要通过大约40个结果才能在这里找到想要的答案。希望其他有类似问题的人也可以发贴。
JPA - EntityManager find method returns duplicates