ColdBox 3.6 - Criteria Builder Casting Question

Hi folks,

I have the following Criteria Builder query:

var c = ORMService.newCriteria( ‘Foo’ );
var partnerObjects = c.in(“FooID”,arguments.restrict).list();

arguments.restrict is a comma separated list of ID’s, so essentially I only want the partnerObjects which are IN the list of ID’s.

This throws the following error:

java.lang.String cannot be cast to java.lang.Integer

I remembered about the casting concerns in criteriaBuilder and tried modifying the query to be:

var c = ORMService.newCriteria( ‘Foo’ );
var partnerObjects = c.in(“FooID”,c. convertIDValueToJavaType (arguments.restrict)).list();

however this threw an error saying that convertIDValueToJavaType() was not found in c or may be overloaded.

I guess I’m not structuring this query correctly. Can someone advise what i may be doing incorrectly?

Thanks.

Nolan

Use ORMService.convertIDValueToJavaType() not c.

That should fix that.

Curt

Hi Curt,

Thanks for your email.

I got it to work by doing this:

var c = ORMService.newCriteria( ‘Foo’ );
local.restrict = ORMService.convertIDValueToJavaType(id=arguments.restrict,entityName=‘Foo’);

var fooObjects = c.in(“FooID”,local.restrict).list();

The CriteriaBuilder docs are a bit confusing.

For instance the line that indicates - // Base ORM Service

c = newCriteria( 'entityName' );

should really be:

// Base ORM Service
c = ORMService.newCriteria( 'entityName' );

Should it not?

And if you’re following the progression of the page and examples and the use of “c” as a variable, this…

You can also find these methods in the Base ORM services and Virtual Entity Services.

c.convertIDValueToJavaType( id = 123 );

c.convertIDValueToJavaType( id = ["1","2","3"] );

c.convertValueToJavaType(propertyName="id", value=arguments.testUserID)

should actually be this:

C = newCriteria() would work if you are already in a base or virtual entity service that extends the coldbox ones.

ormService.newCriteria() would work if you have an object injected based on the ORMServices DSL.

So it depends on the context.

The criteria you are creating when you do newCriteria() isn’t the same as an entityService, but a method of it.

Hope that makes sense.

Curt

Oh, and no CF Summit for me. It didn’t work out date wise.

Curt