hibernate创建错误的查询?

时间:2010-09-07 09:22:48

标签: java

嗨,基本上我有这个映射:

@Entity
@Table(schema="xas",name="billing")

public class Billing implements Serializable{

private String id;

private List<Subtotal> subtotals = new ArrayList<Subtotal>(3);

@Id
@Column(name = "id", nullable = false,length=32)
public String getId() {
    return id;
}

.....

public void addSubtotal(Subtotal subtotal) {
    subtotals.add(subtotal);
}

@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinColumn(name="billing_id")
@IndexColumn(name="idx")
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
public List<Subtotal> getSubtotals() {
    return subtotals;
}

public void setSubtotals(List<Subtotal> subtotals) {
    this.subtotals = subtotals;
}

}

当我查询结算时:

Criteria criteria = session.createCriteria(Billing.class);
        criteria.add(Restrictions.in("id", ids));
        List<Billing> list = criteria.list();

我基本上得到了这个查询,它将检索我而不是一个结算二:

    select *
from oopfmobiles.billing this_ 
left outer join Subtotal subtotals2_ on this_.id=subtotals2_.billing_id 
where this_.id in ('an_id')

怎么了?提前谢谢。

1 个答案:

答案 0 :(得分:0)

ids变量的内容是什么? 您是否两次收到相同的结算实例?

如果是这样,您必须确保使用DistinctRootEntityTransformer。