用于返回特定字段的Spring数据JPA

Gil*_*ert 13 spring jpa spring-data-jpa

Spring Data是否有返回特定字段的机制?

我熟悉语法:

Invoice findByCode(String code);
Run Code Online (Sandbox Code Playgroud)

这个怎么样:

Integer findIdByCode(String code);
Run Code Online (Sandbox Code Playgroud)

它只返回id字段.要么

Tuple findIdAndNameByCode(String code);
Run Code Online (Sandbox Code Playgroud)

它返回一个元组.要么

Invoice findIdAndNameByCode(String code);
Run Code Online (Sandbox Code Playgroud)

返回仅填充特定字段的实体.可以使用构造函数仅在定义时使用那些字段 - 否则构造为空并填充字段.

编辑

为了获得更多资格,我知道@Query,构造函数表达式和现在的@Na​​medEntityGraph等解决方案.我的问题很简单 - Spring数据是否像我建议的那样支持如此简短的语法?

如果没有,也许对于以后的版本来说这是一个很酷的增强...

我不是在寻找解决方法.

Rob*_*roj 4

您可以使用JPQL 构造函数表达式

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

构造函数名称必须是完全限定的