2

I have an entity that has multiple fields; one of the fields is "quantifications", which holds a collection of different entities.

I'm querying for this entity in an API controller call that returns a list via JSON. I want to filter the result set I'm returning by the following criteria:

  1. If the "quantifications" field is empty, don't return that entity as part of the result.
  2. If the "quantifications" field is not empty, only return entities where all Quantification entities in the collection have a status of "approved".

Coldfusion isn't a language I'm terribly familiar with. I have the following code that meets the first criterion:

var EventCriteria = EventService.newCriteria();
EventCriteria.isNotEmpty('quantifications');

How can I check for the second criteria? Thank you.

Edit: Okay, I've been trying a few things, and I'm running into "method not found" errors. Posting the code below.

if (NOT showEventsWithoutQuantifications){
    EventCriteria.isNotEmpty('quantifications');

    var eventStatus = eventStatusService.findWhere(entityName="EventStatus", criteria={eventStatusOrder=javaCast( "int", 150 )}); //approved

    // According to the Coldbox documentation, this should have worked. It doesn't.
    // var QuantificationService = quantificationService.newCriteria()
    //      .isEq("Quantification.status", eventStatus)
    //      .withProjections(property="event.eventID");
    // EventCriteria.in("eventID", QuantificationService); // .in() method not found. Why?
    // EventCriteria.add(EventCriteria.restrictions.in("eventID", QuantificationService)); // .in() method not found. Why?

    EventCriteria.add(wmtEventCriteria.createSubcriteria('Quantifications').isEq("status", eventStatus));  // .createSubcriteria method () method not found. Why?
}

For what it's worth, the EventService inherits from coldbox.system.orm.hibernate.VirtualEntityService. I haven't found the code for that class yet (as I'm not using a native CF IDE for this) but I would imagine that this should expose the methods in question...?

Could this be an issue with the Hibernate version I'm running against? Or perhaps the version of Coldbox?

George Mauer
  • 117,483
  • 131
  • 382
  • 612
  • The language is CFML; ColdFusion is the product. Coldbox is an MVC framework. The ORM used is Hibernate. – Peter Boughton Nov 16 '13 at 22:11
  • Coldbox provides a [wrapper](http://wiki.coldbox.org/wiki/ORM:CriteriaBuilder.cfm) on the Hibernate [Criteria/Restrictions](https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querycriteria.html) functionality, so you mainly need to understand that rather than CFML, however I've [tried to] use Criterias a couple of times and found the whole thing a pain to work with - far simpler to just write an SQL (or HQL) query directly against the database. – Peter Boughton Nov 16 '13 at 22:12
  • @PeterBoughton Yeah. Unfortunately, I'm dealing with inherited code. I'm wondering if there's, like, an `each()` or `all()` call that I can use within the criteria for a collection field, similar to LINQ. Ideally, I'd like to do something like `EventCriteria.Where(x => x.quantifications.All(y => y.Status == true))`, but in this Coldbox format. (That LINQ statement isn't exact.) –  Nov 16 '13 at 22:24
  • The docs are here: http://wiki.coldbox.org/wiki/ORM:CriteriaBuilder.cfm - you might be able to get that with `EventCriteria.withQuantifications.isTrue("status")` or using a [sub-criteria](http://wiki.coldbox.org/wiki/ORM:DetachedCriteriaBuilder.cfm) like `EventCriteria.add( EventCriteria.createSubCriteria('Quantifications').filters )` – Peter Boughton Nov 16 '13 at 22:49
  • But still, unless you've got an existing large criteria set you have to append this to, I'd be more inclined to go with a simple query, like [`OrmExecuteQuery`](http://cfdocs.org/ormexecutequery)`( "From Event e, Quantifications q WHERE e.id = ? AND q.status = true" , [EventId] )` or whatever. – Peter Boughton Nov 16 '13 at 22:49
  • @PeterBoughton Unfortunately, I do. The budget time for this change is low, so I can't really afford to change around a lot of code. –  Nov 16 '13 at 22:52
  • 1
    re: _"I haven't found the code for that class yet"_ - the VirtualEntityService is mostly just a wrapper around [BaseOrmService](https://github.com/ColdBox/coldbox-platform/blob/master/system/orm/hibernate/BaseORMService.cfc) (but not sure that's relevant here - the [newCriteria](https://github.com/ColdBox/coldbox-platform/blob/master/system/orm/hibernate/BaseORMService.cfc#L1391) call simply creates a CriteriaBuilder instance). Anyhow, not sure why I didn't mention this before, but you'll probably get a better response from the [Coldbox mailing list](http://groups.google.com/group/coldbox). – Peter Boughton Nov 29 '13 at 22:25
  • @PeterBoughton Thanks for the suggestion; I've added a post there. –  Nov 30 '13 at 02:34

1 Answers1

0

Regarding the "method not found" issues, it's simply a naming issue, partly a fault from ColdBox on Adobe CF.
In situations like this, use the alias methods. For .in(), use isIn() instead.

At the bottom of the page below you can see the different aliases. https://github.com/ColdBox/coldbox-platform/blob/master/system/orm/hibernate/criterion/Restrictions.cfc

  • Unfortunately, the `isIn()` methods didn't work either - I did try all of those aliases. –  Dec 04 '13 at 00:14