[Java Spring] JPA CrudRepository query language

Entity:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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;
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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);
}
复制代码

 

 

posted @   Zhentiw  阅读(125)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2019-12-12 [Unit test] jasmine createSpyObj
2019-12-12 [Algorithm] 171. Excel Sheet Column Number
2019-12-12 [React] Fix "React Error: Rendered fewer hooks than expected"
2019-12-12 [Git] Remove Files from Staging Before Committing
2019-12-12 [Git] --amend
2019-12-12 [Javascript] Use Optional Chaining to Safely Handle Objects with Unknown Properties
2019-12-12 [Javascript] Use the Nullish Coalescing Operator to Set Object Defaults
点击右上角即可分享
微信分享提示