6

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は素晴らしい投稿でしたが、

Spring Data Specification では勝てませんでした。

4

1 に答える 1

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();

左外部結合が必要ないくつかの要約があったため、それをエンティティにマップし直す方法がありませんでした。

于 2014-06-13T12:11:13.007 に答える