Tuesday, August 31, 2010

JPA query syntax

Within a controller:

def query = entityManager.createQuery("select from Account a where a.key=:key order by ${params.sort} ${params.order}")
query.setParameter("key",session.key)
query.setMaxResults( params.max )
query.setFirstResult( params.offset )
def accounts = query.resultList;

EntityManager isn't injected in Services but jpaTemplate is so:

private getEntityManager(){
jpaTemplate.execute({EntityManager entityManager->
return entityManager;
} as JpaCallback);
}