简体   繁体   中英

Hibernate criteria subclass attribute

I have an Invoice Class like this:

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

The Attachment class:

Attachment {
int id;
Status status;}

And, The Status class:

Status {
int id;
String desc;}

I want to build a method that given a status element, of an Attachment, return all related invoices.

This is my method:

    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();

But this returns a ClassCastException during creation the Example:

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

What is wrong?

Thanks!

try this:

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();

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