弹簧数据 JPA 投影 从 DB 中选择的字段

我正在测试Spring Data 1.10.4.RELEASE,遵循Spring Data Docs中的示例 http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections

我注意到一些问题,我有2个问题。

首先,假设我有以下 2 个实体:

@Entity
public class Person {

  @Id @GeneratedValue
  private Long id;
  private String firstName, lastName;

  @OneToOne
  private Address address;
}

@Entity
public class Address {

  @Id @GeneratedValue
  private Long id;
  private String street, state, country;
}
  • 问题 1:

对于以下预测:

interface PersonLimited {  

  String getFirstName(); 

  AddressLimited getAddress();
}

interface AddressLimited {  

  String getCountry(); 
}

当我运行 findPersonByFirstNameProjectedForLimitedData 时

interface PersonRepository extends CrudRepository<Person, Long> {

  @Query("select p from Person p where p.firstName = ?1")
  PersonLimited findPersonByFirstNameProjectedForLimitedData(String firstName);
}

它完全返回预期的结果:

{
    firstName: 'Homer',
    address: {
        country: 'USA'
    }
}

现在,如果我查看生成的SQL,这就是我所拥有的:

SELECT person0_.firstName      AS col_0_0_, 
       address1_.id            AS id1_13_, 
       address1_.street        AS street2_13_, 
       address1_.state         AS state3_13_, 
       address1_.country       AS country4_13_
FROM   person person0_ 
       LEFT OUTER JOIN address address1_ 
                    ON person0_.addressId = address1_.id 
WHERE  person0_.firstName = ?  

“Person”实体的投影仅选择“fistName”,这是100%正确的,因为在PersonLimited界面中,我只定义了“getFirstName”。

但是对于“地址”实体,它选择所有字段,这是错误的,因为在AddressLimited界面中,我只定义了“getCountry”,它应该只选择“国家”。。

生成的查询应类似于:

SELECT person0_.firstName      AS col_0_0_, 
       address1_.country       AS country4_13_
FROM   person person0_ 
       LEFT OUTER JOIN address address1_ 
                    ON person0_.addressId = address1_.id 
WHERE  person0_.firstName = ?  

所以问题是,为什么它不只为地址“实体”选择“国家”字段?为什么需要选择所有字段?春天是虫子吗?

  • 问题 2:

对于与上述相同的投影,

当我运行 findAllPersonsProjectedForLimitedData 时

interface PersonRepository extends CrudRepository<Person, Long> {

  @Query("select p from Person p")
  List<PersonLimited> findAllPersonsProjectedForLimitedData();
}

它完全返回预期的结果:

[
     {
        firstName: 'Homer',
        address: {
            country: 'USA'
        }
     },
     {
        firstName: 'Maggie',
        address: {
            country: 'USA'
        }
     }
]

现在,如果我查看生成的SQL,这就是我所拥有的:

SELECT person0_.id                 AS id1_18_, 
       person0_.firstName          AS firstName2_18_, 
       person0_.lastName           AS lastName3_18_, 
       person0_.addressid          AS company4_18_
FROM   person person0_ 

SELECT address0_.id         AS id1_13_0_, 
       address0_.street     AS street2_13_0_, 
       address0_.state      AS state3_13_0_, 
       address0_.country    AS country4_13_0_
FROM   address address0_ 
WHERE  address0_.id = ? 

在这里,“人”和“地址”实体的投影都选择了所有错误的字段,它应该只选择“名字”和“国家”。。

生成的查询应类似于:

SELECT person0_.firstName        AS firstName2_18_
FROM   person person0_ 

SELECT address0_.country    AS country4_13_0_
FROM   address address0_ 
WHERE  address0_.id = ? 

这是正常行为吗,不应该只选择我们需要的字段吗?

谢谢


答案 1

如果要将注释@Query与Spring数据投影一起使用,则必须使用字段别名,并且需要确保为与投影字段匹配的项目添加别名。以下代码应该适用于问题 1:

interface PersonRepository extends CrudRepository<Person, Long> {

  @Query("select p.firstName as firstname, p.address as address from Person p where p.firstName = ?1")
  PersonLimited findPersonByFirstNameProjectedForLimitedData(String firstName);
}

可以使用的另一种方法是使用属性表达式定义查询。只要有可能:

interface PersonRepository extends CrudRepository<Person, Long> {

  List<PersonLimited> findByFirstName(String firstName);
}

答案 2

我遇到了同样的问题,当我将投影从接口更改为pojo类时,它正确地生成了SQL。


推荐