Giter Club home page Giter Club logo

hibernate-lazy-initialization-exception's Introduction

Build Status

hibernate-lazy-initialization-exception

The main goal of this project is to show typical problems with lazy fetching - 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 the database 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 provider runtime that the value must be eagerly fetched,
    • LAZY - hint to the persistence provider runtime.

annotations

Annotation Default
@OneToOne EAGER
@OneToMany LAZY
@ManyToOne EAGER
@ManyToMany LAZY
@Basic EAGER

LazyInitializationException

LazyInitializationException indicates an attempt to access not-yet-fetched data outside of 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 fire loading:

    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

hibernate-lazy-initialization-exception's People

Contributors

mtumilowicz avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.