[ColdBox 3.7.0] Orm: get only certain property from my object

I would like to get only id and name from my object.

I have this code:

`

property name=“userService” inject=“entityService:User”;

userService.list(asQuery=“false”,max=10);

`

This will output all the User property. How can I retrieve only id and name?

Thanks

Hi Mark–

You could use a property project via CriteriaBuilder:

Sorry, hit the wrong button :slight_smile:

Here’s the docs: http://wiki.coldbox.org/wiki/ORM:CriteriaBuilder.cfm#Projections

And here’s an example:

var c = UserService.newCriteria();
c.withProjections( property=“id,name” );
var results = UserService.list( asQuery=false, criteria=c, max=10 );

Be aware, though, that this will no longer return the entity objects from list, but rather an array of arrays with positional values for each requested property.

Hope this helps!

I got this error:

`
invalid name, there is no property with name [getEntityName] in the entity [User]
valid properties names are [name, image, creationDate, surname, reports]

`

To add to Joel, you can use the following to get an array of structs:

.resultTransformer( c.ALIAS_TO_ENTITY_MAP )

That tells hibernate to build an array of structs according to the aliases of the properties. You can also create aliases to the properties

c.withProjections( property=“id:userID,name:userName” )

This will create an array of structs with userID and userName as the key names.

signature0.jpg

Luis F. Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com

Social: twitter.com/lmajano facebook.com/lmajano

I got an error that chenge at each refresh:

invalid name, there is no property with name [SETNATIVECRITERIA] in the entity [User]
valid properties names are [dateCreated, lastUpdated, firstName, lastName, userName, password, email, lastLogin, usernamePasswordHash, emailPasswordHash, passwordHasReset, active, roles, reports, parent]

invalid name, there is no property with name [setCriterias] in the entity [User]
valid properties names are [dateCreated, lastUpdated, firstName, lastName, userName, password, email, lastLogin, usernamePasswordHash, emailPasswordHash, passwordHasReset, active, roles, reports, parent]

invalid name, there is no property with name [LEFT_JOIN] in the entity [User]
valid properties names are [dateCreated, lastUpdated, firstName, lastName, userName, password, email, lastLogin, usernamePasswordHash, emailPasswordHash, passwordHasReset, active, roles, reports, parent]