14

Does Spring Data have a mechanism for returning specific fields?

I'm familiar with the syntax:

Invoice findByCode(String code);

How about this:

Integer findIdByCode(String code);

which returns the id field only. Or

Tuple findIdAndNameByCode(String code);

which returns a tuple. Or

Invoice findIdAndNameByCode(String code);

which returns an entity only populated with specific fields. Can use a constructor taking only those field if defined - else construct empty and populate the fields.

EDIT

To qualify some more, I'm aware of solutions like @Query, constructor expressions and now, @NamedEntityGraph. My question is simply - does Spring data support such a shorthand syntax as I'm suggesting?

If not, perhaps this is a cool enhancement for a later version...

I'm not looking for workarounds.

Gilbert
  • 3,584
  • 2
  • 26
  • 30
  • See the [reference guide](http://docs.spring.io/spring-data/jpa/docs/1.8.0.RELEASE/reference/html/#jpa.entity-graph) requires JPA 2.1 and the newest Spring Data JPA version. – M. Deinum May 22 '15 at 13:21
  • NamedEntityGraph is a useful feature, but more verbose that using @Query to solve my scenarios. I'd like to see a solution without any annotations. – Gilbert May 22 '15 at 14:14
  • How is `@Query` not an annotation.. So instead of using a default solution you want to work around that. Either way you have to do something to return the subset of data you want, using the default it is reusable when doing a query you have to repeat everything each time and create a lot of different constructors. – M. Deinum May 22 '15 at 18:20
  • As a side note, I'm using Hibernate, and that does not limit the fields returned: https://hibernate.atlassian.net/browse/HHH-9270 – Gilbert May 23 '15 at 03:45
  • 2
    No there is no shorthand for that, that would only work with a specific query or specification. – M. Deinum May 23 '15 at 12:55

4 Answers4

4

You can use JPQL Constructor Expressions:

SELECT NEW com.company.PublisherInfo(pub.id, pub.revenue, mag.price)
    FROM Publisher pub JOIN pub.magazines mag WHERE mag.price > 5.00

The constructor name must be fully qualified

Robert Niestroj
  • 15,299
  • 14
  • 76
  • 119
  • Check out projections now too, not quite exactly what you want, but closer - http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections – chrismarx Sep 02 '16 at 18:45
1

If you want to return just 1 field from table and it's primitive(or autoboxing), you can use next:

@Query("select distinct t.locationId from Table t")
List<Long> findAllWashroomLocationId();

Where:

  • Table - name of class which represent your table
  • t - alias
  • locationId - name of field(in your Table object)
  • Long - type of locationId (Integer, String, ...)
Max
  • 766
  • 9
  • 19
1

Not sure if what you're trying to achieve is the same as using multiple projections on the same JPA generated query (where method name are the same). I have posted an answer in this post.

https://stackoverflow.com/a/43373337/4540216

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)
Community
  • 1
  • 1
XPLOT1ON
  • 2,994
  • 2
  • 20
  • 36
0

i have a nativequery, this is a insert and i going to return all fields after insert whit "RETURNING *"

this query return all fields of my database, and this data going to save in my entity "Perfil Detalles" my entity have all configurations of my fields of my database

@Query(
        value= "INSERT INTO \"USUARIO\".\"PERFIL_CONFIGURACION\" (id_perfil, id_group, id_role) VALUES(:id_perfil, :id_group, :id_role) returning *",
        nativeQuery = true)
        public PerfilDetalles insertPerfilDetalles(
        @Param("id_perfil") Long id_perfil,
        @Param("id_group") int id_group,
        @Param("id_role") int id_role); 
desertnaut
  • 57,590
  • 26
  • 140
  • 166