What is the difference between JOIN and JOIN FETCH when using JPA and Hibernate
In this two queries, you are using JOIN to query all employees that have at least one department associated.
But, the difference is: in the first query you are returning only the Employes for the Hibernate. In the second query, you are returning the Employes and all Departments associated.
So, if you use the second query, you will not need to do a new query to hit the database again to see the Departments of each Employee.
You can use the second query when you are sure that you will need the Department of each Employee. If you not need the Department, use the first query.
I recomend read this link if you need to apply some WHERE condition (what you probably will need): How to properly express JPQL "join fetch" with "where" clause as JPA 2 CriteriaQuery?
Update
If you don't use fetch
and the Departments continue to be returned, is because your mapping between Employee and Department (a @OneToMany
) are setted with FetchType.EAGER
. In this case, any HQL (with fetch
or not) query with FROM Employee
will bring all Departments. Remember that all mapping *ToOne (@ManyToOne
and @OneToOne
) are EAGER by default.
What is the difference between inner join and inner join fetch ? ,HQL
what Koitoer has mentioned is correct
Additionally
In your Account person is normally set to load lazily, even if you join it in the HQL, the collection person may not load, you have to use 'fetch' to load them.
What's the difference between JOIN FETCH and JOIN ?
Remember that, in the end, JOIN FETCH is always a JOIN.
By default @OneToMany
relationships are lazy loaded.
Using JOIN FETCH you load the related entities in advance.
You can find more info reading FETCH JOIN is still a JOIN by Piotr Nowicki
JPA / Hibernate Spring @Transactional vs. JOIN FETCH
There's a couple of things we need to untangle here.
- @Transactional means nothing more than Spring makes sure to open up a database connection(+ transaction) and closes it again. That's it.
- When you select an entity containing a lazy field, you are essentially saying: I am selecting "some" fields from my entity, except the lazy one.
- If you, however, need that lazy field later on because you are trying to access it in your views (.html, .ftl, .jsp, whatever) you need to issue another select to the database to fetch it.
- The problem: At that point, if you are outside of a @Transactional method you do not have a database connection open anymore, hence the LazyInitException.
- To sum up: Your fetch makes sure to issue 1 select for all the data. If you do NOT do that, you need an open database connection/transaction, which @Transactional gives you.
Recommendation: You should try and fetch all the data you need to render a view, with appropriate JPQL/Criteria/SQL statements and not rely on re-selecting lazy-fields too much.
Difference between LEFT JOIN and LEFT JOIN FETCH in Hibernate?
The "fetch" tells hibernate to load it now instead of letting it be loaded lazily. The reference guide has a whole chapter dealing with such things that it's good to be acquainted with.
Join vs join fetch
Don't use a debugger to test if an object is initialized. The debugger will call methods of your objects behind your back, to show the content of the object, and calling these methods will initialize the object.
Use Hibernate.isInitialized(question.getSection())
to test if the session is initialized.
Related Topics
Mockito - Difference Between Doreturn() and When()
How to Change Jdk Version for an Eclipse Project
Strings Are Objects in Java, So Why Don't We Use 'New' to Create Them
Why am I Getting Inputmismatchexception
Why Is It Not Possible to Extend Annotations in Java
How to Change Text Color in the Jtextarea
Explicit Casting from Super Class to Subclass
Javafx "Location Is Required." Even Though It Is in the Same Package
Why Does the Behavior of the Integer Constant Pool Change at 127
How to Ignore Ssl Certificate Errors in Apache Httpclient 4.0
How to Determine the Class of a Generic Type
Handling Passwords Used for Auth in Source Code
Java Remove Duplicates from an Array
How to Compare Two Dates Without the Time Portion
Difference Between Java Hh:Mm and Hh:Mm on Simpledateformat