spring data jpa查询一个实体类的部分属性

      使用Spring Data Repository查询时候,通常情况下返回的是一个实体所有的属性。但是在很多情况下,我们只需要实体类的部分属性。下面的部分讲述如何实现查询一个实体类的部分属性。

      首先我们定义两个实体类

    

package cn.net.leadu.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;

/**
 * Created by PengChao on 2016/12/1.
 */
@Entity
public class Person {
    @Id
    @GeneratedValue
    private Long id;
    private String firstName;
    private String lastName;
    @OneToOne
    private Address address;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }
}

 

package cn.net.leadu.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

/**
 * Created by PengChao on 2016/12/1.
 */
@Entity
public class Address {
    @Id
    @GeneratedValue
    private Long id;
    private String street;
    private String state;
    private String country;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }
}

 

     然后创建person实体类的repository

 

package cn.net.leadu.dao;

import cn.net.leadu.domain.Person;
import org.springframework.data.jpa.repository.JpaRepository;

/**
 * Created by PengChao on 2016/12/1.
 */
public interface PersonRepository extends JpaRepository {
    Person findByFirstName(String firstName); // 1
}

    "1"方法将会返回Person对象的所有属性(包括address)


spring data jpa查询一个实体类的部分属性_第1张图片
 

但实际情况我们不需要返回address对象,这种情况我们可以定义一个projection,如下:

 

 

package cn.net.leadu.domain;
 
/**
 * Created by PengChao on 2016/12/1.
 */
public interface NoAddress {
    String getFirstName(); //2
    String getLasetName(); //3
 
}

    “2”输出person对象的firstName属性,“3”输出person对象的lastName属性

 

最后在Person实体类的repository中定义一个查询方法,返回值是NoAddress,如下

package cn.net.leadu.dao;

import cn.net.leadu.domain.NoAddress;
import cn.net.leadu.domain.Person;
import org.springframework.data.jpa.repository.JpaRepository;

/**
 * Created by PengChao on 2016/12/1.
 */
public interface PersonRepository extends JpaRepository {
    NoAddress findByFirstName(String firstName);
}

返回结果只包含firstName和lastName两个属性


spring data jpa查询一个实体类的部分属性_第2张图片
 

 

 

 

你可能感兴趣的:(spring,data,jpa)