spring data jpa query some properties of an entity class

      When querying using Spring Data Repository, it usually returns all attributes of an entity. But in many cases, we only need part of the properties of the entity class. The following sections describe how to implement querying some properties of an entity class.

      First we define two entity classes

    

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;
    }
}

 

     Then create the repository of the person entity class

 

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, Long> {
    Person findByFirstName(String firstName); // 1
}

    The "1" method will return all properties of the Person object (including address)



 

But in practice, we don't need to return the address object. In this case, we can define a projection as follows:

 

 

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

    "2" outputs the firstName property of the person object, "3" outputs the lastName property of the person object

 

Finally, define a query method in the repository of the Person entity class, and the return value is NoAddress, as follows

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<Person, Long> {
    NoAddress findByFirstName(String firstName);
}

The returned result contains only two properties, firstName and lastName



 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326606017&siteId=291194637