我有两个 EntityCustomer
和Product
with relation many-to-many
。
`
@ManyToMany(cascade = CascadeType.MERGE, fetch=FetchType.LAZY)
@JoinTable(
name = "customer_products",
joinColumns={@JoinColumn(name="customer_id")},
inverseJoinColumns = {@JoinColumn(name="product_id")})
@CollectionId(
columns = @Column(name="id"),
type = @Type(type="long"),
generator = "native"
)
public Collection<Product> getProducts() {
return products;
}
public void addProduct(Product product){
this.products.add(product);
}
public void setProducts(Collection<Product> products) {
this.products = products;
}
`
我想添加客户产品
`
Customer customer = (Customer)session.load(Customer.class, new Long(1));
Product product = (Product) session.load(Product.class, new Long(1));
customer.addProduct(product);
session.persist(customer);`
代码所做的是,它从表中选择所有列,Customer
并Products
仅从ids
这些表中插入到customer_products
表中。有没有办法只ids
从上面的表格中选择?