-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: arbitary outer joins in HQL are not possible...
PostPosted: Sun Jul 18, 2004 10:37 pm 
Regular
Regular

Joined: Mon Sep 29, 2003 9:39 am
Posts: 67
from http://forum.hibernate.org/viewtopic.php?t=929934
Quote:
arbitary outer joins in HQL are not possible yet. You can use createSqlQuery to workaround probably.


I. Does createSqlQuery offer functionality that createQuery does not?

This query returns what I need, in sql terms (ids where
objects are needed, values where values are needed):
Code:
SELECT t2.column_c, t1.column_g,
case when t4.column_h is null then 0 else column_h end column_h_alias
FROM table_one t1
INNER JOIN table_two t2 ON t1.column_c = t2.column_c
INNER JOIN table_three t3 ON t3.column_b = t1.column_a
LEFT JOIN table_four t4 ON t4.column_d = t2.column_d AND t4.column_a = t3.column_a
WHERE t1.column_a = ? AND column_e = ? AND t3.column_a=?

II.
1. Can this SQL query be used in createSqlQuery to
return objects and properties(or NULL when there is no t4.column_h) ?

2. If not, how do you correctly
create objects from the ids and values you've returned from SQL?

i.e. I want to create a new object with:
    an association whose id corresponds to t2.column_c,
    a property calculated from t1.column_g within the application,
    and a property t1.column_h?

I could create the database values that would form the new objects
in a stored procedure, and then load them via HQL, but the calculations
are significant business logic, and I don't like the idea of duplicating the
code in the db layer as well as the application layer - seems like asking for trouble.

Is there a strategy for loading from SQL, building objects manually,
and saving the to the session when you can't do it through HQL?

Thank you for your time reading this question.


Top
 Profile  
 
 Post subject: Simplification of question...
PostPosted: Mon Jul 19, 2004 9:16 am 
Regular
Regular

Joined: Mon Sep 29, 2003 9:39 am
Posts: 67
OK, perhaps that is a bit confusing. So I'll rephrase the question:
I'd like to perform a left join where the join is not on an id column.

A simple example where this would be useful is a self-joined table used to
implement a tree:
Code:
PriceLevel Table (cost_level_id is a FK of the price_level_id)
price_level_id | cost_level_id | margin

This is used to represent various points in a supply chain, where one level's price is another level's cost, and every price has a cost. (or put differently, every item that will be sold must be bought first). Now consider a separate table used to represent prices (read-only):
Code:
Current Price Table
current_price_id | item_id| price_level_id| current_price

And one that represents potental prices (r/w):
Code:
Proposed Price Table
proposed_price_id | item_id | price_level_id | new_price | current_price


Finally, you'd like to create a set of proposed prices for given items at a given price_level, but the catch is, you'd like to populate the current price where it exists, and 0 or null for current price where it does not. This implies that you need proposed costs for the items as well as current prices.

This is straightforward using SQL, but I'm not sure how to do it in HQL, or if it is even possible (the previous linked thread says it is not).

So is there a way to create these objects from SQL, perform some business logic on them, and store them back through
Code:
session.save()
?
Or is it possible to use createSQLQuery to load the objects themselves?
(Item, PriceLevel, ProposedCost, CurrentPrice)?

Thanks for your help.


Top
 Profile  
 
 Post subject: Temporary Fix
PostPosted: Mon Jul 19, 2004 10:32 am 
Regular
Regular

Joined: Mon Sep 29, 2003 9:39 am
Posts: 67
Ok, so I've implemented this as a hybrid solution. The SQL is used to return a JDBC resultset in the DAO, and a callback to the business object is used to create the object given the raw values. The new objects are saved in the session using save.

This provides a hook for calculating the new values without loading unnecessary objects or executing multiple queries per new object. The drawback is that this leads to tight coupling - however, the parameter list
of the callback forms a contract, I suppose you know what you can do
with the lightweight versions of the object because you create them yourself.
i.e.
Code:
Y createY(long xId,  double a) {
X x = new X(xId);
Y y = new Y(x, a);
y.getA(); //OK, because it's there...
y.getX().getB();  //Can't do this unless you load xId.
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.