我想将Hibernate 转换 与 Spring Data 一起使用。
我有一个AgentRecord
属性为的实体
@Entity
public class AgentRecord extends AbstractEntity<Long> {
@ManyToOne
private User processedBy;
private String description;
private RecordType recordType;
private AgentRecordStatus status;
}
我遵循将所需属性设置为调用的不同 DTOAgentRecordDTO
并将其返回给 Client-side( gwt
) 的做法。
public class AgentRecordDTO implements IsSerializable {
private long processedBy;
private String description;
private RecordType recordType;
private AgentRecordStatus status;
}
我不想获取实体的所有属性,而是想获取一些属性并将它们设置AgentRecordDTO
为new AgentRecordDTO()
我可以在 hql 中做的那样,但想用Spring Data Specification做。
我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 日是一篇精彩的文章,但是