Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Saturday, January 30, 2016

Difference between FetchType LAZY and EAGER in Java persistence?

Difference between FetchType LAZY and EAGER in Java persistence?


I am a newbie to Java persistence and Hibernate.

What is the difference between FetchType.LAZY and FetchType.EAGER in Java persistence?

Answer by unbeli for Difference between FetchType LAZY and EAGER in Java persistence?


Basically,

LAZY = fetch when needed  EAGER = fetch immediately  

Answer by T.J. Crowder for Difference between FetchType LAZY and EAGER in Java persistence?


From the Javadoc:

The EAGER strategy is a requirement on the persistence provider runtime that data must be eagerly fetched. The LAZY strategy is a hint to the persistence provider runtime that data should be fetched lazily when it is first accessed.

E.g., eager is more proactive than lazy. Lazy only happens on first use (if the provider takes the hint), whereas with eager things (may) get pre-fetched.

Answer by Behrang for Difference between FetchType LAZY and EAGER in Java persistence?


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.

The University entity might have some basic properties such as id, name, address, etc. as well as a property called students:

public class University {   private String id;   private String name;   private String address;   private List 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 students: 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 with it when they are not needed. So in suchlike cases, you can declare that you want students to be loaded when they are actually needed. This is called lazy loading.

Answer by Bozho for Difference between FetchType LAZY and EAGER in Java persistence?


EAGER loading of collections means that they are fetched fully at the time their parent is fetched. So if you have Course and it has List, all the students are fetched from the database at the time the Course is fetched.

LAZY on the other hand means that the contents of the List are fetched only when you try to access them. For example, by calling course.getStudents().iterator(). Calling any access method on the List will initiate a call to the database to retrieve the elements. This is implemented by creating a Proxy around the List (or Set). So for your lazy collections, the concrete types are not ArrayList and HashSet, but PersistentSet and PersistentList (or PersistentBag)

Answer by Kyung Min for Difference between FetchType LAZY and EAGER in Java persistence?


I may consider performance and memory utilization. One big difference is that EAGER fetch strategy allows to use fetched data object without session. Why?
All data is fetched when eager marked data in the object when session is connected. However, in case of lazy loading strategy, lazy loading marked object does not retrieve data if session is disconnected (after session.close() statement). All that can be made by hibernate proxy. Eager strategy lets data to be still available after closing session.

Answer by babai for Difference between FetchType LAZY and EAGER in Java persistence?


By default, for all collection and map objects the fetching rule is FetchType.LAZY and for other instances it follows the FetchType.EAGER policy.
In brief, @OneToMany and @ManyToMany relations does not fetch the related objects (collection and map) implicictly but the retrieval operation is cascaded through the field in @OneToOne and @ManyToOne ones.

(courtesy :- objectdbcom)

Answer by Jules Martel for Difference between FetchType LAZY and EAGER in Java persistence?


@drop-shadow if you're using Hibernate, you can call Hibernate.initialize() when you invoke the getStudents() method:

Public class UniversityDaoImpl extends GenericDaoHibernate implements UniversityDao {      //...      @Override      public University get(final Integer id) {          Query query = getQuery("from University u where idUniversity=:id").setParameter("id", id).setMaxResults(1).setFetchSize(1);          University university = (University) query.uniqueResult();          ***Hibernate.initialize(university.getStudents());***          return university;      }      //...  }  

Answer by JDeveloper for Difference between FetchType LAZY and EAGER in Java persistence?


As per my knowledge both type of fetch depends your requirement.

FetchType.LAZY is on demand (i.e. when we required the data).

FetchType.EAGER is immediate (i.e. before our requirement comes we are unnecessarily fetching the record)

Answer by Vlad Mihalcea for Difference between FetchType LAZY and EAGER in Java persistence?


Both FetchType.LAZY and FetchType.EAGER are used to define the default fetch plan.

Unfortunately you can only override the default fetch plan for LAZY fetching. EAGER fetching is less flexible and can lead to many performance issues.

My advice is to restrain the urge of making your associations EAGER, because fetching is a query-time responsibility. So all your queries should use the fetch directive to only retrieve what's necessary for the current business case.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.