1

We are using dynamic HQL query generator which generates query like below query:

select sum(en1.amount) from SampleEntity1 as en1 where en1.child.name = ? 

Hibernate will convert above query to sql as follows:

select sum(tbl1.c_amount) from sampletable1 as tbl1, samplechild as child1 where tbl1.c_child = child1.c_id and child1.c_name = ? 

Tables sampletable1 and samplechild both have more than 1 million records and because the generated sql has cross join, the performance of the query is very low and it suffers lots of database I/O. But if we run the sql using inner join the query will be run in much better performance.

Is there any way to force hibernate to convert such HQL queries to inner join instead of cross join?

1
  • would you like to give Batoo JPA a chance. Batoo JPA is the new JPA implementation with +15x faster performance. batoo.jp Commented Oct 6, 2012 at 12:32

1 Answer 1

1

This is an inner join, but it's just not using the inner join syntax. If you want it with an inner join syntax, use an inner join, as explained in the Hibernate reference manual:

select sum(en1.amount) from SampleEntity1 as en1 inner join en1.child child where child.name = ? 

I'm surprise that both queries don't execute in the same time. They're equivalent, and should use the same indices.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your answer, but I want to force hibernate to convert en1.child to sql query like sampletable1 as tbl1 inner join samplechild as child on child.tbl1_fk = tbls1.id
Yes. That's precisely what the query in my answer does.
I changed my HibernateDialect and everything goes right now and all queries are converting to inner join.
@JBNizet the both queries executed in the same time on database, but hibernate uses reflection after getting results (in case of cross join).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.