我有一个数据库结构,其中包含三个对象表和两个“多对多连接”表来连接它们。结果是一个三级对象模型。
公司<->员工<->电子邮件
当我像这样对数据库执行查询时:
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对象时会发生问题。
春季启动版本: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
+ "}}";
}
}