1

Consider the following query:

"SELECT a.code as code,{locFrom.*} " +
"FROM audit.auditlogrecord a " +
"LEFT OUTER JOIN iwrs.location locFrom on locFrom.id=old_value "

I want to map the result to this class:

public class MovementHistoryImpl implements MovementHistory {

private String code;
private String user;
private Date date;
private Location locFrom;

I have no problems getting a List with only scalar properties:

        List list = currentSession().createSQLQuery(query)
            .addScalar("code")
            .setResultTransformer(Transformers.aliasToBean(MovementHistoryImpl.class))
            .list();

However, when I want to inject into the locFrom property of MovementHistoryImpl, adding

.addEntity("locFrom",LocationImpl.class)  

does not work: the resulting MovementHistoryImpl item has null value for locFrom.

I have inspected without the resultTransformer (ie, returning a List) and I do get a valid (and expected) LocationImpl object. My questions are:

1) How can one populate the MovementHistoryImpl correctly? 2) Does it work if your MovementHistoryImpl property is named differently (initially I had Location from, instead of locFrom 3) Is it possible to fetch only one property of Location - in reality I'm not interested in locFrom.*, but only locFrom.name, so I wouldn't mind my locFrom object in MovementHistoryImpl to only have the "name" property populated.

mmalmeida
  • 1,037
  • 9
  • 27
  • Is the an entity mapped to auditlogrecord? If so you can use the select new syntax. – James DW Oct 14 '11 at 11:24
  • @JamesDW AuditLogRecord has an Entity, but MovementHistoryImpl is not an hibernate Entity (just a DTO at this point in time). Not sure I understood your comment, would you care to elaborate please? – mmalmeida Oct 17 '11 at 09:18
  • @wild_oscar As I understand you want to populate **MovementHistoryImpl** bean. Do you want to use any other methods apart from the **ResultTransformers**? – ManuPK Oct 30 '11 at 05:55

0 Answers0