简体   繁体   中英

Spring Data JPA Query - class projection containing another custom object

Suppose I have a Foo entity (fields: id , name and barId ) and a Bar entity (fields: id , age ), in a one-to-many relation. I want to select all the Foo s, having the corresponding Bar age attached.

I see 2 options, but none seems to work:

  1. The so-called closed projection approach
@Query("select f as foo, b.age as age from Foo f inner join Bar b on f.barId = b.id")
List<FooWithAge> query1();

where FooWithAge is an interface having as methods Foo getFoo() and Integer getAge() ;

  1. The so-called class projection approach
@Query("select new FooWithAge(new Foo(f), b.age) from Foo f inner join Bar b on f.barId = b.id")
List<FooWithAge> query2();

where FooWithAge is a class having as fields Foo foo and Integer age and a constructor that takes these 2 as parameters.

None of these solutions seem to work. A third solution, of taking all the fields of Foo as parameters of the constructor of FooWithAge works, but I would like to find a cleaner solution.

Note: I cannot use anything else than vanilla Spring Data JPA queries. Native queries are excluded.

  1. For foo object you have to define a sub-interface:
public interface FooWithAge {
 
    String getAge();
    FooView getFoo();
     
    interface FooView {
        //list of getters of foo
    }
}
  1. In the first parameter you are creating a foo in a foo, so basically you have just to pass f :
@Query("select new FooWithAge(f, b.age) from Foo f inner join Bar b on f.barId = b.id")
List<FooWithAge> query2();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM