我有兩個實體Customer
和Product
,關係爲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
?
我認爲在性能方面會更好。它發送和接收的數據較少。我錯了還是微不足道? – user1137146 2012-04-07 09:23:24
但是你打算如何處理ID? – JMelnik 2012-04-07 10:54:15
我這樣理解。當在Customer類的hibernate中調用addProduct方法時,會在customer_products表中添加一行,並且它唯一添加的列是來自Customer和Table的表的ids。所以基本上我不需要選擇其餘的數據。 – user1137146 2012-04-07 11:07:24