Criteriabuilder: projections and restrictions together

I’d like to use criteriabuilder to perform a search query. However I notice that this not work with projections.

`

`

var c = userService.newCriteria();

c.add( c.restrictions.eq(“username”,“admin”) );

var u = c
.withProjections( property=‘username,email,firstName,lastName’ )
.resultTransformer( c.ALIAS_TO_ENTITY_MAP )

.list();

`

`

I got error:

Type: org.hibernate.exception.SQLGrammarException
Messages: could not execute query

did you turn on SQL logging?

Luis Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com
P/F: 1-888-557-8057
Direct: (909) 248-3408

Linked In: http://www.linkedin.com/pub/3/731/483

Social: twitter.com/ortussolutions | twitter.com/coldbox | twitter.com/lmajano | twitter.com/gocontentbox

Hi Luis,

according this solution on stackoverflow: http://stackoverflow.com/questions/84644/hibernate-query-by-example-and-projections

I have make a change nad now works, however I’m not sure this is correct:

`

var c = userService.newCriteria();

c.add( c.restrictions.like(“this.username”,“admin”) ); // THIS LINE IS CHANGED
var u = c
.createAlias( “role”, “role” )
.withProjections( property=‘username,email,firstName,lastName,role.role’ )
.resultTransformer( c.ALIAS_TO_ENTITY_MAP )
.list();

`

I have added this.username to restrictions and now It’s ok.