Tôi muốn truy vấn Tiêu chí khởi tạo lớp DTO bằng cách sử dụng AliasToBeanResultTransformer. Mục tiêu là tạo danh sách phân trang trọng lượng nhẹ với ID để có thêm hành động cho trang chủ. Thao tác này yêu cầu truy vấn loại báo cáo.AliasToBeanResultTransformer (MyDTO.class) không thể khởi tạo MyDTO
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();
Điều này không thể khởi tạo lớp DTO của tôi. ProfileDTO có một constructor phù hợp:
public ProfileDTO(Long id, String lastName, String fullName, String email,
String phone) {
this(id,fullName);
this.lastName = lastName;
this.email = email;
this.phone = phone;
}
Và truy vấn hoạt động khi tôi xây dựng ProfileDTO đối tượng bằng tay với các hàng kết quả
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);
}
workaround của tôi là làm việc tốt, nhưng có vẻ như không cần thiết. Tôi đang làm gì sai?
Cảm ơn JB. Tôi sẽ ngừng lo lắng và ôm lấy DIY. Như bạn nói, nó hoạt động tốt như vậy. – carbontax
bạn có thể đặt một ví dụ không? –