Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!

Vin Smith

Greenhorn
+ Follow
since May 22, 2009
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Vin Smith

Okay, success with the DTO transformation. Here's how you do it:



The key is to use the exact member names i.e. employee, employeeAddress in the select statement (as opposed to arbitrary aliases such as emp, empAddr, etc ...) , as I'm guessing these are used in a reflection type mechanism to determine the appropriate setters

Only 1 select.

Joy!!
Not necessarily, .. here's one way I found to do achieve something close without invoking N+1 queries, using a result transformer (only a single query)



resulting in



I still haven't managed to get the result transformer to a data transfer object to work correctly yet, though.
To answer the 2nd question, yes, . there is a separate SELECT query statement executed for each of the "load" statements". I turned on SQL query logging on the database and observed this in real time.
Consider a simple class heirarchy



This query gets around the N+1 select issue for the Car objects by using LEFT JOIN FETCH


The SQL generated is pretty sane:


However, I want to instantiate a new Data Transfer Object EmployeeData using various data (employee + employee.address) (yes, I know this example is a bit contrived since we can get the address from the employee anyway, .. .but in real life, we might want other data from a JOIN with another table etc etc ... . I use the following HQL



This is what I see in the debug logs.
1. How can I avoid all the extra SELECTs while still being able to instantiate the DTO EmployeeData object? Alternatively, is there another preferred way of obtaining various bits information from (potentially different classes) and returning them in a concise and convenient manner (like my DTO object).
2. Are all the "load" operations actually executing new selects (as per the hibernate session.load() method)?

Thanks in Advance

Assuming my entity class looks something like this snippet:



Assuming I want to create a DAO method getUserCaseDetails(long userId, long caseId), what does the syntax look like? The variables userId and caseId correspond to particular row Ids in the User and Case tables. The criteria I've constructed in the past usually refer to Objects directly but in this case, all I have are the ids .

Thanks