cape
cape

Reputation: 434

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!

Upvotes: 1

Views: 249

Answers (1)

ZaoTaoBao
ZaoTaoBao

Reputation: 2615

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

Upvotes: 1

Related Questions