Created
April 19, 2015 04:11
-
-
Save thjanssen/cadda834eaf1c44efc0b to your computer and use it in GitHub Desktop.
5 ways to initialize lazy relations and when to use them (http://www.thoughts-on-java.org/2014/12/5-ways-to-initialize-lazy-relations.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
@Entity | |
@NamedEntityGraph(name = "graph.Order.items", | |
attributeNodes = @NamedAttributeNode("items")) | |
public class Order implements Serializable { | |
.... |
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 graph = this.em.createEntityGraph(Order.class); | |
Subgraph itemGraph = graph.addSubgraph("items"); | |
Map hints = new HashMap(); | |
hints.put("javax.persistence.loadgraph", graph); | |
Order order = 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
CriteriaBuilder cb = em.getCriteriaBuilder(); | |
CriteriaQuery q = cb.createQuery(Order.class); | |
Root o = q.from(Order.class); | |
o.fetch("items", JoinType.INNER); | |
q.select(o); | |
q.where(cb.equal(o.get("id"), orderId)); | |
Order order = (Order)this.em.createQuery(q).getSingleResult(); |
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
Query q = this.em.createQuery("SELECT o FROM Order o JOIN FETCH o.items i WHERE o.id = :id"); | |
q.setParameter("id", orderId); | |
newOrder = (Order) q.getSingleResult(); |
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
Order order = this.em.find(Order.class, orderId); | |
order.getItems().size(); |
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 graph = this.em.getEntityGraph("graph.Order.items"); | |
Map hints = new HashMap(); | |
hints.put("javax.persistence.fetchgraph", graph); | |
Order order = this.em.find(Order.class, orderId, hints); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment