Skip to content

Commit

Permalink
Hibernate Learning - EntityGraph Example Added
Browse files Browse the repository at this point in the history
  • Loading branch information
NRuslanR committed Sep 27, 2022
1 parent d711152 commit f10ac70
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"name": "Launch HibernateLearning",
"request": "launch",
"mainClass": "com.learning.hibernate.HibernateLearning",
"args": "FetchMode",
"args": "EntityGraph",
"projectName": "hibernate"
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.NamedAttributeNode;
import jakarta.persistence.NamedEntityGraph;
import jakarta.persistence.OneToMany;
import jakarta.persistence.SequenceGenerator;
import jakarta.persistence.Table;
Expand All @@ -25,6 +27,10 @@
@Table(name = "shopping_carts")
@ToString(callSuper = true)
@SequenceGenerator(name = "pk_gen", sequenceName = "sc_pk_gen", initialValue = 100)
@NamedEntityGraph(
name = "shopping-cart-graph",
attributeNodes = @NamedAttributeNode(value = "items")
)
public class ShoppingCart extends SequencedExampleEntity {

@NonNull
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.learning.hibernate.examples;

import java.util.Collection;
import java.util.stream.IntStream;

import org.hibernate.Session;
import org.hibernate.stat.internal.SessionStatisticsImpl;

import com.learning.hibernate.entities.ShoppingCart;
import com.learning.hibernate.entities.ShoppingCartItem;
import com.learning.hibernate.utils.HibernateUtils;
import com.learning.hibernate.values.Money;

import jakarta.persistence.EntityGraph;

public class EntityGraphExample extends HibernateExample {

public EntityGraphExample() {
super("EntityGraph");
}

@Override
protected void doRun(Session session, Object data) throws Exception {

prepareData(session);
runEntityGraphExample(session);

}

private void prepareData(Session session) {

final int itemCount = 5, childItemCount = 5;

Collection<ShoppingCart> cartsToBePrepared =
IntStream
.rangeClosed(1, itemCount)
.boxed()
.map(i ->
new ShoppingCart(
"Cart" + i,
IntStream
.rangeClosed(1, childItemCount)
.boxed()
.map(j ->
ShoppingCartItem.of(
23, "Product" + j, Money.ofUSD(43
))
).toList()
)
).toList();

HibernateUtils.persistInTransaction(session, cartsToBePrepared);
}

private void runEntityGraphExample(Session session) {

EntityGraph rootGraph = session.getEntityGraph("shopping-cart-graph");

Collection<ShoppingCart> carts =
session
.createQuery(
"select sc from ShoppingCart sc",
ShoppingCart.class
)
.setHint("jakarta.persistence.loadgraph", rootGraph)
.getResultList();

printEntities(carts, "carts fetched by entity load-based graph");

carts =
session
.createQuery(
"select sc from ShoppingCart sc", ShoppingCart.class
).setHint("jakarta.persistence.fetchgraph", rootGraph
).getResultList();

printEntities(carts, "carts fetched by entity fetch-based graph");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.learning.hibernate.examples.AttributeConvertersExample;
import com.learning.hibernate.examples.BatchOperationsExample;
import com.learning.hibernate.examples.CriteriaAPIExample;
import com.learning.hibernate.examples.EntityGraphExample;
import com.learning.hibernate.examples.EntityIdentifyingStrategiesExample;
import com.learning.hibernate.examples.EntityLifeCycleEventsExample;
import com.learning.hibernate.examples.EntityLifeCycleExample;
Expand Down Expand Up @@ -47,7 +48,8 @@ public static Examples getExamples() throws Exception
new SortingAndPaginationExample(),
new BatchOperationsExample(),
new CriteriaAPIExample(),
new FetchModeExample()
new FetchModeExample(),
new EntityGraphExample()
);

return examples;
Expand Down

0 comments on commit f10ac70

Please sign in to comment.