From 52d095be9735a04e913bd8f8344c67f86a6b19a0 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Wed, 30 Oct 2013 10:45:19 -0500 Subject: [PATCH] HHH-8660 - NamedEntityGraphDefinition built too early --- .../cfg/annotations/EntityBinder.java | 38 +++++++------- .../NamedEntityGraphDefinition.java | 7 ++- .../internal/EntityManagerFactoryImpl.java | 15 +++++- .../jpa/test/graphs/named/basic/Person.java | 2 +- .../named/multiple/NamedEntityGraphsTest.java | 50 +++++++++++++++++++ .../test/graphs/named/multiple/Person.java | 42 ++++++++++++++++ 6 files changed, 132 insertions(+), 22 deletions(-) create mode 100644 hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/graphs/named/multiple/NamedEntityGraphsTest.java create mode 100644 hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/graphs/named/multiple/Person.java diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java index 0a919319ca..42c36959f5 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java @@ -164,25 +164,6 @@ public class EntityBinder { this.annotatedClass = annotatedClass; bindEjb3Annotation( ejb3Ann ); bindHibernateAnnotation( hibAnn ); - - processNamedEntityGraphs(); - } - - private void processNamedEntityGraphs() { - processNamedEntityGraph( annotatedClass.getAnnotation( NamedEntityGraph.class ) ); - final NamedEntityGraphs graphs = annotatedClass.getAnnotation( NamedEntityGraphs.class ); - if ( graphs != null ) { - for ( NamedEntityGraph graph : graphs.value() ) { - processNamedEntityGraph( graph ); - } - } - } - - private void processNamedEntityGraph(NamedEntityGraph annotation) { - if ( annotation == null ) { - return; - } - mappings.addNamedEntityGraphDefintion( new NamedEntityGraphDefinition( annotation, name, persistentClass.getEntityName() ) ); } @@ -425,6 +406,25 @@ public class EntityBinder { catch (MappingException me) { throw new AnnotationException( "Use of the same entity name twice: " + name, me ); } + + processNamedEntityGraphs(); + } + + private void processNamedEntityGraphs() { + processNamedEntityGraph( annotatedClass.getAnnotation( NamedEntityGraph.class ) ); + final NamedEntityGraphs graphs = annotatedClass.getAnnotation( NamedEntityGraphs.class ); + if ( graphs != null ) { + for ( NamedEntityGraph graph : graphs.value() ) { + processNamedEntityGraph( graph ); + } + } + } + + private void processNamedEntityGraph(NamedEntityGraph annotation) { + if ( annotation == null ) { + return; + } + mappings.addNamedEntityGraphDefintion( new NamedEntityGraphDefinition( annotation, name, persistentClass.getEntityName() ) ); } public void bindDiscriminatorValue() { diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/NamedEntityGraphDefinition.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/NamedEntityGraphDefinition.java index 3082e58770..740dddfc77 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/NamedEntityGraphDefinition.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/NamedEntityGraphDefinition.java @@ -42,7 +42,12 @@ public class NamedEntityGraphDefinition { this.annotation = annotation; this.jpaEntityName = jpaEntityName; this.entityName = entityName; - this.name = StringHelper.isEmpty( annotation.name() ) ? jpaEntityName : annotation.name(); + this.name = StringHelper.isNotEmpty( annotation.name() ) + ? annotation.name() + : jpaEntityName; + if ( name == null ) { + throw new IllegalArgumentException( "Named entity graph name cannot be null" ); + } } public String getRegisteredName() { diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/EntityManagerFactoryImpl.java b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/EntityManagerFactoryImpl.java index 52ac30b809..f82e5dca5c 100755 --- a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/EntityManagerFactoryImpl.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/internal/EntityManagerFactoryImpl.java @@ -233,7 +233,20 @@ public class EntityManagerFactoryImpl implements HibernateEntityManagerFactory { @SuppressWarnings("unchecked") private void applyNamedEntityGraphs(Collection namedEntityGraphs) { for ( NamedEntityGraphDefinition definition : namedEntityGraphs ) { - final EntityType entityType = metamodel.getEntityTypeByName( definition.getJpaEntityName() ); + log.debugf( + "Applying named entity graph [name=%s, entity-name=%s, jpa-entity-name=%s", + definition.getRegisteredName(), + definition.getEntityName(), + definition.getJpaEntityName() + ); + final EntityType entityType = metamodel.getEntityTypeByName( definition.getEntityName() ); + if ( entityType == null ) { + throw new IllegalArgumentException( + "Attempted to register named entity graph [" + definition.getRegisteredName() + + "] for unknown entity ["+ definition.getEntityName() + "]" + + ); + } final EntityGraphImpl entityGraph = new EntityGraphImpl( definition.getRegisteredName(), entityType, diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/graphs/named/basic/Person.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/graphs/named/basic/Person.java index b3e4b93b29..eafe5ec203 100644 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/graphs/named/basic/Person.java +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/graphs/named/basic/Person.java @@ -30,7 +30,7 @@ import javax.persistence.NamedEntityGraph; /** * @author Steve Ebersole */ -@Entity(name = "Person") +@Entity @NamedEntityGraph public class Person { @Id diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/graphs/named/multiple/NamedEntityGraphsTest.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/graphs/named/multiple/NamedEntityGraphsTest.java new file mode 100644 index 0000000000..8dd3f2d1d2 --- /dev/null +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/graphs/named/multiple/NamedEntityGraphsTest.java @@ -0,0 +1,50 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2013, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.jpa.test.graphs.named.multiple; + +import javax.persistence.EntityGraph; + +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; + +import org.junit.Test; + +import static junit.framework.Assert.assertNotNull; + +/** + * @author Steve Ebersole + */ +public class NamedEntityGraphsTest extends BaseEntityManagerFunctionalTestCase { + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Person.class }; + } + + @Test + public void testIt() { + EntityGraph graph = getOrCreateEntityManager().getEntityGraph( "abc" ); + assertNotNull( graph ); + graph = getOrCreateEntityManager().getEntityGraph( "xyz" ); + assertNotNull( graph ); + } +} diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/graphs/named/multiple/Person.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/graphs/named/multiple/Person.java new file mode 100644 index 0000000000..fb8b16c334 --- /dev/null +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/graphs/named/multiple/Person.java @@ -0,0 +1,42 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2013, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.jpa.test.graphs.named.multiple; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.NamedEntityGraph; +import javax.persistence.NamedEntityGraphs; + +/** + * @author Steve Ebersole + */ +@Entity(name = "Person") +@NamedEntityGraphs({ + @NamedEntityGraph( name = "abc" ), + @NamedEntityGraph( name = "xyz" ) +}) +public class Person { + @Id + public Long id; +}