I have a really ugly legacy database system that I need to integrate with. Essentially I’m doing some read only reporting on the system, and I don’t want to set up a thousand entities representing each of the tables that I’m working on. Instead, I’d like to just define an Entity for each of the report-types that I generate (essentially a union of a bunch of columns from different tables), and then let hibernate map from the nasty (many joined, many unioned) sql query to a list of such entities.
The question is: can I create an entity that doesn’t have an underlying table, and use a sql statement to populate a list of said entities?
Thanks!
We do that sort of thing all the time – and here is how we do it:
Define a simple bean-like object to represent each row of output in your report:
Define a hibernate mapping file, CityStateRevneueReport.hbm.xml:
Then run the query and populate instances: