Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

540
Vistas
Spring JPA: Using multiple projection on same query interface

I am trying to use Spring JPA's projection to filter out unnecessary data from query result. However, I have multiple projections that will need to be used on the same interface method.

The problem is, I am trying to query data from the same method with a different returning object but java doesn't allowed this.

The query are auto generated by JPA based on method name, so I cannot make changes to method name.

Is there a alternative, other than creating a new interface, since I think it's a hassle and unnecessary

here is a sample code, of what I am trying to do.

Auto-Generated Query

public interface UserRepository extends CrudRepository<UserAccount, Long> {

    AuthenticateProjection getByUsername(String username);

    UserDetailsProjection getByUsername(String username);

}

Projections

public interface AuthenticateProjection {

    @Value("#{target.username}")
    String getUsername();

    @Value("#{target.credentail.token}")
    String getHashPassword();
}

public interface UserDetailsProjection {

    @Value("#{target.username}")
    String getUsername();

    @Value("#{target.firstname}")
    String getFirstName();

    @Value("#{target.lastname}")
    String getLastName();
}
about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

So I've managed to figure out how to use multiple projections with a single query.

<T> T getByUsername(String username, Class<T> projection)

This allows the method caller to specified the type of projection to be applied to the query.

To further improve this so it is less prone to error, I made a blank interface that the projection will have to extend in order to be able to insert class into the parameter.

public interface JPAProjection {
}

public interface UserRepository extends CrudRepository<UserAccount, Long> {
    <T extends JPAProjection > T getByUsername(String username, Class<? extends JPAProjection> projection);
}

Projection Interface

public interface UserDetailsProjection extends JPAProjection{
    @Value("#{target.username}")
    String getUsername();

    @Value("#{target.firstname}")
    String getFirstname();

    @Value("#{target.lastname}")
    String getLastname();
}

Then I can call the query method by

getByUsername("...", UserDetailsProjection.class)
about 4 years ago · Santiago Trujillo Denunciar

0

Just add something between get (or e.g. find) and By starting with an upper case character. It is ignored in the query generation.

public interface UserRepository extends CrudRepository<UserAccount, Long> {

   AuthenticateProjection getByUsername(String username);

   UserDetailsProjection getAnotherByUsername(String username);

}
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda