Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Simple example of LazyInitializationException.

NotificationsYou must be signed in to change notification settings

mtumilowicz/hibernate-lazy-initialization-exception

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

hibernate-lazy-initialization-exception

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

preface

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.

annotations

AnnotationDefault
@OneToOneEAGER
@OneToManyLAZY
@ManyToOneEAGER
@ManyToManyLAZY
@BasicEAGER

LazyInitializationException

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.

tests

  • 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_consumingCollection

    repository.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@Transactional annotation.

n+1 query problem

Please refer my other project:https://github.com/mtumilowicz/hibernate-batch-size

About

Simple example of LazyInitializationException.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp