Created
April 19, 2015 03:36
-
-
Save thjanssen/148e78c95301776054d7 to your computer and use it in GitHub Desktop.
JPA 2.1 Entity Graph - Part 2: Define lazy/eager loading at runtime (http://www.thoughts-on-java.org/2014/04/jpa-21-entity-graph-part-2-define.html)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
EntityGraph<Order> graph = this.em.createEntityGraph(Order.class); | |
graph.addAttributeNodes("items"); | |
Map<String, Object> hints = new HashMap<String, Object>(); | |
hints.put("javax.persistence.loadgraph", graph); | |
this.em.find(Order.class, orderId, hints); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
EntityGraph<Order> graph = this.em.createEntityGraph(Order.class); | |
Subgraph<OrderItem> itemGraph = graph.addSubgraph("items"); | |
itemGraph.addAttributeNodes("product"); | |
Map<String, Object> hints = new HashMap<String, Object>(); | |
hints.put("javax.persistence.loadgraph", graph); | |
return this.em.find(Order.class, orderId, hints); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity | |
@Table(name = "purchaseOrder") | |
@NamedEntityGraph(name = "graph.Order.items", | |
attributeNodes = @NamedAttributeNode(value = "items", subgraph = "items"), | |
subgraphs = @NamedSubgraph(name = "items", attributeNodes = @NamedAttributeNode("product"))) | |
public class Order implements Serializable { | |
@Id | |
@GeneratedValue(strategy = GenerationType.AUTO) | |
@Column(name = "id", updatable = false, nullable = false) | |
private Long id = null; | |
@Version | |
@Column(name = "version") | |
private int version = 0; | |
@Column | |
private String orderNumber; | |
@OneToMany(mappedBy = "order", fetch = FetchType.LAZY) | |
private Set<OrderItem> items = new HashSet<OrderItem>(); | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity | |
public class OrderItem implements Serializable | |
{ | |
@Id | |
@GeneratedValue(strategy = GenerationType.AUTO) | |
@Column(name = "id", updatable = false, nullable = false) | |
private Long id = null; | |
@Version | |
@Column(name = "version") | |
private int version = 0; | |
@Column | |
private int quantity; | |
@ManyToOne | |
private Order order; | |
@ManyToOne(fetch = FetchType.LAZY) | |
private Product product; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity | |
public class Product implements Serializable | |
{ | |
@Id | |
@GeneratedValue(strategy = GenerationType.AUTO) | |
@Column(name = "id", updatable = false, nullable = false) | |
private Long id = null; | |
@Version | |
@Column(name = "version") | |
private int version = 0; | |
@Column | |
private String name; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2014-04-07 20:08:15,260 DEBUG [org.hibernate.loader.plan.build.spi.LoadPlanTreePrinter] (default task-2) LoadPlan(entity=blog.thoughts.on.java.jpa21.entity.graph.model.Order) | |
- Returns | |
- EntityReturnImpl(entity=blog.thoughts.on.java.jpa21.entity.graph.model.Order, querySpaceUid=, path=blog.thoughts.on.java.jpa21.entity.graph.model.Order) | |
- CollectionAttributeFetchImpl(collection=blog.thoughts.on.java.jpa21.entity.graph.model.Order.items, querySpaceUid=, path=blog.thoughts.on.java.jpa21.entity.graph.model.Order.items) | |
- (collection element) CollectionFetchableElementEntityGraph(entity=blog.thoughts.on.java.jpa21.entity.graph.model.OrderItem, querySpaceUid=, path=blog.thoughts.on.java.jpa21.entity.graph.model.Order.items.) | |
- QuerySpaces | |
- EntityQuerySpaceImpl(uid=, entity=blog.thoughts.on.java.jpa21.entity.graph.model.Order) | |
- SQL table alias mapping - order0_ | |
- alias suffix - 0_ | |
- suffixed key columns - {id1_2_0_} | |
- JOIN (JoinDefinedByMetadata(items)) : -> | |
- CollectionQuerySpaceImpl(uid=, collection=blog.thoughts.on.java.jpa21.entity.graph.model.Order.items) | |
- SQL table alias mapping - items1_ | |
- alias suffix - 1_ | |
- suffixed key columns - {order_id4_2_1_} | |
- entity-element alias suffix - 2_ | |
- 2_entity-element suffixed key columns - id1_0_2_ | |
- JOIN (JoinDefinedByMetadata(elements)) : -> | |
- EntityQuerySpaceImpl(uid=, entity=blog.thoughts.on.java.jpa21.entity.graph.model.OrderItem) | |
- SQL table alias mapping - items1_ | |
- alias suffix - 2_ | |
- suffixed key columns - {id1_0_2_} | |
2014-04-07 20:08:15,260 DEBUG [org.hibernate.loader.entity.plan.EntityLoader] (default task-2) Static select for entity blog.thoughts.on.java.jpa21.entity.graph.model.Order [NONE:-1]: select order0_.id as id1_2_0_, order0_.orderNumber as orderNum2_2_0_, order0_.version as version3_2_0_, items1_.order_id as order_id4_2_1_, items1_.id as id1_0_1_, items1_.id as id1_0_2_, items1_.order_id as order_id4_0_2_, items1_.product_id as product_5_0_2_, items1_.quantity as quantity2_0_2_, items1_.version as version3_0_2_ from purchaseOrder order0_ left outer join OrderItem items1_ on order0_.id=items1_.order_id where order0_.id=? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment