Hibernate: Can I return a non-mapped list of objects that contains a List?

270 Views Asked by At

I currently use .addScalar and ResultTransformer to return a non-mapped list of objects that do not have any List variables. I'd like to modify my object to contain a List.

Following an initial example provide in Hibernate documentation, I successfully accomplished something similar to:

sess.createSQLQuery("SELECT ID as id, NM_CDE as nameCode, NM_VALUE as nameValue FROM EMP")
 .addScalar("id", Hibernate.LONG)
 .addScalar("nameCode", Hibernate.STRING)
 .addScalar("nameValue", Hibernate.STRING)
 .setResultTransformer(Transformers.aliasToBean(Employee.class))

Suppose the data was:

  ID    NM_CDE          NM_VALUE
  1     LEGAL           John Allan Doe
  1     NICKNM          Johnny
  1     FORMATTED       Doe, John

Instead of 3 objects being returned, I'd like a single object returned, assuming the above hypothetical data. The hypothetical Employee.class would be updated to contain a List of Name objects.

This:

private Long id;
private String nameCode;
private String nameValue;

Would be replaced with:

private Long id;
private List<EmpName> empNameList;

But how does the native SQL and/or Hibernate statement need to get updated? Is this even possible?

0

There are 0 best solutions below