Hibernate标准子类属性

时间:2013-09-17 08:50:01

标签: java hibernate

我有一个这样的发票类:

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

附件类:

Attachment {
int id;
Status 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();

但是在创建Example:

期间会返回ClassCastException
Example.create((Set<Attachment>)criteria.getAttachments()));

有什么问题?

谢谢!

1 个答案:

答案 0 :(得分: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();