簡體   English   中英

QueryDSL中的查詢謂詞

[英]Query Predicate in QueryDSL

環境是Java,Spring-boot,Hibernat,QueryDSL,MySQL。

我有表結構

插曲

+----+-------------+--------
| id | address_id  | eventno
+----+-------------+--------
|  5 |         27  | F123
|  6 |         30  | F456
|  7 |         45  | F789
+----+-------------+--------

@Entity
public class Episode {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotEmpty
private String eventno;
@ManyToOne(cascade = CascadeType.ALL)
private Address address;

Episode_Person

+----+--------------+--------------+------------+-----------+
| id | episode_role | primary_flag | episode_id | person_id |
+----+--------------+--------------+------------+-----------+
| 19 | Buyer        |              |          5 |         1 |
| 20 | Subject      |              |          5 |         2 |
| 23 | Witness      |              |          6 |         3 |
| 24 | Child        |              |          6 |         4 |
| 27 | Buyer        |              |          5 |         3 |
| 63 | Investor     |              |          5 |         4 |
| 64 | Subject      |              |          7 |         1 |
| 65 | Subject      |              |          7 |         3 |

@Entity
public class EpisodePerson {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne
@Valid
private Person person;

@ManyToOne
private Episode episode;

+----+-----------+----------+
| id | firstname | surname  |
+----+-----------+----------+
|  1 | Clint     | eastwood |
|  2 | Angelina  | joilee   |
|  3 | Brad      | pitt     |
|  4 | Jennifer  | aniston  |

@Entity
@Table(uniqueConstraints = @UniqueConstraint(columnNames = {"nia"}))
public class Person {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String surname;
private String firstname;
private String gender;

因此,每一集都有多個人。 聯接表是Episode_Person。

我的UI有一個數據表,每列上都有一個過濾器:

在此處輸入圖片說明

過濾已在事件和地址上起作用。 看起來像QueryDSL中的謂詞:

            BooleanBuilder where = new BooleanBuilder();
        if (pagination.getFilterBy().getMapOfFilters().get("eventno")!=null) {
            where.and(qEpisode.eventno.containsIgnoreCase(pagination.getFilterBy().getMapOfFilters().get("eventno")));
        }
        if (pagination.getFilterBy().getMapOfFilters().get("address")!=null) {
            where.and(qEpisode.address.formattedAddress.containsIgnoreCase(pagination.getFilterBy().getMapOfFilters().get("address")));
        }
        where.and(qEpisode.creatingUser.eq(user));
        List<Episode> e = episodeRepository.findAll(where);

現在,我要如何為案例名稱添加第三個謂詞,其中案例名稱是根據某集中返回的人員集合中返回的前兩個人構造的?

UPDATE

為了澄清起見,支持UI視圖的DTO包含“ casename”屬性。 域對象轉換為DTO時,將在服務層中創建它:

episodeDashboard.setNames(episodePersonList.get(0).getPerson().getSurname().toUpperCase() +" & " +episodePersonList.get(1).getPerson().getSurname().toUpperCase());

除非您將某些處理委托給數據庫,否則不容易。

如果我們可以將case_name屬性填充到數據庫層,而不是作為應用程序邏輯中的派生屬性填充,那么前端代碼就變得微不足道了。

我們可以通過視圖來做到這一點。 確切的定義將取決於您的數據庫,但是輸出將如下所示:

episode_summary_vw

+------------+-------------------------+
| epsiode_id | case_name               |
+------------+-------------------------+
|  5         |        Eastwood & Joilee| 
|  6         |           Pitt & Aniston| 
|  7         |           Aniston & Pitt| 
+------------+-------------------------+

對於Oracle來說, LISTAGG函數似乎是您想要的,而對於MySQL, GROUP_CONCAT函數。 然后在MySQL中,我認為這看起來像:

CREATE VIEW episode_summary_vw as
SELECT ep.episode_id, GROUP_CONCAT(p.surname SEPARATOR ' & ')
FROM episode_person ep
INNER JOIN person p on p.id = ep.person_id
GROUP BY ep.episode_id;
-- todo: needs limit to first 2 records

一旦有了視圖,我們就可以使用JPA的@SecondaryTable功能將case_name映射到Episode實體:

@Entity
@Table(name = "episodes")
@SecondaryTable(name = "episode_summary_vw", primaryKeyJoinColumna = @PrimaryKeyJoinColumn(name="episode_id", reference_column_name="id"))
public class Episode {

    @Column(name ="case_name", table = "episode_summary_vw")
    private String caseName;
}

然后,您可以像對其他任何字段一樣對屬性進行過濾和排序:

if (pagination.getFilterBy().getMapOfFilters().get("caseName")!=null) {

    where.and(qEpisode.caseName.containsIgnoreCase(pagination.getFilterBy().
       getMapOfFilters().get("caseName")));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM