【发布时间】:2013-03-13 23:36:23
【问题描述】:
我想将 Hibernate Transformation 与 Spring Data 一起使用。
我有一个实体AgentRecord,其属性为
@Entity
public class AgentRecord extends AbstractEntity<Long> {
@ManyToOne
private User processedBy;
private String description;
private RecordType recordType;
private AgentRecordStatus status;
}
我遵循将所需属性设置为名为 AgentRecordDTO 的不同 DTO 并将其返回给客户端 (gwt) 的做法。
public class AgentRecordDTO implements IsSerializable {
private long processedBy;
private String description;
private RecordType recordType;
private AgentRecordStatus status;
}
我不想获取实体的所有属性,而是想获取一些属性并将它们设置为 AgentRecordDTO,就像我可以在 hql 中做的 new AgentRecordDTO() 一样,但想要使用 Spring 数据规范强>。
我的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;
}
Hibernate 3.2: Transformers for HQL and SQL 2008 年 6 月 3 日发布的帖子很棒,但是
我无法在 Spring Data Specification 中战胜它。
【问题讨论】:
-
我完全同意。不幸的是,Spring 数据似乎只关注 JPA,而不是底层的休眠扩展选项。
标签: hibernate spring-data hibernate-criteria transformer