Java 针对具有类层次结构的实体的JPA和QueryDSL投影
我有一组属于特定类层次结构的实体(该层次结构中的每个类都映射回数据库中的同一个表)。我正在尝试使用QueryDSL投影类层次结构表示的数据。投影需要从每个具体类返回字段,但我不确定如何正确地返回,或者是否可能 我有一些东西可以工作,但具体类中存在的字段没有正确投影,返回的结果是重复的,并且生成的查询对同一个表多次连接 我拥有的一个例子是(我排除了getter/setter/constructor和berevety的大多数hibernate注释) 当我让它返回我想要的结果和一个普通的查询时,我正在尝试做的是可能的吗Java 针对具有类层次结构的实体的JPA和QueryDSL投影,java,hibernate,jpa,querydsl,Java,Hibernate,Jpa,Querydsl,我有一组属于特定类层次结构的实体(该层次结构中的每个类都映射回数据库中的同一个表)。我正在尝试使用QueryDSL投影类层次结构表示的数据。投影需要从每个具体类返回字段,但我不确定如何正确地返回,或者是否可能 我有一些东西可以工作,但具体类中存在的字段没有正确投影,返回的结果是重复的,并且生成的查询对同一个表多次连接 我拥有的一个例子是(我排除了getter/setter/constructor和berevety的大多数hibernate注释) 当我让它返回我想要的结果和一个普通的查询时,我正在
public abstract class AbstractLocation {
@Id
private int id;
}
public class Municipality extends AbstractLocation {
private String municipality;
}
public class Address extends AbstractLocation {
private String buildingAddress;
}
public class LocationDTO {
private int id;
private String municipality;
private String buildingAddress;
}
public void getLocationDTO() {
final QAbstractLocation location = new QAbstractLocation("abstractCase");
final QMunicipality municipality = new QMunicipality("sampleUnitBasedCase");
final QAddress address = new QAddress("listingCase");
final JPAQuery query = new JPAQuery(this.getEntityManager());
return query.from(location, municipality, address)
.list(new QLocationDTO(
location.id,
municipality.municipality,
address.buildingAddress));
}