Solved

subqueries in hibernate

Posted on 2004-09-28
12
4,355 Views
Last Modified: 2013-11-24
Hi experts,

I have a table with approximately 80 columns, of which about 40 are references to other tables. They are defined as <many-to-one ... /> in hibernate mapping file.

When hibernate generates sql it uses "outer left join" keywords and generates a huge SQL clause. It takes about 20 seconds to execute this query.

I am using Oracle and using subqueries in select statements improves the  performance greatly.

Here is my question:

How can I make Hibernate 2.1 use sub-select statements instead of huge joins?

Eg. hibernate generates this

select person.name, city.name from T_PERSON person, T_CITY city
where person.city=city.code

And I want hibernate to generate this

select
    person.name,
    (select city.name from T_CITY city where city.code = person.city) as city_name
from
   T_PERSON person

thanks
0
Comment
Question by:alikoank
12 Comments
 
LVL 12

Expert Comment

by:Giant2
ID: 12171108
Not all the databases permit the sub-select statement, so I believe hibernate not implement in this manner and use the outer join.
I believe you could increase the performance if you indexes your tables.

Bye, Giant.
0
 
LVL 4

Author Comment

by:alikoank
ID: 12177054
All my necessary tables have indexes.

I do not think hibernate omit sub-selects just because some vendor does not support them, if it is so shame on them. What is the point of using dialects anyway?

0
 
LVL 12

Expert Comment

by:Giant2
ID: 12177106
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12177123
Hibernate 2.1 supports subselect if the underlying database supports it
http://www.hibernate.org/hib_docs/reference/en/html/queryhql.html
0
 
LVL 4

Author Comment

by:alikoank
ID: 12177212
Ok, I think I need to clarify a bit, using subselects in HQL is not a problem, I want to find out if it is possible to modify the way hibernate generates SQL.

Here is the HQL statement I execute

"from com.Person"

in the mapping

<class name="com.Person">
     <many-to-one name="city" ../>
</class>

<class name="com.City">
  ...
</class>

when I issue my HQL hibernate generates something like following,

select person.name, city.name from T_PERSON person, T_CITY city
where person.city=city.code

And I want hibernate to generate this:

select
    person.name,
    (select city.name from T_CITY city where city.code = person.city) as city_name
from
   T_PERSON person

thanks
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12177228
I find this:
15.1.3. Properties
Database properties may be specified as system properties with -D<property>

in hibernate.properties

in a named properties file with --properties

The needed properties are:
Table 15.3. SchemaExport Connection Properties
Property Name                              Description
hibernate.connection.driver_class   jdbc driver class
hibernate.connection.url                 jdbc url
hibernate.connection.username       database user
hibernate.connection.password       user password
hibernate.dialect                            dialect

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 4

Author Comment

by:alikoank
ID: 12177263
I can already connect to database,

I used both OracleDialect and Oracle9Dialect, generated sql synax changes a bit and OracleDialect generates SQL which executes slightly faster but they both do use Outer joins.
0
 
LVL 35

Expert Comment

by:girionis
ID: 12177555
If I remember correctly there is the Criteria class where you can pass several criteria for your query. Maybe it will help.
0
 
LVL 4

Author Comment

by:alikoank
ID: 12320144
Still no answers.
I will ask CS to delete this question.
0
 
LVL 3

Accepted Solution

by:
Gunt earned 500 total points
ID: 12468622
There's no way to do that. Dialects do not command the way an SQL is generated (structured), just hints as the keywords to use, ways to do database specific tasks (Sequence nextval, result limiting, etc), sql functions, and the such.

So, not even writting your own dialect you will be able to accomplish what you want. The only way, is to change the queries builder, which implies modifying Hibernate source code (specially SessionImpl and Loader classes). It would be a really challenging task.

Hibernate will ALWAYS fetch associations using outer joins, or just don't fetch them.
You can instruct Hibernate to not load the associations unless accessed, making them lazy (just like a collection, many-to-one associations can be set to lazy using the lazy="true").
This may improve your query, but if you access this associations latter, you will end up issuing a select for each accessed association (n+1 select problem).

Sorry.
0
 
LVL 4

Author Comment

by:alikoank
ID: 12470430
I wish there was a way to do that, sub queries improve Oracle perfomance greatly.

Thanks anyway.
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12482716
Ok.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now