简体   繁体   中英

Stuck on how to find QueryDSL predicate in spring data app

I have a problem in querying some data, I will explain my problem briefly and set an example for clarification

I use Spring data, QueryDSL and MySQL.

I want to query like this in queryDSL method. A specific example is just a simple example to give an idea.

Say for example, there are two tables Employee and Certificate. Relation between two is ONE (Employee) to MANY (Certificate)

Following are tables

Employee (id, first_name, last_name);
Certificate (id, name, date, fk_emp);

Following are the entities,

@Entity
@Table(name = "EMPLOYEE")
class Employee {
  @Id
  @Column(name = "ID")
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Integer id;

  @Column(name = "FIRST_NAME")
  private String firstName;

  @Column(name = "LAST_NAME")
  private String lastName;

  @OneToMany(mappedBy = "employee", cascade = CascadeType.ALL)
  private List<Certificate> certificates = new ArrayList<>();
}

@Entity
@Table(name = "CERTIFICATE")
class Certificate {
  @Id
  @Column(name = "ID")
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Integer id;

  @Column(name = "CERTIFICATE_NAME")
  private String certificateName;

  @Column(name = "DATE")
  private Date date;

  @ManyToOne
  @JoinColumn(name = "REF_EMPLOYEE")
  private Employee employee;
}

What should the QueryDSL predicate be for Returning all employees with name contains (in first_name and last_name) and from that result whose certification between date 22-12-2014 to 22-12-2015

At the same time, I want to obtain all employees who do not have any certificates

example of results:

+----+--------------------+---------------------+--------+
| id | name               | date                | fk_emp |
+----+--------------------+---------------------+--------+
|  1 | FirstName LastName | 2014-02-01 00:00:00 |  11111 |
+----+--------------------+---------------------+--------+
|  2 | FirstName LastName | 2014-03-01 00:00:00 |  null  |
+----+--------------------+---------------------+--------+
|  3 | FirstName LastName | 2014-04-01 00:00:00 |        |
+----+--------------------+---------------------+--------+
|  5 | FirstName LastName | 2014-05-01 00:00:00 |        |
+----+--------------------+---------------------+--------+
| 12 | FirstName LastName | 2014-06-01 00:00:00 |  25555 |
+----+--------------------+---------------------+--------+
| 13 | FirstName LastName | 2014-07-01 00:00:00 |  78888 |
+----+--------------------+---------------------+--------+
| 16 | FirstName LastName | 2014-08-01 00:00:00 |  null  |
+----+--------------------+---------------------+--------+

Thank you

Native JPQL Query:

SELECT * FROM EMPLOYEE A
LEFT JOIN CERTIFICATE B ON A.ID = B.REF_EMPLOYEE AND DATE BETWEEN CAST('2014-12-22' AS DATE) AND CAST('2015-12-22' AS DATE);

Querydsl join variant:

query.from(employee)
    .leftJoin(employee.certificates, certificate)
    .on(certificate.date.between('from_date', 'to_date'))
    .list(employee);

Note: Date format can be different here and please change according to your need

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM