- Notifications
You must be signed in to change notification settings - Fork3
mtumilowicz/hibernate-lazy-initialization-exception
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
The main goal of this project is to show typical problems with lazyfetching -LazyInitializationException.
Reference:http://docs.jboss.org/hibernate/orm/5.3/userguide/html_single/Hibernate_User_Guide.html
Reference:http://download.oracle.com/otn-pub/jcp/persistence-2_2-mrel-spec/JavaPersistence.pdf?AuthParam=1538677370_fbd3bf5088a447d9721552ad131ae9c7
Fetching, essentially, is the process of grabbing data from thedatabase and making it available to the application.
We have two approaches (provided by JPA 2.2):
public enum FetchType { LAZY, EAGER };EAGER- requirement on the persistence providerruntime that the value must be eagerly fetched,LAZY- hint to the persistence provider runtime.
| Annotation | Default |
|---|---|
@OneToOne | EAGER |
@OneToMany | LAZY |
@ManyToOne | EAGER |
@ManyToMany | LAZY |
@Basic | EAGER |
LazyInitializationException indicates an attempt to access not-yet-fetched data outsideof a session context.
For example, when an uninitialized proxy or collection is accessed after the session was closed.
noSession_onlyGetter- there is no exception because we don't fireloading:repository.findById(1).map(Employee::getIssues);doesn't consume the collection so there is no need to load it.
noSession_consumingCollectionrepository.findById(1).map(Employee::getIssues).map(Collection::size);consumes the collection so query is fired and throws the exception(there is no open session).
transactional()repository.findById(1).map(Employee::getIssues).map(Collection::size);we consume the collection but we also provided the open session by
@Transactionalannotation.
Please refer my other project:https://github.com/mtumilowicz/hibernate-batch-size
About
Simple example of LazyInitializationException.
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.