We're attempting to select a DB peristence tool, so naturally my research has lead me here. I've never actually used Hibernate past playing around with the Caveat Emptor example, so please if I'm missing something, be sure to point it out.
The real issue we have is that we want both full and partial beaned results returned. I've noticed that when querying with HQL, if you choose to have SELECT return only a few columns, they come back as an array of Longs and Strings, for instance:
Code:
Query queryAuditOne = HibernateUtil.getSession().createQuery("select lr.message, lr.entityId, lr.userId from AuditLogRecord lr where lr.entityId = :id");
I found that you can force the result into a Java bean constructor, like such:
Code:
Query queryAuditOne = HibernateUtil.getSession().createQuery("select new AuditLogRecord(lr.message,lr.entityId,lr.entityClass,lr.userId) from AuditLogRecord lr where lr.entityId = :id");
But this is a clunky method to allow for any combination of selected columns, and inevitably can have two combinations that would match the same constructor imprint.
Here's the question: I've noticed that for a Criteria based query, you specify the class of the return table beaned object. For a query, I do not see such a format. Is there a way to FORCE a unique or list returned query to a specific type, and to set the properties of a bean when only partial results are returned?
For those who help, thank you! Can you please refrain from answering the question with the two following answers:
1) Redesign your tables so that this is not necessary and only full results are returned always.
2) On return of the List, use reflection to call beaned set methods of the object list. We're trying to avoid this.
Thanks all for the insight.
-ian