1

我有一个这样的发票类:

Invoice {
int id;
Set<Attachment> attachments;}

附件类:

Attachment {
int id;
Status status;}

而且,状态类:

Status {
int id;
String desc;}

我想构建一个给定状态元素的方法,一个附件,返回所有相关的发票。

这是我的方法:

    public List<Invoice> findbyCriteria(Invoice criteria, int init,
    int pageSize, String orderBy, String ascDesc) {


    Criteria c = getSession().createCriteria(Invoice.class).
    add(Example.create(criteria));
    if(criteria.getAttachment() !=null && criteria.getAttachment().size() > 0)
    c.createCriteria("attachments").add(Example.create((Set<Attachment>)criteria.getAttachments()));    

return c.list();

但这会在创建示例期间返回 ClassCastException:

Example.create((Set<Attachment>)criteria.getAttachments()));

怎么了?

谢谢!

4

1 回答 1

1

尝试这个:

List<Invoice> invoices  = sess.createCriteria(Invoice.class)
                .add(what you need to filter)
                .createCriteria("attachments").add(what you need to filter)//like  Restrictions.like("name", "F%")
                .createCriteria("status").add(what you need to filter).list();
于 2013-09-17T09:03:59.210 回答