diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java index 77034482f6..1ce6542a96 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java @@ -209,6 +209,9 @@ public final class AnnotationBinder { public static void bindDefaults(Mappings mappings) { Map defaults = mappings.getReflectionManager().getDefaults(); + + // id generators ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + { List anns = ( List ) defaults.get( SequenceGenerator.class ); if ( anns != null ) { @@ -231,6 +234,9 @@ public final class AnnotationBinder { } } } + + // queries ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + { List anns = ( List ) defaults.get( NamedQuery.class ); if ( anns != null ) { @@ -247,6 +253,9 @@ public final class AnnotationBinder { } } } + + // result-set-mappings ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + { List anns = ( List ) defaults.get( SqlResultSetMapping.class ); if ( anns != null ) { @@ -256,6 +265,8 @@ public final class AnnotationBinder { } } + // stored procs ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + { final List annotations = (List) defaults.get( NamedStoredProcedureQuery.class ); @@ -265,7 +276,6 @@ public final class AnnotationBinder { } } } - { final List annotations = (List) defaults.get( NamedStoredProcedureQueries.class ); diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java b/hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java index ea3f4b8a2b..671b0bbb28 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java @@ -81,6 +81,8 @@ import org.hibernate.annotations.common.reflection.java.JavaReflectionManager; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl; +import org.hibernate.cache.spi.GeneralDataRegion; +import org.hibernate.cfg.annotations.NamedEntityGraphDefinition; import org.hibernate.cfg.annotations.NamedProcedureCallDefinition; import org.hibernate.cfg.annotations.reflection.JPAMetadataProvider; import org.hibernate.context.spi.CurrentTenantIdentifierResolver; @@ -217,6 +219,7 @@ public class Configuration implements Serializable { protected Map namedSqlQueries; protected Map namedProcedureCallMap; protected Map sqlResultSetMappings; + protected Map namedEntityGraphMap; protected Map typeDefs; protected Map filterDefinitions; @@ -299,6 +302,7 @@ public class Configuration implements Serializable { namedQueries = new HashMap(); namedSqlQueries = new HashMap(); sqlResultSetMappings = new HashMap(); + namedEntityGraphMap = new HashMap(); typeDefs = new HashMap(); filterDefinitions = new HashMap(); @@ -2619,6 +2623,12 @@ public class Configuration implements Serializable { } } + public java.util.Collection getNamedEntityGraphs() { + return namedEntityGraphMap == null + ? Collections.emptyList() + : namedEntityGraphMap.values(); + } + // Mappings impl ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -2886,6 +2896,16 @@ public class Configuration implements Serializable { } } + @Override + public void addNamedEntityGraphDefintion(NamedEntityGraphDefinition definition) + throws DuplicateMappingException { + final String name = definition.getRegisteredName(); + final NamedEntityGraphDefinition previous = namedEntityGraphMap.put( name, definition ); + if ( previous != null ) { + throw new DuplicateMappingException( "NamedEntityGraph", name ); + } + } + public void addDefaultSQLQuery(String name, NamedSQLQueryDefinition query) { applySQLQuery( name, query ); defaultNamedNativeQueryNames.add( name ); diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/Mappings.java b/hibernate-core/src/main/java/org/hibernate/cfg/Mappings.java index 4b49851563..6ea34e60b1 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/Mappings.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/Mappings.java @@ -37,6 +37,7 @@ import org.hibernate.MappingException; import org.hibernate.annotations.AnyMetaDef; import org.hibernate.annotations.common.reflection.ReflectionManager; import org.hibernate.annotations.common.reflection.XClass; +import org.hibernate.cfg.annotations.NamedEntityGraphDefinition; import org.hibernate.cfg.annotations.NamedProcedureCallDefinition; import org.hibernate.engine.ResultSetMappingDefinition; import org.hibernate.engine.spi.FilterDefinition; @@ -348,6 +349,15 @@ public interface Mappings { */ public void addNamedProcedureCallDefinition(NamedProcedureCallDefinition definition) throws DuplicateMappingException; + /** + * Adds metadata for a named entity graph to this repository + * + * @param namedEntityGraphDefinition The procedure call information + * + * @throws DuplicateMappingException If an entity graph already exists with that name. + */ + public void addNamedEntityGraphDefintion(NamedEntityGraphDefinition namedEntityGraphDefinition); + /** * Get the metadata for a named SQL result set mapping. * 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 defd4096f7..0a919319ca 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 @@ -35,6 +35,8 @@ import javax.persistence.Access; import javax.persistence.Entity; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; +import javax.persistence.NamedEntityGraph; +import javax.persistence.NamedEntityGraphs; import javax.persistence.PrimaryKeyJoinColumn; import javax.persistence.SecondaryTable; import javax.persistence.SecondaryTables; @@ -162,8 +164,28 @@ 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() ) ); + } + + @SuppressWarnings("SimplifiableConditionalExpression") private void bindHibernateAnnotation(org.hibernate.annotations.Entity hibAnn) { { 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 new file mode 100644 index 0000000000..4c10a940b1 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/NamedEntityGraphDefinition.java @@ -0,0 +1,59 @@ +/* + * 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.cfg.annotations; + +import javax.persistence.NamedEntityGraph; + +/** + * Models the definition of a {@link NamedEntityGraph} annotation + * + * @author Steve Ebersole + */ +public class NamedEntityGraphDefinition { + private final NamedEntityGraph annotation; + private final String jpaEntityName; + private final String entityName; + + public NamedEntityGraphDefinition(NamedEntityGraph annotation, String jpaEntityName, String entityName) { + this.annotation = annotation; + this.jpaEntityName = jpaEntityName; + this.entityName = entityName; + } + + public String getRegisteredName() { + return jpaEntityName; + } + + public String getJpaEntityName() { + return jpaEntityName; + } + + public String getEntityName() { + return entityName; + } + + public NamedEntityGraph getAnnotation() { + return annotation; + } +} diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/graph/internal/AbstractGraphNode.java b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/graph/internal/AbstractGraphNode.java index ff760bd2f1..e30c4f88fb 100644 --- a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/graph/internal/AbstractGraphNode.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/graph/internal/AbstractGraphNode.java @@ -162,42 +162,42 @@ public abstract class AbstractGraphNode implements GraphNodeImplementor { } @SuppressWarnings("unchecked") - public Subgraph addSubgraph(Attribute attribute) { + public SubgraphImpl addSubgraph(Attribute attribute) { return addAttribute( attribute ).makeSubgraph(); } @SuppressWarnings("unchecked") - public Subgraph addSubgraph(Attribute attribute, Class type) { + public SubgraphImpl addSubgraph(Attribute attribute, Class type) { return addAttribute( attribute ).makeSubgraph( type ); } @SuppressWarnings("unchecked") - public Subgraph addSubgraph(String attributeName) { + public SubgraphImpl addSubgraph(String attributeName) { return addAttribute( attributeName ).makeSubgraph(); } @SuppressWarnings("unchecked") - public Subgraph addSubgraph(String attributeName, Class type) { + public SubgraphImpl addSubgraph(String attributeName, Class type) { return addAttribute( attributeName ).makeSubgraph( type ); } @SuppressWarnings("unchecked") - public Subgraph addKeySubgraph(Attribute attribute) { + public SubgraphImpl addKeySubgraph(Attribute attribute) { return addAttribute( attribute ).makeKeySubgraph(); } @SuppressWarnings("unchecked") - public Subgraph addKeySubgraph(Attribute attribute, Class type) { + public SubgraphImpl addKeySubgraph(Attribute attribute, Class type) { return addAttribute( attribute ).makeKeySubgraph( type ); } @SuppressWarnings("unchecked") - public Subgraph addKeySubgraph(String attributeName) { + public SubgraphImpl addKeySubgraph(String attributeName) { return addAttribute( attributeName ).makeKeySubgraph(); } @SuppressWarnings("unchecked") - public Subgraph addKeySubgraph(String attributeName, Class type) { + public SubgraphImpl addKeySubgraph(String attributeName, Class type) { return addAttribute( attributeName ).makeKeySubgraph( type ); } } diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/graph/internal/AttributeNodeImpl.java b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/graph/internal/AttributeNodeImpl.java index 55584cb426..d6cb4b6e68 100644 --- a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/graph/internal/AttributeNodeImpl.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/graph/internal/AttributeNodeImpl.java @@ -108,16 +108,16 @@ public class AttributeNodeImpl implements AttributeNode, AttributeNodeImpl } @SuppressWarnings("unchecked") - public Subgraph makeSubgraph() { - return (Subgraph) internalMakeSubgraph( null ); + public SubgraphImpl makeSubgraph() { + return (SubgraphImpl) internalMakeSubgraph( null ); } @SuppressWarnings("unchecked") - public Subgraph makeSubgraph(Class type) { - return (Subgraph) internalMakeSubgraph( type ); + public SubgraphImpl makeSubgraph(Class type) { + return (SubgraphImpl) internalMakeSubgraph( type ); } - private Subgraph internalMakeSubgraph(Class type) { + private SubgraphImpl internalMakeSubgraph(Class type) { if ( attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.BASIC || attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.EMBEDDED ) { throw new IllegalArgumentException( @@ -193,12 +193,17 @@ public class AttributeNodeImpl implements AttributeNode, AttributeNodeImpl return type.isAssignableFrom( entityPersister.getMappedClass() ); } - public Subgraph makeKeySubgraph() { - return (SubgraphImpl) makeKeySubgraph( null ); + @SuppressWarnings("unchecked") + public SubgraphImpl makeKeySubgraph() { + return (SubgraphImpl) internalMakeKeySubgraph( null ); } @SuppressWarnings("unchecked") - public Subgraph makeKeySubgraph(Class type) { + public SubgraphImpl makeKeySubgraph(Class type) { + return (SubgraphImpl) internalMakeKeySubgraph( type ); + } + + public SubgraphImpl internalMakeKeySubgraph(Class type) { if ( ! attribute.isCollection() ) { throw new IllegalArgumentException( String.format( "Non-collection attribute [%s] cannot be target of key subgraph", getAttributeName() ) diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/graph/internal/EntityGraphImpl.java b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/graph/internal/EntityGraphImpl.java index 4189b9144d..c41ed06b65 100644 --- a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/graph/internal/EntityGraphImpl.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/graph/internal/EntityGraphImpl.java @@ -83,42 +83,42 @@ public class EntityGraphImpl extends AbstractGraphNode implements EntityGr } @Override - public Subgraph addSubgraph(Attribute attribute) { + public SubgraphImpl addSubgraph(Attribute attribute) { return super.addSubgraph( attribute ); } @Override - public Subgraph addSubgraph(Attribute attribute, Class type) { + public SubgraphImpl addSubgraph(Attribute attribute, Class type) { return super.addSubgraph( attribute, type ); } @Override - public Subgraph addSubgraph(String attributeName) { + public SubgraphImpl addSubgraph(String attributeName) { return super.addSubgraph( attributeName ); } @Override - public Subgraph addSubgraph(String attributeName, Class type) { + public SubgraphImpl addSubgraph(String attributeName, Class type) { return super.addSubgraph( attributeName, type ); } @Override - public Subgraph addKeySubgraph(Attribute attribute) { + public SubgraphImpl addKeySubgraph(Attribute attribute) { return super.addKeySubgraph( attribute ); } @Override - public Subgraph addKeySubgraph(Attribute attribute, Class type) { + public SubgraphImpl addKeySubgraph(Attribute attribute, Class type) { return super.addKeySubgraph( attribute, type ); } @Override - public Subgraph addKeySubgraph(String attributeName) { + public SubgraphImpl addKeySubgraph(String attributeName) { return super.addKeySubgraph( attributeName ); } @Override - public Subgraph addKeySubgraph(String attributeName, Class type) { + public SubgraphImpl addKeySubgraph(String attributeName, Class type) { return super.addKeySubgraph( attributeName, type ); } diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/graph/internal/SubgraphImpl.java b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/graph/internal/SubgraphImpl.java index 1f206ff32f..f52c01dba1 100644 --- a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/graph/internal/SubgraphImpl.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/graph/internal/SubgraphImpl.java @@ -68,42 +68,42 @@ public class SubgraphImpl extends AbstractGraphNode implements Subgraph } @Override - public Subgraph addSubgraph(Attribute attribute) { + public SubgraphImpl addSubgraph(Attribute attribute) { return super.addSubgraph( attribute ); } @Override - public Subgraph addSubgraph(Attribute attribute, Class type) { + public SubgraphImpl addSubgraph(Attribute attribute, Class type) { return super.addSubgraph( attribute, type ); } @Override - public Subgraph addSubgraph(String attributeName) { + public SubgraphImpl addSubgraph(String attributeName) { return super.addSubgraph( attributeName ); } @Override - public Subgraph addSubgraph(String attributeName, Class type) { + public SubgraphImpl addSubgraph(String attributeName, Class type) { return super.addSubgraph( attributeName, type ); } @Override - public Subgraph addKeySubgraph(Attribute attribute) { + public SubgraphImpl addKeySubgraph(Attribute attribute) { return super.addKeySubgraph( attribute ); } @Override - public Subgraph addKeySubgraph(Attribute attribute, Class type) { + public SubgraphImpl addKeySubgraph(Attribute attribute, Class type) { return super.addKeySubgraph( attribute, type ); } @Override - public Subgraph addKeySubgraph(String attributeName) { + public SubgraphImpl addKeySubgraph(String attributeName) { return super.addKeySubgraph( attributeName ); } @Override - public Subgraph addKeySubgraph(String attributeName, Class type) { + public SubgraphImpl addKeySubgraph(String attributeName, Class type) { return super.addKeySubgraph( attributeName, type ); } 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 07da96f48b..6d0d26fb1e 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 @@ -27,12 +27,16 @@ import javax.persistence.Cache; import javax.persistence.EntityGraph; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; +import javax.persistence.NamedAttributeNode; +import javax.persistence.NamedEntityGraph; +import javax.persistence.NamedSubgraph; import javax.persistence.PersistenceContextType; import javax.persistence.PersistenceException; import javax.persistence.PersistenceUnitUtil; import javax.persistence.Query; import javax.persistence.SynchronizationType; import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.metamodel.Attribute; import javax.persistence.metamodel.EntityType; import javax.persistence.metamodel.Metamodel; import javax.persistence.spi.LoadState; @@ -42,6 +46,7 @@ import java.io.InvalidObjectException; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; @@ -55,6 +60,7 @@ import org.hibernate.Hibernate; import org.hibernate.SessionFactory; import org.hibernate.cache.spi.RegionFactory; import org.hibernate.cfg.Configuration; +import org.hibernate.cfg.annotations.NamedEntityGraphDefinition; import org.hibernate.ejb.HibernateEntityManagerFactory; import org.hibernate.engine.spi.NamedQueryDefinition; import org.hibernate.engine.spi.NamedQueryDefinitionBuilder; @@ -64,12 +70,15 @@ import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.id.IdentifierGenerator; import org.hibernate.id.UUIDGenerator; import org.hibernate.internal.SessionFactoryImpl; +import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.config.ConfigurationHelper; import org.hibernate.jpa.AvailableSettings; import org.hibernate.jpa.HibernateQuery; import org.hibernate.jpa.boot.internal.SettingsImpl; import org.hibernate.jpa.criteria.CriteriaBuilderImpl; +import org.hibernate.jpa.graph.internal.AbstractGraphNode; import org.hibernate.jpa.graph.internal.EntityGraphImpl; +import org.hibernate.jpa.graph.internal.SubgraphImpl; import org.hibernate.jpa.internal.metamodel.EntityTypeImpl; import org.hibernate.jpa.internal.metamodel.MetamodelImpl; import org.hibernate.jpa.internal.util.PersistenceUtilHelper; @@ -161,7 +170,10 @@ public class EntityManagerFactoryImpl implements HibernateEntityManagerFactory { entityManagerFactoryName = (String) UUID_GENERATOR.generate(null, null); } this.entityManagerFactoryName = entityManagerFactoryName; - EntityManagerFactoryRegistry.INSTANCE.addEntityManagerFactory(entityManagerFactoryName, this); + + applyNamedEntityGraphs( cfg.getNamedEntityGraphs() ); + + EntityManagerFactoryRegistry.INSTANCE.addEntityManagerFactory( entityManagerFactoryName, this ); } private enum JpaMetaModelPopulationSetting { @@ -220,6 +232,68 @@ public class EntityManagerFactoryImpl implements HibernateEntityManagerFactory { } } + @SuppressWarnings("unchecked") + private void applyNamedEntityGraphs(Collection namedEntityGraphs) { + for ( NamedEntityGraphDefinition definition : namedEntityGraphs ) { + final EntityType entityType = metamodel.getEntityTypeByName( definition.getJpaEntityName() ); + final EntityGraphImpl entityGraph = new EntityGraphImpl( + definition.getRegisteredName(), + entityType, + this + ); + + final NamedEntityGraph namedEntityGraph = definition.getAnnotation(); + + if ( namedEntityGraph.includeAllAttributes() ) { + for ( Object attributeObject : entityType.getAttributes() ) { + entityGraph.addAttributeNodes( (Attribute) attributeObject ); + } + } + + if ( namedEntityGraph.attributeNodes() != null ) { + applyNamedAttributeNodes( namedEntityGraph.attributeNodes(), namedEntityGraph, entityGraph ); + } + + entityGraphs.put( definition.getRegisteredName(), entityGraph ); + } + } + + private void applyNamedAttributeNodes( + NamedAttributeNode[] namedAttributeNodes, + NamedEntityGraph namedEntityGraph, + AbstractGraphNode graphNode) { + for ( NamedAttributeNode namedAttributeNode : namedAttributeNodes ) { + if ( StringHelper.isNotEmpty( namedAttributeNode.subgraph() ) ) { + final SubgraphImpl subgraph = graphNode.addSubgraph( namedAttributeNode.value() ); + applyNamedSubgraphs( + namedEntityGraph, + namedAttributeNode.subgraph(), + subgraph + ); + } + if ( StringHelper.isNotEmpty( namedAttributeNode.keySubgraph() ) ) { + final SubgraphImpl subgraph = graphNode.addKeySubgraph( namedAttributeNode.value() ); + applyNamedSubgraphs( + namedEntityGraph, + namedAttributeNode.keySubgraph(), + subgraph + ); + } + } + } + + private void applyNamedSubgraphs(NamedEntityGraph namedEntityGraph, String subgraphName, SubgraphImpl subgraph) { + for ( NamedSubgraph namedSubgraph : namedEntityGraph.subgraphs() ) { + if ( subgraphName.equals( namedSubgraph.name() ) ) { + applyNamedAttributeNodes( + namedSubgraph.attributeNodes(), + namedEntityGraph, + subgraph + ); + } + } + } + public EntityManager createEntityManager() { return createEntityManager( Collections.EMPTY_MAP ); } diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/graphs/named/basic/BasicNamedEntityGraphTest.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/graphs/named/basic/BasicNamedEntityGraphTest.java new file mode 100644 index 0000000000..b6109b4a3a --- /dev/null +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/graphs/named/basic/BasicNamedEntityGraphTest.java @@ -0,0 +1,48 @@ +/* + * 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.basic; + +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 BasicNamedEntityGraphTest extends BaseEntityManagerFunctionalTestCase { + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Person.class }; + } + + @Test + public void testIt() { + EntityGraph graph = getOrCreateEntityManager().getEntityGraph( "Person" ); + assertNotNull( graph ); + } +} 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 new file mode 100644 index 0000000000..dc9a55bf35 --- /dev/null +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/graphs/named/basic/Person.java @@ -0,0 +1,38 @@ +/* + * 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.basic; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.NamedEntityGraph; + +/** + * @author Steve Ebersole + */ +@Entity(name = "Person") +@NamedEntityGraph() +public class Person { + @Id + private Long id; +}