N+1 Select Issues with Hibernate Part 2: Solving n+1 select issues - - PowerPoint PPT Presentation

n 1 select issues with hibernate
SMART_READER_LITE
LIVE PREVIEW

N+1 Select Issues with Hibernate Part 2: Solving n+1 select issues - - PowerPoint PPT Presentation

How to find and fix N+1 Select Issues with Hibernate Part 2: Solving n+1 select issues with @NamedEntityGraphs www.thoughts-on-java.org Course Structure Part 1: What is the n+1 select issue? How to find it in your project?


slide-1
SLIDE 1

How to find and fix N+1 Select Issues with Hibernate

Part 2: Solving n+1 select issues with @NamedEntityGraphs

www.thoughts-on-java.org

slide-2
SLIDE 2

Course Structure

  • Part 1:
  • What is the n+1 select issue?
  • How to find it in your project?
  • Part 2:
  • Solving n+1 select issues with @NamedEntityGraphs
  • Part 3:
  • Solving n+1 select issues with dynamic EntityGraphs

www.thoughts-on-java.org

slide-3
SLIDE 3

Application

www.thoughts-on-java.org

slide-4
SLIDE 4

@NamedEntityGraph

www.thoughts-on-java.org

slide-5
SLIDE 5

@NamedEntityGraph

  • Introduced in JPA 2.1
  • Declaratively defines a graph of entities which will be loaded
  • Graph is query independent

www.thoughts-on-java.org

slide-6
SLIDE 6

@NamedEntityGraph

  • Define a simple @NamedEntityGraph

www.thoughts-on-java.org

@NamedEntityGraph( name = "graph.AuthorBooksReviews", attributeNodes = @NamedAttributeNode(value = "books") )

slide-7
SLIDE 7

@NamedEntityGraph

  • Define a multi-level @NamedEntityGraph

www.thoughts-on-java.org

@NamedEntityGraph( name = "graph.AuthorBooksReviews", attributeNodes = @NamedAttributeNode(value = "books", subgraph = "books"), subgraphs = @NamedSubgraph( name = "books", attributeNodes = @NamedAttributeNode("reviews") ) )

slide-8
SLIDE 8

@NamedEntityGraph

  • Provide entity graph as a hint

www.thoughts-on-java.org

EntityGraph graph = this.em.getEntityGraph("graph.AuthorBooks"); this.em.createQuery("SELECT DISTINCT a FROM Author a") .setHint("javax.persistence.loadgraph", graph);

slide-9
SLIDE 9

@NamedEntityGraph

  • Fetch graph
  • Eager loading for all elements of the graph
  • Lazy loading for all other attributes
  • Load graph
  • Eager loading for all elements of the graph
  • Loads all other attributes with their defined FetchType
  • Hib

iber erna nate te alwa always ys uses es a a lo load ad grap aph

  • HHH-8776

www.thoughts-on-java.org

slide-10
SLIDE 10

@NamedEntityGraph

  • Advantages
  • Query specific EAGER loading
  • Definition of the graph is independent of the query
  • Disadvantages
  • Creates cartesian product

www.thoughts-on-java.org

slide-11
SLIDE 11

Want to learn how to identify and fix other Hibernate performance issues?

Join my

Hib Hiber erna nate te Per erforma formanc nce e Tuni ning ng On Online ine Train ining: ing:

www.thoughts-on-java.org/course-hibernate- performance-tuning