Spring Data でHibernate Transformationを使用したい。
AgentRecord
属性を持つエンティティがあります
@Entity
public class AgentRecord extends AbstractEntity<Long> {
@ManyToOne
private User processedBy;
private String description;
private RecordType recordType;
private AgentRecordStatus status;
}
必要な属性を別の DTO に設定し、AgentRecordDTO
それを Client-side( gwt
) に返すという慣例に従っています。
public class AgentRecordDTO implements IsSerializable {
private long processedBy;
private String description;
private RecordType recordType;
private AgentRecordStatus status;
}
エンティティのすべての属性をフェッチする代わりに、いくつかの属性をフェッチして、hql で実行できるAgentRecordDTO
ように設定したいのですが、 Spring Data Specificationで実行したいと考えています。new AgentRecordDTO()
私のAgentRepository
は
public interface AgentRecordRepository extends CrudRepository<AgentRecord, Long>, JpaSpecificationExecutor<AgentRecord> {
}
私の変換の不完全なコードは次のようになります
public Page<AgentRecordDTO> getAgentRecords(final long userId) {
SimplePageable page = new SimplePageable(1, 10); //my custom object
Page<AgentRecord> pages = agentRecordRepository.findAll(new Specification<AgentRecord>() {
@Override
public Predicate toPredicate(Root<AgentRecord> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
//Projection plus Transformers.aliasToBean(AgentRecordDTO) missing here
Predicate predicate = cb.equal(root.get("processedBy").get("id"), userId);
if (null != predicate) {
predicate = cb.or(predicate, cb.equal(root.get("recordType"), RecordType.VERIFICATION_REQUEST));
predicate = cb.or(predicate, cb.equal(root.get("recordType"), RecordType.VERIFICATION));
}
return predicate;
}
}, new PageRequest(page.getPage(), page.getSize(), new Sort(new Order(Direction.DESC, "id"))));
return null;
}
2008 年 6 月 3 日の Hibernate 3.2: Transformers for HQL and SQLは素晴らしい投稿でしたが、