This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
So. I'm finally starting to get the hang of Hibernate! There's this thing that's tripping me up though.
I have a Coordinator class which has a one-to-many relationship with a Doctors class. (A Coordinator can be assigned to more then one Doctor). I've successfully created a method that will list all the Doctors for a Coordinator:
but... Doing it to opposite way - list the Coordinator for the Doctor - it doesn't seem to work correctly...
...see, when I originall ran the query, I expected to cast the results as a Coordinator, like so:
...and then return the first item (if exists). Instead, it throws me an error when I try to Cast the result, so instead I get the id (PK) of the Coordinator, which is the query you see now - and then pass it to another function that will return the Coordinator based on the id.
Is there a reason why I cannot cast the result? I feel it has something to do with the join, but when I try to do doctor.getCoordinator(), it throws a LazyException error. (Which I thought it would)...
I'll take a best guess at the solution to your problem... To restate your issue: you cannot cast your results into Coordinator objects.
This is happening because you are joining tables explicitly (without fetching). What you get back as your results is a list of Object (List<Object> . The first element in your array is the Coordinator and the second is the Doctor. From what I can tell, this explicit join is unnecessary for two reasons:
1) Since you are looking for the doctor.id in the where clause, the keyword 'left' is unnecessary. You will always only get rows bach that have a matching doctor.
2) Hibernate allows you to use implicity joins that traverse the object graph and allow you to use those values in your where clause. Unlike an explicit join, this will only return to you a List of object in your from clause (List<Doctor> .
For example: Your query will probably give you back what you want if it were coded like this:
David Madouros<br />SCJP 1.4
Joined: Mar 12, 2007
Bam! Thats exactly what I was trying to figure out, and that worked like a charm. Thanks!
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com