I have a class ContractRequest that contains a map of <String, String>, mapped like so:
Code:
/**
* @hibernate.map table="icd.tblcontractrequestfield"
* @hibernate.collection-key column="cnrq_id"
* @hibernate.collection-element column="cnrf_value" type="string"
* @hibernate.collection-index column="cnrf_key" type="string"
* @return
*/
public Map getFieldMap() {
if(fieldMap==null) fieldMap = new HashMap();
return fieldMap;
}
public void setFieldMap(Map fieldMap) {
this.fieldMap = fieldMap;
}
Now I'm trying to query ContractRequest by searching the items in that map, but I can't find a way to do it using Criteria. This is how I'm doing it with HQL:
Code:
public List findAllByFilter(Long reqId, int status, Calendar requested, String field) throws BusinessException {
String query = "select distinct cr from ContractRequest cr left outer join cr.fieldMap as field where cr.id = cr.id";
List params = new ArrayList();
List paramTypes = new ArrayList();
if(reqId!=null) {
query += " and cr.id = ?";
params.add(reqId);
paramTypes.add(Hibernate.INTEGER);
}
if(StringUtils.isNotEmpty(field)) {
query += " and upper(field) like ?";
params.add("%"+(field.toUpperCase())+"%");
paramTypes.add(Hibernate.STRING);
}
System.err.println(query);
Query hnquery = new HibernateFilter().getDatabaseContext().getSession().createQuery(query);
hnquery.setParameters(params.toArray(), (Type[]) paramTypes.toArray(new Type[0]));
return hnquery.list();
}
But that just seems plain wrong to me, and the query is to become a lot more complex in the future so I would like to do this using Criteria. I just can't find how to do this using Criteria. Since the collection is not an assocation, I can't use createCriteria("fieldMap") and even if I could do that I wouldn't know where to go from there.
Any help would be greatly appreciated.