3

EDIT : How to Keep only needed columns in SELECT for Spring Data Rest Projections?

Spring Data Rest Projections are good for getting a subset of columns for links which are generated, but the Query that gets generated in behind still has all columns in it.

How can Projections be created where also SQL queries have only those columns in SELECT which are in Projection

2 Answers 2

2

I don't know why it's missing from the docs, but this spring sample (from spring) shows that you can use projections as the return type for a @Query. So you can do:

public interface ActionId {
    String getId(); 
}

@Query("select a.id as id from Action a where a.type = :type")
public List<ActionId> findByType(@Param("type") String type);

Now instead of having to use constructor expressions, you can more succinctly just select the columns you want, and return an object. I wish that projections could be applied to the domain object itself, so you could still return an "Action" with just the id field, but that doesn't look possible now-

ref:https://github.com/spring-projects/spring-data-examples/blob/master/jpa/example/src/main/java/example/springdata/jpa/projections/CustomerRepository.java

Sign up to request clarification or add additional context in comments.

Comments

0

I think you can use "nested projections", let's say.

An example: resource A contains field bubi and resource B, which has field foo, bar, zed.

You should create a projection for B where you list just the fields you want:

@Projection(name="reduced", types = B.class)
public interface BReduced {
    String foo;
    //exclude bar, for instance
    int zed;
}

Then you use that projection in A's projection.

@Projection(name="reduced", types = A.class)
public interface AReduced {
    int bubi;
    BReduced b;
}

Have I undestood well, or were you talking about performance in SQL query?

5 Comments

bluish, thanks for reply but can we also have this provision that SQL that gets generated has just these columns as in projection, say for AReduced , select has only bubi, and foo, zed ?
nested Projections is one of most used feature and I totally like it, just been wondering if this could also trigger a SELECT clause change as well
In the repository you could add a method, specifying the exact SQL query with @Query("SELECT ....") annotation. So you'll have a custom query!
That is true but how do I link that SQL Query ( most likely a findQuery) to a Projection ? so that when I call /app/A/?projection=reduced , then that query gets fired for projections only. So, if I visit /app/A, then findAll() anyway will get all Columns but when I hit a projection URL then, it gets the subset only in SELECT
@fortm Any updates to SQL execution in projections??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.