Spring Data Transformers.aliasToBean (AgentRecordDTO.class)

I want to use Hibernate Transformation with Spring data.

I have an AgentRecord entity with attributes like

 @Entity public class AgentRecord extends AbstractEntity<Long> { @ManyToOne private User processedBy; private String description; private RecordType recordType; private AgentRecordStatus status; } 

I follow the practice of setting the necessary attributes in another DTO called AgentRecordDTO and returning it on the client side ( gwt ).

 public class AgentRecordDTO implements IsSerializable { private long processedBy; private String description; private RecordType recordType; private AgentRecordStatus status; } 

Instead of extracting all the attributes of the object, I want to get several attributes and set them in AgentRecordDTO , as well as in new AgentRecordDTO() , which I can do in hql, but I want to do data specification with Spring .

Transformation

My AgentRepository is

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

The wrong code for my transformation looks like

 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 dated June 03, 2008 were a brilliant message, but

I could not defeat it in the Spring Data Specification.

+6
source share
1 answer

Not a direct answer, but a workaround ..

Whenever Spring's datastore did not meet my requirements, I resorted to directly migrating to Hibernate from entitymanager.

eg.

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

I had several resumes that needed left outer joins, so could not map them back to entities.

+2
source

All Articles