HHH-8855 corrected EntityGraph loadplan strategy, testcase cleanup

This commit is contained in:
Brett Meyer 2014-01-14 19:38:44 -05:00
parent 63ecc1c8c3
commit 76e0e42796
2 changed files with 73 additions and 72 deletions

View File

@ -29,6 +29,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.AttributeNode;
import javax.persistence.Subgraph;
import javax.persistence.metamodel.Attribute;
@ -54,7 +55,6 @@ import org.hibernate.persister.walking.spi.CollectionIndexDefinition;
import org.hibernate.persister.walking.spi.CompositionDefinition;
import org.hibernate.persister.walking.spi.EntityDefinition;
import org.hibernate.persister.walking.spi.WalkingException;
import org.jboss.logging.Logger;
/**
@ -106,12 +106,14 @@ public abstract class AbstractEntityGraphVisitationStrategy
public void start() {
super.start();
graphStack.addLast( getRootEntityGraph() );
attributeNodeImplementorMap = buildAttributeNodeMap();
}
@Override
public void finish() {
super.finish();
graphStack.removeLast();
attributeNodeImplementorMap = Collections.emptyMap();
//applying a little internal stack checking
if ( !graphStack.isEmpty() || !attributeStack.isEmpty() || !attributeNodeImplementorMap.isEmpty() ) {
throw new WalkingException( "Internal stack error" );
@ -122,7 +124,6 @@ public abstract class AbstractEntityGraphVisitationStrategy
public void startingEntity(final EntityDefinition entityDefinition) {
//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)?
//this maybe the root entity graph or a sub graph.
attributeNodeImplementorMap = buildAttributeNodeMap();
super.startingEntity( entityDefinition );
}
@ -142,12 +143,6 @@ public abstract class AbstractEntityGraphVisitationStrategy
return attributeNodeImplementorMap;
}
@Override
public void finishingEntity(final EntityDefinition entityDefinition) {
attributeNodeImplementorMap = Collections.emptyMap();
super.finishingEntity( entityDefinition );
}
/**
* I'm using NULL-OBJECT pattern here, for attributes that not existing in the EntityGraph,
* a predefined NULL-ATTRIBUTE-NODE is pushed to the stack.

View File

@ -23,101 +23,107 @@
*/
package org.hibernate.jpa.test.graphs.find;
import org.hibernate.Hibernate;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
import javax.persistence.*;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.*;
import javax.persistence.Entity;
import javax.persistence.EntityGraph;
import javax.persistence.EntityManager;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import org.hibernate.Hibernate;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.junit.Test;
/**
* @author Christian Bauer
*/
public class FindEntityGraphTests extends BaseEntityManagerFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[]{Foo.class, Bar.class, Baz.class};
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] { Foo.class, Bar.class, Baz.class };
}
@Test
public void loadParallelManyToOne() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
@Test
public void loadMultipleAssociations() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
Bar bar = new Bar();
bar.id = 1;
bar.name = "bar";
em.persist(bar);
Bar bar = new Bar();
bar.name = "bar";
em.persist( bar );
Baz baz = new Baz();
baz.id = 2;
baz.name = "baz";
em.persist(baz);
Baz baz = new Baz();
baz.name = "baz";
em.persist( baz );
Foo foo = new Foo();
foo.id = 3;
foo.name = "foo";
foo.bar = bar;
foo.baz = baz;
em.persist(foo);
Foo foo = new Foo();
foo.name = "foo";
foo.bar = bar;
foo.baz = baz;
em.persist( foo );
em.getTransaction().commit();
em.close();
em.getTransaction().commit();
em.clear();
em = getOrCreateEntityManager();
em.getTransaction().begin();
em.getTransaction().begin();
EntityGraph<Foo> fooGraph = em.createEntityGraph(Foo.class);
fooGraph.addAttributeNodes("bar", "baz");
EntityGraph<Foo> fooGraph = em.createEntityGraph( Foo.class );
fooGraph.addAttributeNodes( "bar", "baz" );
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("javax.persistence.loadgraph", fooGraph);
Map<String, Object> properties = new HashMap<String, Object>();
properties.put( "javax.persistence.loadgraph", fooGraph );
Foo result = em.find(Foo.class, foo.id, properties);
Foo result = em.find( Foo.class, foo.id, properties );
assertTrue(Hibernate.isInitialized(result));
assertTrue(Hibernate.isInitialized(result.bar));
assertTrue(Hibernate.isInitialized(result.baz));
assertTrue( Hibernate.isInitialized( result ) );
assertTrue( Hibernate.isInitialized( result.bar ) );
assertTrue( Hibernate.isInitialized( result.baz ) );
em.getTransaction().commit();
em.close();
}
em.getTransaction().commit();
em.close();
}
@Entity
public static class Foo {
@Entity
public static class Foo {
@Id
public Integer id;
@Id
@GeneratedValue
public Integer id;
public String name;
public String name;
@ManyToOne(fetch = FetchType.LAZY)
public Bar bar;
@ManyToOne(fetch = FetchType.LAZY)
public Bar bar;
@ManyToOne(fetch = FetchType.LAZY)
public Baz baz;
}
@ManyToOne(fetch = FetchType.LAZY)
public Baz baz;
}
@Entity
public static class Bar {
@Entity
public static class Bar {
@Id
public Integer id;
@Id
@GeneratedValue
public Integer id;
public String name;
}
public String name;
}
@Entity
public static class Baz {
@Entity
public static class Baz {
@Id
public Integer id;
@Id
@GeneratedValue
public Integer id;
public String name;
}
public String name;
}
}