У меня есть структура базы данных с тремя таблицами объектов и двумя таблицами «многие ко многим соединениям» для их соединения В результате получается трехуровневая объектная модель.
компания <-> сотрудник <-> электронная почта
Когда я выполняю запрос к базе данных следующим образом:
List <CompanyDTO> companies = new JPAQueryFactory(entityManager)
.from(COMPANY)
.leftJoin(COMPANY.employee, COMPANY_EMPLOYEE)
.leftJoin(COMPANY_EMPLOYEE.email, COMPANY_EMPLOYEE_EMAIL)
.transform(groupBy(COMPANY.id).list(
new QCompanyDTO(
COMPANY.id,
set(new QEmployeeDTO(
COMPANY_EMPLOYEE.id,
set(new QEmailDTO(
COMPANY_EMPLOYEE_EMAIL.id,
COMPANY_EMPLOYEE_EMAIL.email,
COMPANY_EMPLOYEE_EMAIL.type
))
))
)));
В Querydsl есть проблема с инициализацией объекта.
Проблема возникает при инициализации объекта EmailDTO из-за неправильного использования конструктора.
Spring boot version: v2.1.5.RELEASE
Версия Querydsl: 4.2.1
Есть ли способ заставить это работать?
Caused by: java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_192]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_192]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_192]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_192]
at com.querydsl.core.types.ConstructorExpression.newInstance(ConstructorExpression.java:141) ~[querydsl-core-4.2.1.jar:na]
at com.querydsl.core.types.FactoryExpressionUtils.compress(FactoryExpressionUtils.java:155) ~[querydsl-core-4.2.1.jar:na]
at com.querydsl.core.types.FactoryExpressionUtils.access$100(FactoryExpressionUtils.java:28) ~[querydsl-core-4.2.1.jar:na]
at com.querydsl.core.types.FactoryExpressionUtils$FactoryExpressionAdapter.newInstance(FactoryExpressionUtils.java:62) ~[querydsl-core-4.2.1.jar:na]
at com.querydsl.core.types.FactoryExpressionUtils$FactoryExpressionAdapter.newInstance(FactoryExpressionUtils.java:62) ~[querydsl-core-4.2.1.jar:na]
at com.querydsl.jpa.TransformingIterator.next(TransformingIterator.java:63) ~[querydsl-jpa-4.2.1.jar:na]
at com.querydsl.core.group.GroupByList.transform(GroupByList.java:60) ~[querydsl-core-4.2.1.jar:na]
at com.querydsl.core.group.GroupByList.transform(GroupByList.java:36) ~[querydsl-core-4.2.1.jar:na]
at com.querydsl.core.support.FetchableQueryBase.transform(FetchableQueryBase.java:55) ~[querydsl-core-4.2.1.jar:na]
import com.querydsl.core.annotations.QueryProjection;
public class EmailDTO {
private Long id;
private String mail;
private String type;
@QueryProjection
public EmailDTO (Long id, String mail, String type) {
this.id = id;
this.mail = mail;
this.type = type;
}
public Long getId () {
return id;
}
public void setId (Long id) {
this.id = id;
}
public String getMail () {
return mail;
}
public void setMail (String mail) {
this.mail = mail;
}
public String getType () {
return type;
}
public void setType (String type) {
this.type = type;
}
@Override
public String toString () {
return "{\"email\":{"
+ "\"id\":\"" + id + "\""
+ ", \"mail\":\"" + mail + "\""
+ ", \"type\":\"" + type + "\""
+ "}}";
}
}
import com.querydsl.core.annotations.QueryProjection;
import java.util.List;
public class EmployeeDTO {
private Long id;
private List <EmailDTO> emails;
@QueryProjection
public EmployeeDTO (Long id, List <EmailDTO> emails) {
this.id = id;
this.emails = emails;
}
public Long getId () {
return id;
}
public void setId (Long id) {
this.id = id;
}
public List <EmailDTO> getEmails () {
return emails;
}
public void setEmails (List <EmailDTO> emails) {
this.emails = emails;
}
@Override
public String toString () {
return "{\"employee\":{"
+ "\"emails\":" + emails
+ ", \"id\":\"" + id + "\""
+ "}}";
}
}
import com.querydsl.core.annotations.QueryProjection;
import java.util.List;
public class CompanyDTO {
private Long id;
private List <EmployeeDTO> employees;
@QueryProjection
public CompanyDTO (Long id, List <EmployeeDTO> employees) {
this.id = id;
this.employees = employees;
}
public Long getId () {
return id;
}
public void setId (Long id) {
this.id = id;
}
public List <EmployeeDTO> getPipes () {
return employees;
}
public void setPipes (List <EmployeeDTO> employees) {
this.employees = employees;
}
@Override
public String toString () {
return "{\"Company\":{"
+ "\"id\":\"" + id + "\""
+ ", \"employees\":" + employees
+ "}}";
}
}