Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java QueryDSL中的查询谓词_Java_Jpa_Spring Data_Querydsl - Fatal编程技术网

Java QueryDSL中的查询谓词

Java QueryDSL中的查询谓词,java,jpa,spring-data,querydsl,Java,Jpa,Spring Data,Querydsl,环境是Java、Spring boot、Hibernat、QueryDSL、MySQL 我有桌子结构 插曲 +----+-------------+-------- | id | address_id | eventno +----+-------------+-------- | 5 | 27 | F123 | 6 | 30 | F456 | 7 | 45 | F789 +----+-------------+-------- @

环境是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;
插曲\u个人

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

+----+--------------+--------------+------------+-----------+
| 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;
所以每集都有多个人。联接表是一个人

我的UI有一个datatable,每个列上都有一个筛选器:

筛选已对事件和地址起作用。看起来像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);

除非将部分处理委托给数据库,否则这并不容易

如果我们可以在数据库层而不是作为应用程序逻辑中的派生属性填充
case\u name
属性,那么前端代码就变得微不足道了

我们可以通过视图来实现这一点。具体定义取决于您的数据库,但输出如下:

剧情摘要\u大众

+------------+-------------------------+
| epsiode_id | case_name               |
+------------+-------------------------+
|  5         |        Eastwood & Joilee| 
|  6         |           Pitt & Aniston| 
|  7         |           Aniston & Pitt| 
+------------+-------------------------+
对于Oracle来说,
listag
函数是您想要的,而对于MySQL来说,
GROUP\u 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
功能简单地将案例名称映射到事件实体:

@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")));
}

您使用的是哪个数据库?@AlanHay我使用的是MySQL。是否为数据库中的每一集创建了视图表?