Spring Data JPA Projection从数据库

时间:2016-10-22 16:48:21

标签: java spring-data spring-data-jpa

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

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

首先让我假设我有这两个实体:

@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”,它应该只选择“country”。

生成的查询应类似于:

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 = ? 

这里,Person和Address实体的投影选择了所有错误的字段,它应该只选择“firstName”和“country”。

生成的查询应类似于:

SELECT person0_.firstName        AS firstName2_18_
FROM   person person0_ 

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

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

谢谢,

2 个答案:

答案 0 :(得分:14)

如果要在Spring Data Projections中使用注释 @Query ,则必须使用字段别名,并且需要确保对与投影字段匹配的项目进行别名。以下代码适用于问题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);
}

您可以使用的另一种方法是使用Property Expressions定义查询。只要有可能:

interface PersonRepository extends CrudRepository<Person, Long> {

  List<PersonLimited> findByFirstName(String firstName);
}

答案 1 :(得分:0)

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