【发布时间】:2012-03-25 05:41:35
【问题描述】:
我想要一个 Criteria 查询来使用 AliasToBeanResultTransformer 实例化 DTO 类。目标是生成一个带有 ID 的轻量级分页列表,用于主页的进一步操作。这需要报告类型查询。
Criteria crit = session.createCriteria(Profile.class);
crit.createAlias("personalData", "pd");
crit.createAlias("emails", "e");
crit.createAlias("telephones", "t");
ProjectionList properties = Projections.projectionList();
properties.add(Projections.property("id").as( "id"));
properties.add(Projections.property("pd.lastName").as("lastName"));
properties.add(Projections.property("pd.fullName").as("fullName"));
properties.add(Projections.property("e.emailAddress").as("email"));
properties.add(Projections.property("t.phoneNumber").as("phone"));
crit.setProjection(properties);
crit.setResultTransformer(new AliasToBeanResultTransformer(ProfileDTO.class));
profiles = crit.list();
这无法实例化我的 DTO 类。 ProfileDTO 有一个匹配的构造函数:
public ProfileDTO(Long id, String lastName, String fullName, String email,
String phone) {
this(id,fullName);
this.lastName = lastName;
this.email = email;
this.phone = phone;
}
当我使用结果行手动构造 ProfileDTO 对象时,查询有效
List<Object[]> rows = crit.list();
for ( Object[] row: rows ) {
ProfileDTO dto = new ProfileDTO();
dto.setId((Long)row[0]);
dto.setLastName((String)row[1]);
dto.setFullName((String)row[2]);
dto.setEmail((String)row[3]);
dto.setPhone((String)row[4]);
profiles.add(dto);
}
我的解决方法运行良好,但似乎没有必要。我做错了什么?
【问题讨论】:
标签: hibernate