• [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);
    }

     

  • 相关阅读:
    js 获取和设置css3 属性值的实现方法
    API的自动化测试
    删除html标签或标签属性以及样式
    JS+CSS实现数字滚动
    video元素和audio元素相关事件
    SDT v0.0.1 上线
    safari浏览器fixed后,被软键盘遮盖的问题—【未解决】
    js中DOM事件探究
    使用Web存储API存取本地数据
    剑指offer(Java版)第七题:用两个栈实现一个队列。队列的声明如下,请实现它的两个函数appendTail和deleteHead, 分别完成在队列尾部插入结点和在队列头部删除结点的功能。
  • 原文地址:https://www.cnblogs.com/Answer1215/p/14127054.html
Copyright © 2020-2023  润新知