Question

I've been trying to do a join query with cayenne but I'm getting stuck with the use of Expressions and that. In sql it would be something like this:

SELECT *
FROM usuarios, rol, user_rol
WHERE usuarios.cod_usuario = user_rol.cod_usuario
AND user_rol.cod_rol = rol.cod_rol

This would be the basic. Thanks in advance

Était-ce utile?

La solution

The answer to that, just like with the other question that you had is to use relationships. Once you map the relationships between Usuarios / UserRol and UserRol / Rol, you can either traverse the relationship from the user object by calling its methods:

Usuarios u = ..
for(UserRol ur : u.getUserRols()) {
    Rol r = ur.getRol();
    // now do something with it
}

Or find Rols with a query matching that user:

SelectQuery query = new SelectQuery(Rol.class);
query.andQualifier(ExpressionFactory.matchExp("userRol.user", u);
List<Rol> roles = context.performQuery(query);

Path argument of 'ExpressionFactory.matchExp' (i.e. "userRol.user" String) should be adjusted to match the actual relationship names starting at the Rol entity (as Rol is the root of the query), going to UserRol, and from that to Usuarios.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top