Default fetch type for one-to-one, many-to-one and one-to-many in Hibernate
It depends on whether you are using JPA or Hibernate.
From the JPA 2.0 spec, the defaults are:
OneToMany: LAZY
ManyToOne: EAGER
ManyToMany: LAZY
OneToOne: EAGER
And in hibernate, all is Lazy
UPDATE:
The latest version of Hibernate aligns with the above JPA defaults.
JPA default fetch type
To date, I have chosen to have Hibernate follow the JPA spec in terms of mapping via annotations simply because I have not received any feature requests for making it configurable, which was surprising TBH. As you point out, since Hibernate 3.0 what you want has been the default set up when using hbm.xml
mapping files.
Allowing this via configuration would not violate the spec as another answer suggested.
So long story short, no it is not possible today. Create a feature request if you'd like to see that be configurable.
How hibernate decide which of FetchMode use by default?
If the Hibernate annotation @Fetch is not present on a field, then the default FetchMode for this field is:
- if this field has FetchType = EAGER, then FetchMode = JOIN.
- Otherwise, FetchMode = SELECT.
My source for this information is the code itself (Hibernate 5.0): HERE, HERE and most importantly, HERE.
Difference between FetchType LAZY and EAGER in Java Persistence API?
Sometimes you have two entities and there's a relationship between them. For example, you might have an entity called University
and another entity called Student
and a University might have many Students:
The University entity might have some basic properties such as id, name, address, etc. as well as a collection property called students that returns the list of students for a given university:
public class University {
private String id;
private String name;
private String address;
private List<Student> students;
// setters and getters
}
Now when you load a University from the database, JPA loads its id, name, and address fields for you. But you have two options for how students should be loaded:
- To load it together with the rest of the fields (i.e. eagerly), or
- To load it on-demand (i.e. lazily) when you call the university's
getStudents()
method.
When a university has many students it is not efficient to load all of its students together with it, especially when they are not needed and in suchlike cases you can declare that you want students to be loaded when they are actually needed. This is called lazy loading.
Here's an example, where students
is explicitly marked to be loaded eagerly:
@Entity
public class University {
@Id
private String id;
private String name;
private String address;
@OneToMany(fetch = FetchType.EAGER)
private List<Student> students;
// etc.
}
And here's an example where students
is explicitly marked to be loaded lazily:
@Entity
public class University {
@Id
private String id;
private String name;
private String address;
@OneToMany(fetch = FetchType.LAZY)
private List<Student> students;
// etc.
}
Related Topics
Implementation Difference Between Aggregation and Composition in Java
When to Use Atomicreference in Java
Java 8, Streams to Find the Duplicate Elements
Java Outofmemoryerror Strange Behaviour
Why Is "Final" Not Allowed in Java 8 Interface Methods
Create MySQL Database from Java
Spring MVC Type Conversion:Propertyeditor or Converter
How to Add Shapes on Javafx Linechart
Difference Between One-To-Many, Many-To-One and Many-To-Many
Java Equivalent to #Region in C#
Plsql Jdbc: How to Get Last Row Id
When Is It Ok to Catch Nullpointerexception
Hibernate Sessionfactory VS. JPA Entitymanagerfactory
Should You Always Code to Interfaces in Java
Java's Bigdecimal.Power(Bigdecimal Exponent): Is There a Java Library That Does It