简体   繁体   中英

hibernate creates wrong query?

Hi I have basically this mapping:

@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;
}

}

And when I query billing:

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

I get basically this query which will retrieve me instead one billing two:

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

What's wrong? Thank you in advance.

What is the content of the ids variable ? Do you receive the same Billing instance twice ?

If so, you'll have to make sure that you use the DistinctRootEntityTransformer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM