论坛首页 Java版 Hibernate

请教一下这个操作的实现

浏览 4739 次
该帖已经被评为精华帖
作者 正文
时间:2004-03-31
有三个类,order item product
item做为order的一个组件
在item关联product
item和product的多对一关系
就是第十七章的第三个例子
我现在的开发的和这例子很像,
现在就是对这个组件的操作的效率非常的低下

我是采用遍历order中的item,在item中取出product和传进来的product

代码如下:

[code:1]
public void removeProduct(Order order, Product product){
List items = order.getItems();

for(Iterator iter = litems.iterator(); iter.hasNext();){
LineItem lineItem = (LineItem)iter.next();
Product pro = lineItem.getProduct();
if(product.equals(pro)){
iter.remove();
}
}
update(order);
}[/code:1]
本来想用hql来实现
但是在product中无法关联到item因为item是做为order的一个组件出现的
所以不知道hql该如何来写.所以就用了上面比较低效的方法

请问大家有没什么更好的实现方式和建议,谢谢!
   
时间:2004-04-01
Please try like follows:

hibernate-mapping:
[code:1]
<class name="LineItem" ......>
......
<many-to-one name="prodcut" column="product_id" />
</class>
[/code:1]

PO:
[code:1]
Class LineItem {
......
Integer orderID;
Product product;
}

Class Product {
Integer productID;
}
[/code:1]

DAO:
[code:1]
String DELETE_PRODUCT_IN_ORDER =
"from LineItem as item " +
"where item.orderID=? and item.product.productID=? ";

......

Integer orderID = ...;
Integer productID = ...;

session.delete(DELETE_PRODUCT_IN_ORDER, new Object[] {orderID, ProductID},
new Type[] {Hibernate.INTEGER, Hibernate.INTEGER} );

......
[/code:1]
   
0 请登录后投票
时间:2004-04-01
映射文件是这样的,LineItem是没有映射文件的,
是做为Order的一个组件,而组件只能有一个父类就是order,product不可能再成为LineItem的父类.
[code:1]
<class name="Order" table="orders">
<id name="id">
<generator class="native"/>
</id>
<property name="date"/>
<many-to-one name="customer" column="customer_id"/>
[color=red]<list name="lineItems" table="line_items" lazy="true">[/color]
<key column="order_id"/>
<index column="line_number"/>
<composite-element class="LineItem">
<property name="quantity"/>
<many-to-one name="product" column="product_id"/>
</composite-element>
</list>
</class>

<class name="Product" table="products">
<id name="id">
<generator class="native"/>
</id>
<property name="serialNumber"/>
</class>[/code:1]
   
0 请登录后投票
时间:2004-04-01
Even no mapping of lineitem, but you have LineItem class like:
[code:1]
public class LineItem {
private Integer orderID;
private Integer lineNo;
private Integer productID;
.....
}
[/code:1]

that's more simple, just use HQL:
[code:1]
session.delete("from LineItem as item where orderID=? and productID=?", .....);
[/code:1]
   
0 请登录后投票
时间:2004-04-01
thx!
I will try:)
   
0 请登录后投票
时间:2004-04-01
"from Item"执行不了,
但是"from Order,Order. item"可以,
试了好多种hql的组合都没有办法取出(elements)Item
   
0 请登录后投票
时间:2004-04-02
chengld 写道
Even no mapping of lineitem, but you have LineItem class like:
[code:1]
public class LineItem {
private Integer orderID;
private Integer lineNo;
private Integer productID;
.....
}
[/code:1]

that's more simple, just use HQL:
[code:1]
session.delete("from LineItem as item where orderID=? and productID=?", .....);

[/code:1]


质疑,在LineItem中存orderID和productID合适吗
不是应该这样?

[code:1]public class LineItem {
private Order order;
private Integer lineNo;
private Product product;
.....
}[/code:1]
   
0 请登录后投票
时间:2004-04-02
Feiing 写道


质疑,在LineItem中存orderID和productID合适吗
不是应该这样?

[code:1]public class LineItem {
private Order order;
private Integer lineNo;
private Product product;
.....
}[/code:1]


That's up to you, if you don't care do a query that link 3 tables when you just need load LineItems.
   
0 请登录后投票
时间:2004-04-02
引用
[code:1]
session.delete("from LineItem as item where orderID=? and productID=?", .....);
[/code:1]

这句执行不了
我在测试的时候,
"from LineItem"会报undefine alias 的错误
大概是因为没有hbm的原因
"select elements(oreder.lineItem) from order"是可以的
但是如果子句中出现order.lineItem.productid之类的就会报:expecting 'elements' or 'indices'的错误

所以用hql还没有实现:(.
   
0 请登录后投票
时间:2004-04-02
Could you paste your whole hibernate mapping, po class and hql query here, I try to figure it out this weekend.
   
0 请登录后投票
论坛首页 Java版 Hibernate

跳转论坛: