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

牧云@^-^@ 提交于 2019-12-13 18:12:34

问题


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?

来源:https://stackoverflow.com/questions/13656054/hibernate-can-i-return-a-non-mapped-list-of-objects-that-contains-a-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!