Skip to content

Commit cd964a9

Browse files
committed
HHH-8855 corrected EntityGraph loadplan strategy, testcase cleanup
1 parent aff694b commit cd964a9

File tree

2 files changed

+73
-72
lines changed

2 files changed

+73
-72
lines changed

hibernate-core/src/main/java/org/hibernate/loader/plan/build/internal/AbstractEntityGraphVisitationStrategy.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.HashMap;
3030
import java.util.List;
3131
import java.util.Map;
32+
3233
import javax.persistence.AttributeNode;
3334
import javax.persistence.Subgraph;
3435
import javax.persistence.metamodel.Attribute;
@@ -54,7 +55,6 @@
5455
import org.hibernate.persister.walking.spi.CompositionDefinition;
5556
import org.hibernate.persister.walking.spi.EntityDefinition;
5657
import org.hibernate.persister.walking.spi.WalkingException;
57-
5858
import org.jboss.logging.Logger;
5959

6060
/**
@@ -106,12 +106,14 @@ protected AbstractEntityGraphVisitationStrategy(
106106
public void start() {
107107
super.start();
108108
graphStack.addLast( getRootEntityGraph() );
109+
attributeNodeImplementorMap = buildAttributeNodeMap();
109110
}
110111

111112
@Override
112113
public void finish() {
113114
super.finish();
114115
graphStack.removeLast();
116+
attributeNodeImplementorMap = Collections.emptyMap();
115117
//applying a little internal stack checking
116118
if ( !graphStack.isEmpty() || !attributeStack.isEmpty() || !attributeNodeImplementorMap.isEmpty() ) {
117119
throw new WalkingException( "Internal stack error" );
@@ -122,7 +124,6 @@ public void finish() {
122124
public void startingEntity(final EntityDefinition entityDefinition) {
123125
//TODO check if the passed in entity definition is the same as the root entity graph (a.k.a they are came from same entity class)?
124126
//this maybe the root entity graph or a sub graph.
125-
attributeNodeImplementorMap = buildAttributeNodeMap();
126127
super.startingEntity( entityDefinition );
127128
}
128129

@@ -142,12 +143,6 @@ protected Map<String, AttributeNodeImplementor> buildAttributeNodeMap() {
142143
return attributeNodeImplementorMap;
143144
}
144145

145-
@Override
146-
public void finishingEntity(final EntityDefinition entityDefinition) {
147-
attributeNodeImplementorMap = Collections.emptyMap();
148-
super.finishingEntity( entityDefinition );
149-
}
150-
151146
/**
152147
* I'm using NULL-OBJECT pattern here, for attributes that not existing in the EntityGraph,
153148
* a predefined NULL-ATTRIBUTE-NODE is pushed to the stack.

hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/graphs/find/FindEntityGraphTests.java

Lines changed: 70 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -23,101 +23,107 @@
2323
*/
2424
package org.hibernate.jpa.test.graphs.find;
2525

26-
import org.hibernate.Hibernate;
27-
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
28-
import org.junit.Test;
26+
import static org.junit.Assert.assertTrue;
2927

30-
import javax.persistence.*;
3128
import java.util.HashMap;
3229
import java.util.Map;
3330

34-
import static org.junit.Assert.*;
31+
import javax.persistence.Entity;
32+
import javax.persistence.EntityGraph;
33+
import javax.persistence.EntityManager;
34+
import javax.persistence.FetchType;
35+
import javax.persistence.GeneratedValue;
36+
import javax.persistence.Id;
37+
import javax.persistence.ManyToOne;
38+
39+
import org.hibernate.Hibernate;
40+
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
41+
import org.junit.Test;
3542

3643
/**
3744
* @author Christian Bauer
3845
*/
3946
public class FindEntityGraphTests extends BaseEntityManagerFunctionalTestCase {
4047

41-
@Override
42-
protected Class<?>[] getAnnotatedClasses() {
43-
return new Class[]{Foo.class, Bar.class, Baz.class};
44-
}
48+
@Override
49+
protected Class<?>[] getAnnotatedClasses() {
50+
return new Class[] { Foo.class, Bar.class, Baz.class };
51+
}
4552

46-
@Test
47-
public void loadParallelManyToOne() {
48-
EntityManager em = getOrCreateEntityManager();
49-
em.getTransaction().begin();
53+
@Test
54+
public void loadMultipleAssociations() {
55+
EntityManager em = getOrCreateEntityManager();
56+
em.getTransaction().begin();
5057

51-
Bar bar = new Bar();
52-
bar.id = 1;
53-
bar.name = "bar";
54-
em.persist(bar);
58+
Bar bar = new Bar();
59+
bar.name = "bar";
60+
em.persist( bar );
5561

56-
Baz baz = new Baz();
57-
baz.id = 2;
58-
baz.name = "baz";
59-
em.persist(baz);
62+
Baz baz = new Baz();
63+
baz.name = "baz";
64+
em.persist( baz );
6065

61-
Foo foo = new Foo();
62-
foo.id = 3;
63-
foo.name = "foo";
64-
foo.bar = bar;
65-
foo.baz = baz;
66-
em.persist(foo);
66+
Foo foo = new Foo();
67+
foo.name = "foo";
68+
foo.bar = bar;
69+
foo.baz = baz;
70+
em.persist( foo );
6771

68-
em.getTransaction().commit();
69-
em.close();
72+
em.getTransaction().commit();
73+
em.clear();
7074

71-
em = getOrCreateEntityManager();
72-
em.getTransaction().begin();
75+
em.getTransaction().begin();
7376

74-
EntityGraph<Foo> fooGraph = em.createEntityGraph(Foo.class);
75-
fooGraph.addAttributeNodes("bar", "baz");
77+
EntityGraph<Foo> fooGraph = em.createEntityGraph( Foo.class );
78+
fooGraph.addAttributeNodes( "bar", "baz" );
7679

77-
Map<String, Object> properties = new HashMap<String, Object>();
78-
properties.put("javax.persistence.loadgraph", fooGraph);
80+
Map<String, Object> properties = new HashMap<String, Object>();
81+
properties.put( "javax.persistence.loadgraph", fooGraph );
7982

80-
Foo result = em.find(Foo.class, foo.id, properties);
83+
Foo result = em.find( Foo.class, foo.id, properties );
8184

82-
assertTrue(Hibernate.isInitialized(result));
83-
assertTrue(Hibernate.isInitialized(result.bar));
84-
assertTrue(Hibernate.isInitialized(result.baz));
85+
assertTrue( Hibernate.isInitialized( result ) );
86+
assertTrue( Hibernate.isInitialized( result.bar ) );
87+
assertTrue( Hibernate.isInitialized( result.baz ) );
8588

86-
em.getTransaction().commit();
87-
em.close();
88-
}
89+
em.getTransaction().commit();
90+
em.close();
91+
}
8992

90-
@Entity
91-
public static class Foo {
93+
@Entity
94+
public static class Foo {
9295

93-
@Id
94-
public Integer id;
96+
@Id
97+
@GeneratedValue
98+
public Integer id;
9599

96-
public String name;
100+
public String name;
97101

98-
@ManyToOne(fetch = FetchType.LAZY)
99-
public Bar bar;
102+
@ManyToOne(fetch = FetchType.LAZY)
103+
public Bar bar;
100104

101-
@ManyToOne(fetch = FetchType.LAZY)
102-
public Baz baz;
103-
}
105+
@ManyToOne(fetch = FetchType.LAZY)
106+
public Baz baz;
107+
}
104108

105-
@Entity
106-
public static class Bar {
109+
@Entity
110+
public static class Bar {
107111

108-
@Id
109-
public Integer id;
112+
@Id
113+
@GeneratedValue
114+
public Integer id;
110115

111-
public String name;
112-
}
116+
public String name;
117+
}
113118

114-
@Entity
115-
public static class Baz {
119+
@Entity
120+
public static class Baz {
116121

117-
@Id
118-
public Integer id;
122+
@Id
123+
@GeneratedValue
124+
public Integer id;
119125

120-
public String name;
121-
}
126+
public String name;
127+
}
122128

123129
}

0 commit comments

Comments
 (0)