Nathan Q
Nathan Q

Reputation: 1902

Hibernate query on custom collection

I have a hibernate mapping with custom collection types in it.

java:

class Policy {
...
private HistoryMap<Date, PolicyStatus> statusHistory;
...
}

hbm.xml:

    <map name="statusHistory" inverse="true" table="tbl_policy_in_time" order-by="PolIt_ValidFrom desc"
                    collection-type="be.pensionarchitects.admindb.infrastructure.hibernate.HistoryMapType">
                ...
    </map>

HistoryMapType is an implementation of interface HistoryMap and implements UserCollectionType.

This map has a method getCurrent() which returns the current PolicyStatus.

Now I need to do a query to get all Policy objects having a specific PolicyStatus as current one.

Something like:

Criteria crit = getSession().createCriteria(Policy.class)
                .createAlias("statusHistory.current", "status")
                .add(Restrictions.or(
                        Restrictions.eq("status.code", "active"),
                        Restrictions.eq("status.code", "sleeper")));

I understand this doesn't work as "current" isn't an association mapping. How i should I solve this? I have read that I should use HQL instead, but have no idea how and if it's even possible.

Any pointers are appreciated!

Thanks

Upvotes: 0

Views: 525

Answers (1)

Shivan Dragon
Shivan Dragon

Reputation: 15229

If your HistoryMap implements java.util.Map (and respects the interface's contracts) then you can use Criteria API. If not, I don't think it will work with HQL (and with Criteria API). In that case, using native SQL is your best bet. You can make a native query who's result is marshaleld to a specific Hibernate Bean:

http://docs.jboss.org/hibernate/core/3.3/reference/en/html/querysql.html#d0e13696

Upvotes: 1

Related Questions