Hibernate Spring数据转换器.aliasToBean(AgentRecordDTO.class)
我想使用Spring数据。 我有一个实体Hibernate Spring数据转换器.aliasToBean(AgentRecordDTO.class),hibernate,spring-data,hibernate-criteria,transformer,Hibernate,Spring Data,Hibernate Criteria,Transformer,我想使用Spring数据。 我有一个实体AgentRecord,属性如下 @Entity public class AgentRecord extends AbstractEntity<Long> { @ManyToOne private User processedBy; private String description; private RecordType recordType; priv
AgentRecord
,属性如下
@Entity
public class AgentRecord extends AbstractEntity<Long> {
@ManyToOne
private User processedBy;
private String description;
private RecordType recordType;
private AgentRecordStatus status;
}
与获取一个实体的所有属性不同,我想获取几个属性并将它们设置为AgentRecordDTO
,就像new AgentRecordDTO()
一样,我可以在hql中执行,但要使用Spring数据规范
我的AgentRepository
是
public interface AgentRecordRepository extends CrudRepository<AgentRecord, Long>, JpaSpecificationExecutor<AgentRecord> {
}
2008年6月3日发布的公共接口AgentRecordRepository extends Crudepository是一篇精彩的文章,但我无法在Spring数据规范中战胜它。这不是一个直接的答案,而是一种规避
每当Spring数据存储库与我的需求不匹配时,我就会直接从entitymanager进入Hibernate
e、 g
我有一些总结,需要左外连接,因此无法将其映射回实体。我非常同意。不幸的是,spring数据似乎只关注JPA,而不是底层的hibernate扩展选项。这些天我不再做Spring数据
等了。不管怎样,这似乎是个好把戏。Thx Niels.@Niels-bech-nielsen SPECIAL_查询它的意思是什么?该查询只返回(在本例中)BookingSummary类所需的DTO列。
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;
}
entityManager.unwrap(Session.class).
createSQLQuery(SPECIAL_QUERY).
setTimestamp("requestTime", time).
setParameter("currency", String.valueOf(currency)).
setResultTransformer(
Transformers.aliasToBean(BookingSummary.class)
).list();