2017-04-05 78 views
0

我尝试在连接列上使用Spring数据JPA投影,但没有成功。春天的数据jpa:在投影字段中添加where子句

考虑以下类:

interface CarProjection { 
    String getName(); 
    List<String> getColorsLabel(); 
} 

class Car{ 
    String name; 

    @OneToMany(mappedBy = "car") 
    List<Color> colors; 
} 

class Color{ 
    String label; 

    @ManyToOne 
    Car car; 
} 

而下面的仓库方法:

List<CarProjection> findAllBy(); 

万物运作良好,并弹簧产生以下查询:

[....] left outer join color colors1_ on car0_.id=colors1_.car_id 

我想什么do是在这个连接上添加一个where子句,例如:

[....] left outer join color colors1_ on (car0_.id=colors1_.car_id AND colors1_.location='EXTERIOR') 

在投影类,它会看到这样的:

interface CarProjection { 
    String getName(); 
    @Where("label=='EXTERIOR'") 
    List<String> getColorsLabel(); 
} 

我知道我可以使用一个SPEL表达在@Value做到这一点,但随后春天将获取实体的所有列而不仅仅是投影投影(开放投影)。

我也试过一个JPA规范在我的仓库投影相结合:

List<CarProjection> findAllBy(Specification<CarProjection> specification); 

但似乎我们不能混用规格和预测。

任何想法如何做到这一点?谢谢:)

+0

FWIW Spring没有生成任何查询。你的JPA提供者确实。 –

回答