I'm trying to figure out how Hibernate handles the following situation:
Say I have entities A, and B properly mapped in an hbm file. If I write an HQL query the selects from both of them (e.g. from A, B 开发者_运维问答where ...
) what is returned? I assume it cannot be cast to either the object representing A, or B since it is some combination of these two.
In fact what you're trying to do is called a projection. Hibernate all by itself will return a List (untyped) of all the beans that are returned by your query. If you;re using the JPA implementation, you can actually decalre a Class which would contain the aggregated values of the results you're getting back, something like this:
@SqlResultSetMapping(
name="DepartmentSummary", entities={ @EntityResult(entityClass=Department.class, fields=@FieldResult(name="name", column="DEPT_NAME")), @EntityResult(entityClass=Employee.class) }, columns={@ColumnResult(name="TOT_EMP"), @ColumnResult(name="AVG_SAL")} )
精彩评论