1. 程式人生 > 實用技巧 >[Java Spring] JPA CrudRepository query language

[Java Spring] JPA CrudRepository query language

Entity:

package com.example.ec.domain;

import javax.persistence.*;

@Entity
public class Tour {
    @Id
    @GeneratedValue
    private Integer id;

    @Column
    private String title;

    @Column(length = 2000)
    private String description;

    @Column(length = 2000)
    private String blurb;

    @Column
    private Integer price;

    @Column
    private String duration;

    @Column
    private String keywords;

    @Column(length = 2000)
    private String bullets;

    @ManyToOne
    private TourPackage tourPackage;
@Column @Enumerated private Difficulty difficulty; @Column @Enumerated private Region region; public Tour(String title, String description, String blurb, Integer price, String duration, String bullets, String keywords, TourPackage tourPackage, Difficulty difficulty, Region region) { this.title = title; this.description = description; this.blurb = blurb; this.price = price; this.duration = duration; this.bullets = bullets; this.keywords = keywords; this.tourPackage = tourPackage; this.difficulty = difficulty; this.region = region; } protected Tour() {} public Integer getId() { return id; } public String getKeywords() { return keywords; } public void setKeywords(String keywords) { this.keywords = keywords; } public void setId(Integer id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getBlurb() { return blurb; } public void setBlurb(String blurb) { this.blurb = blurb; } public Integer getPrice() { return price; } public void setPrice(Integer price) { this.price = price; } public String getDuration() { return duration; } public void setDuration(String duration) { this.duration = duration; } public String getBullets() { return bullets; } public void setBullets(String bullets) { this.bullets = bullets; } public TourPackage getTourPackage() { return tourPackage; } public void setTourPackage(TourPackage tourPackage) { this.tourPackage = tourPackage; } public Difficulty getDifficulty() { return difficulty; } public void setDifficulty(Difficulty difficulty) { this.difficulty = difficulty; } public Region getRegion() { return region; } public void setRegion(Region region) { this.region = region; } }

  

package com.example.ec.domain;

import javax.persistence.Column;
import javax.persistence.Id;

public class TourPackage {
    @Id
    private String code;

    @Column
    private String name;

    protected TourPackage() {}

    public TourPackage(String code, String name) {
        this.code = code;
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

In Repository, you can do baisc extension:

public interface TourPackageRepository extends CrudRepository<TourPackage, String> {
    Optional<TourPackage> findByName(String name);
}

You can combine multi Entities:

package com.example.ec.repo;

import com.example.ec.domain.Difficulty;
import com.example.ec.domain.Region;
import com.example.ec.domain.Tour;
import org.springframework.data.repository.CrudRepository;

import java.util.*;


public interface TourRepository extends CrudRepository<Tour, Integer> {
    List<Tour> findTourPackageCodeAndRegion(String code, Region region);

    List<Tour> findByRegionIn(List<Region> regions);

    List<Tour> findByPriceLessThan(Integer maxPrice);

    List<Tour> findByKeywordsContains(String keyword);

    List<Tour> findByTourPackageCodeAndBulletsLike(String code, String searchString);

    List<Tour> findByTourPackageCodeAndDifficultyAndRegionAndPriceLessThan(String code, Difficulty difficulty, Region region, Integer maxPrice);
}

But function name become pretty long and hard to understand, then we can switch to @Query:

package com.example.ec.repo;

import com.example.ec.domain.Difficulty;
import com.example.ec.domain.Region;
import com.example.ec.domain.Tour;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.*;


public interface TourRepository extends CrudRepository<Tour, Integer> {

    @Query("Select t from Tour t where t.tourPackage.code = ?1" +
            " and t.difficult = ?2 and t.region = ?3 and t.price <= ?4")
    List<Tour> lookupTour(String code, Difficulty difficulty, Region region, Integer maxPrice);

    List<Tour> findByTourPackageCodeAndDifficultyAndRegionAndPriceLessThan(String code, Difficulty difficulty, Region region, Integer maxPrice);
}