4

JPA entity graph: e.g.

Order - OrderItem - Product

@NamedEntityGraph(name = "order", 
   attributeNodes = @NamedAttributeNode(value = "orderItems", subgraph = "orderItems"), 
   subgraphs = @NamedSubgraph(name = "orderItems", attributeNodes = @NamedAttributeNode("product")))

Why there is no joinType for order -> orderItems and orderItem -> product? All the joins in an entity graph are supposed to be LEFT joins?

eastwater
  • 4,624
  • 9
  • 49
  • 118

2 Answers2

1

The purpose of an EntityGraph is to define what to include in the result graph.

That does not affect the join type. So your dependencies are loaded the same way as they are loaded without EntityGraph.

Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
  • 1
    CriteriaQuery can specify join types. But for find() method with an entityGraph, and use one SQL with joins to load all entities in the entityGraph, join types are needed. There is no way to specify join types in this case. – eastwater Nov 24 '17 at 18:38
1

Join Types have to be specified in JPQL queries or criteria queries while creating joins .

For example below query will result in inner join between three tables .

select o from Order o join fetch o.orderItems items join fetch items.product p

And Below query will result in left outer join between three tables :

select o from Order o left join  o.orderItems items left join  items.product p

But if you will execute above queries directly hibernate will return cross join of all the three tables .

**Hence to get the retro fitted(all relationships initialised properly ) entities you will have to pass the entity graph as hint to query .Entity graph will not change the join type you have specified in query or Criteria query. It will just indicate hibernate to fetch the relationships eagerly and retro fit them in entities properly . **

Check my github link : https://github.com/vaneetkataria/Jpa-Hibernate/blob/master/jdbcToJpaMigration/src/test/java/com/katariasoft/technologies/jpaHibernate/entity/fetch/joinfetch/JPQLEntityGraphsJoinFetchTests.java

Vaneet Kataria
  • 575
  • 5
  • 14