I have a table with 11 columns, but I need to get only 2 of them in my application, I’m using spring/hibernate/DAO combination. For now I have a domain class which includes all 11 fields, and mapping file which maps all 11 columns in table. How do I use get just 2 of them not all?
Share
Either:
Use projections – Pro: nothing to add – Con: Not typesafe (the result is a
Listof rows where each row is anObject[]):Use a constructor expression in the SELECT clause (the specified class is not required to be an entity or to be mapped to the database) – Pro: typesafe solution – Con: More classes, unless you reuse
FatEntityas holder in which case many fields will benull:Note that if an entity class name is specified in the
SELECT NEWclause, the resulting entity instances are in the new state (no persistent identity).Use another entity mapped on the same table with only the required fields – Pro: It’s a real entity that you can modify and update – Con: More classes.
The main differences between #2 and #3 are:
2 doesn’t require the holder to be an entity at all.