Spring Data Transformers.aliasToBean(AgentRecordDTO.class)

pra*_*upd 6 hibernate transformer-model hibernate-criteria spring-data

我想在 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;
}
Run Code Online (Sandbox Code Playgroud)

我遵循将所需属性设置为调用的不同DTO AgentRecordDTO并将其返回到Client-side(gwt)的做法.

public class AgentRecordDTO implements IsSerializable {

    private long processedBy;

    private String description;

    private RecordType recordType;

    private AgentRecordStatus status;
}
Run Code Online (Sandbox Code Playgroud)

而不是获取实体的所有属性,我想获取一些属性并将它们设置AgentRecordDTOnew AgentRecordDTO()我可以在hql中执行的属性,但是想要使用Spring Data Specification.

转型

我的AgentRepository

public interface AgentRecordRepository extends CrudRepository<AgentRecord, Long>, JpaSpecificationExecutor<AgentRecord> {

}
Run Code Online (Sandbox Code Playgroud)

我的转换不完整代码看起来像

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;
}
Run Code Online (Sandbox Code Playgroud)

Hibernate 3.2: 2008年6月3日的HQL和SQL变形金刚是精彩的帖子,但是

我无法在Spring Data Specification中赢得它.

Nie*_*sen 2

不是直接回答,而是一种规避。

每当 Spring Data 存储库不符合我的要求时,我就会从实体管理器直接进入 Hibernate。

例如

entityManager.unwrap(Session.class).
    createSQLQuery(SPECIAL_QUERY).
    setTimestamp("requestTime", time).
    setParameter("currency", String.valueOf(currency)).
    setResultTransformer(
        Transformers.aliasToBean(BookingSummary.class)
    ).list();
Run Code Online (Sandbox Code Playgroud)

我有一些摘要,需要左外连接,因此无法将其映射回实体。