Hibernate HQL: is JOIN really necessary?

Bruno 82

I'm learning Hibernate and I'm wondering what's the use of the JOIN clause within a HQL query. I might be wrong, but it seems to me that you can always do without.

Let's say I have a ChildClass entity which has a mapped @ManyToOne relationship with a ParentClass.

Now I can build a HQL query like this:

session.createQuery("FROM ChildClass ch WHERE ch.parentClass.id=1L")

As you can see I could filter the results by the parent class' id field, accessing it through the child class: ch.parentClass.id

If I wanted to do the same in SQL, I'd need to perform a JOIN, and the query would be the following:

SELECT * FROM ChildTable ch
JOIN ParentTable p ON (ch.parent_id = p.id)
WHERE p.id=1;

If I got this straight, in HQL I don't need to include any JOIN in my query, because the @ManyToOne mapped relationship implicitly joins the two entities. So why would I ever use JOIN in a HQL query? Is there any specific kind of situations where JOIN is necessary?

Naros

There are several situations where explicitly specifying the join is useful

  1. You want to specify a different join-type than the mapping, e.g. INNER vs OUTER.
  2. You want to specify a join-fetch scenario where not only do you want to join the association but you'd also like the query results to fetch the association too.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Hibernate HQL: Left Join with OnetoOne

From Dev

Hibernate chokes on this HQL cross join

From Dev

Hibernate HQL join fetch not recursively fetching

From Dev

Hibernate fetch=join/select is not applicable for HQL?

From Dev

Hibernate HQl to join two level tables

From Dev

How to query join one to many in hibernate HQL

From Dev

Hibernate HQL bulk delete don't remove join table values

From Dev

Hibernate: HQL join query on association doesnt result as expected

From Dev

Hibernate LEFT JOIN HQL query not counting correctly with condition

From Dev

Hibernate HQL bulk delete don't remove join table values

From Dev

Hibernate: HQL join query on association doesnt result as expected

From Dev

Hibernate HQL from child to parent table using left join

From Java

Is isNaN() really necessary?

From Dev

Are optionals really necessary in Swift?

From Dev

Is a JSON header really necessary?

From Dev

Is an antispyware really necessary?

From Dev

Is a mutex really necessary in this piece of code?

From Dev

PHP security what is really necessary

From Dev

Is the [1] in Muenchian grouping really necessary?

From Dev

Is PHP password salt really necessary?

From Dev

is the decorator pattern really necessary in this example?

From Dev

Is Vuex really necessary in vue 2?

From Dev

Explicit type conversion really necessary?

From Dev

Is a mutex really necessary in this piece of code?

From Dev

Is it really necessary for datetime instruction to be this long?

From Dev

Using JPQL(not HQL) with Hibernate

From Dev

Configuring Hibernate for HQL

From Dev

hibernate HQL Query select

From Dev

HQL Hibernate many tables

Related Related

HotTag

Archive