Hello,
I'd like to have an entity to be transformed into a DTO
AND some of its related associated entities (collections) also transformed into their own DTO, using Hibernate "Transformers.aliasToBean".
I know how to transform a simple entity to a DTO using projections to map entity fields to DTO fields, but I would like to transform on the fly associated entities too.
Here is my main entity :
Code:
Panel{
Long panelId;
String panelName;
Long panelSize;
Collection<Experiment> experiments;
Collection<Lot> lots;
...
}
Here are the associated entities I'd like to transform to DTO on the fly:
Code:
Experiment{
Long experimentId;
String experimentName;
...
}
Code:
Lot{
Long lotId;
String description ;
String lotNumber;
...
}
Here are DTOs I'd like to obtain:
Code:
PanelDTO{
Long panelId ;
String panelName;
Long panelSize ;
Collection<ExperimentDTO> experiments;
Collection<LotDTO> lots ;
...
}
Using following DTO to transform on the fly in PanelDao class:
Code:
ExperimentDTO{
Long experimentId ;
String experimentName;
...
}
Code:
LotDTO{
Long lotId;
String description ;
String lotNumber;
...
}
Would you mind hint me to know what to update in following code to transform on the fly experiments and lots collection to collections of DTO?
Code:
protected findPanel() throws Exception{
Criteria crit = getSession().createCriteria(Panel.class, "p");
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.distinct(Projections.id()));
projectionList.add(Projections.property("p.panelName"), "panelName");
projectionList.add(Projections.property("p.panelSize"), "size");
projectionList.add(Projections.property("p.experiments"), "experiments");
projectionList.add(Projections.property("p.lots"), "lots");
crit.setProjection(projectionList);
crit.setResultTransformer(Transformers.aliasToBean(PanelVO.class));
return crit.list()
}
Thanks a lot for your help.