views:

512

answers:

1

We have an object, A, which contains another object, B. We have Hibernate calling a stored procedure to query the data and populate instances of A. We're using the @NamedNativeQuery annotation with teh resultClass property set to A.class. This works great except that the instances of B are loaded lazily, as if Hibernate can't figure out how to create them based on the metadata returned. We've renamed the 'AS' clauses in the stored procedure to reflect the nesting of B within A and to point directly to the field names of B with no success.

Question is: how do we get eager loading of object fields when a stored procedure is used without resorting to *.hbm.xml or huge @SqlResultSetMapping annotations?

+2  A: 

You can't. From the Hibernate documentation:

Stored procedures currently only return scalars and entities. <return-join> and <load-collection> are not supported.

<return-join> is what you would have used to map eagerly loaded association in (non-SP) named SQL query.

ChssPly76
Can it be done using the @SqlResultSetMapping annotation or are we stuck with lazy-loading all of the child objects?
Mike Reedell
You can use `@SqlResultSetMapping` to return both main and associated entities as query results; you will not be able to use it for custom entity loading (unless you forgo the SP)
ChssPly76
Thanks for your help. Looks like the best way forward is to return both objects side-by-side and add the structure in the service layer.
Mike Reedell