HHH-10664 - Prep 6.0 feature branch - merge hibernate-entitymanager into hibernate-core (fix test failures)

This commit is contained in:
Steve Ebersole 2016-05-02 18:00:42 -05:00
parent b67b5866ac
commit 1c1783e90f
9 changed files with 173 additions and 16 deletions

View File

@ -45,7 +45,6 @@ import org.hibernate.engine.jdbc.spi.SqlExceptionHelper;
import org.hibernate.engine.profile.FetchProfile;
import org.hibernate.engine.query.spi.QueryPlanCache;
import org.hibernate.exception.spi.SQLExceptionConverter;
import org.hibernate.graph.spi.EntityGraphImplementor;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.factory.IdentifierGeneratorFactory;
import org.hibernate.metadata.ClassMetadata;
@ -265,7 +264,7 @@ public class SessionFactoryDelegatingImpl implements SessionFactoryImplementor,
}
@Override
public EntityGraphImplementor findEntityGraphByName(String name) {
public EntityGraph findEntityGraphByName(String name) {
return delegate.findEntityGraphByName( name );
}

View File

@ -9,6 +9,7 @@ package org.hibernate.engine.spi;
import java.io.Serializable;
import java.util.Map;
import java.util.Set;
import javax.persistence.EntityGraph;
import org.hibernate.CustomEntityDirtinessStrategy;
import org.hibernate.EntityNameResolver;
@ -37,7 +38,6 @@ import org.hibernate.engine.jdbc.spi.SqlExceptionHelper;
import org.hibernate.engine.profile.FetchProfile;
import org.hibernate.engine.query.spi.QueryPlanCache;
import org.hibernate.exception.spi.SQLExceptionConverter;
import org.hibernate.graph.spi.EntityGraphImplementor;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.metamodel.spi.MetamodelImplementor;
import org.hibernate.persister.collection.CollectionPersister;
@ -407,7 +407,7 @@ public interface SessionFactoryImplementor extends Mapping, SessionFactory, Quer
return getMetamodel().getImportedClassName( name );
}
EntityGraphImplementor findEntityGraphByName(String name);
EntityGraph findEntityGraphByName(String name);
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -7,11 +7,16 @@
package org.hibernate.graph.spi;
import javax.persistence.EntityGraph;
import javax.persistence.metamodel.EntityType;
/**
* @author Steve Ebersole
*/
public interface EntityGraphImplementor<T> extends EntityGraph<T>, GraphNodeImplementor {
boolean appliesTo(String entityName);
boolean appliesTo(EntityType<? super T> entityType);
/**
* Make a mutable copy of this entity graph
*

View File

@ -86,7 +86,6 @@ import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform;
import org.hibernate.event.service.spi.EventListenerGroup;
import org.hibernate.event.service.spi.EventListenerRegistry;
import org.hibernate.event.spi.EventType;
import org.hibernate.graph.spi.EntityGraphImplementor;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.UUIDGenerator;
import org.hibernate.id.factory.IdentifierGeneratorFactory;
@ -533,7 +532,7 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor {
@Override
public <T> List<EntityGraph<? super T>> findEntityGraphsByType(Class<T> entityClass) {
return null;
return getMetamodel().findEntityGraphsByType( entityClass );
}
@ -619,8 +618,8 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor {
}
@Override
public EntityGraphImplementor findEntityGraphByName(String name) {
return null;
public EntityGraph findEntityGraphByName(String name) {
return getMetamodel().findEntityGraphByName( name );
}
@Override
@ -885,7 +884,7 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor {
@Override
public <T> void addNamedEntityGraph(String graphName, EntityGraph<T> entityGraph) {
getMetamodel().addNamedEntityGraph( graphName, entityGraph );
}
public boolean isClosed() {

View File

@ -3710,18 +3710,24 @@ public final class SessionImpl
@Override
public EntityGraph<?> createEntityGraph(String graphName) {
checkOpen();
final EntityGraphImplementor named = getEntityManagerFactory().findEntityGraphByName( graphName );
final EntityGraph named = getEntityManagerFactory().findEntityGraphByName( graphName );
if ( named == null ) {
return null;
}
return named.makeMutableCopy();
if ( EntityGraphImplementor.class.isInstance( named ) ) {
return ( (EntityGraphImplementor) named ).makeMutableCopy();
}
else {
return named;
}
}
@Override
@SuppressWarnings("unchecked")
public EntityGraph<?> getEntityGraph(String graphName) {
checkOpen();
final EntityGraphImplementor named = getEntityManagerFactory().findEntityGraphByName( graphName );
final EntityGraph named = getEntityManagerFactory().findEntityGraphByName( graphName );
if ( named == null ) {
throw new IllegalArgumentException( "Could not locate EntityGraph with given name : " + graphName );
}

View File

@ -9,11 +9,17 @@ package org.hibernate.metamodel.internal;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import javax.persistence.EntityGraph;
import javax.persistence.NamedAttributeNode;
import javax.persistence.NamedEntityGraph;
import javax.persistence.NamedSubgraph;
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.EmbeddableType;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.ManagedType;
@ -29,11 +35,18 @@ import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy;
import org.hibernate.cache.spi.access.EntityRegionAccessStrategy;
import org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy;
import org.hibernate.cfg.annotations.NamedEntityGraphDefinition;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.graph.spi.EntityGraphImplementor;
import org.hibernate.internal.EntityManagerMessageLogger;
import org.hibernate.internal.HEMLogging;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.jpa.graph.internal.AbstractGraphNode;
import org.hibernate.jpa.graph.internal.AttributeNodeImpl;
import org.hibernate.jpa.graph.internal.EntityGraphImpl;
import org.hibernate.jpa.graph.internal.SubgraphImpl;
import org.hibernate.mapping.Collection;
import org.hibernate.mapping.MappedSuperclass;
import org.hibernate.mapping.PersistentClass;
@ -73,6 +86,8 @@ public class MetamodelImpl implements MetamodelImplementor, Serializable {
private final Map<Class<?>, MappedSuperclassType<?>> jpaMappedSuperclassTypeMap = new ConcurrentHashMap<>();
private final Map<String, EntityTypeImpl<?>> jpaEntityTypesByEntityName = new ConcurrentHashMap<>();
private final transient Map<String,EntityGraph> entityGraphMap = new ConcurrentHashMap<>();
public MetamodelImpl(SessionFactoryImplementor sessionFactory) {
this.sessionFactory = sessionFactory;
}
@ -207,6 +222,87 @@ public class MetamodelImpl implements MetamodelImplementor, Serializable {
this.jpaEmbeddableTypeMap.putAll( context.getEmbeddableTypeMap() );
this.jpaMappedSuperclassTypeMap.putAll( context.getMappedSuperclassTypeMap() );
this.jpaEntityTypesByEntityName.putAll( context.getEntityTypesByEntityName() );
applyNamedEntityGraphs( mappingMetadata.getNamedEntityGraphs().values() );
}
@SuppressWarnings("unchecked")
private void applyNamedEntityGraphs(java.util.Collection<NamedEntityGraphDefinition> namedEntityGraphs) {
for ( NamedEntityGraphDefinition definition : namedEntityGraphs ) {
log.debugf(
"Applying named entity graph [name=%s, entity-name=%s, jpa-entity-name=%s",
definition.getRegisteredName(),
definition.getEntityName(),
definition.getJpaEntityName()
);
final EntityType entityType = entity( 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,
this.getSessionFactory()
);
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 );
}
entityGraphMap.put( definition.getRegisteredName(), entityGraph );
}
}
private void applyNamedAttributeNodes(
NamedAttributeNode[] namedAttributeNodes,
NamedEntityGraph namedEntityGraph,
AbstractGraphNode graphNode) {
for ( NamedAttributeNode namedAttributeNode : namedAttributeNodes ) {
final String value = namedAttributeNode.value();
AttributeNodeImpl attributeNode = graphNode.addAttribute( value );
if ( StringHelper.isNotEmpty( namedAttributeNode.subgraph() ) ) {
final SubgraphImpl subgraph = attributeNode.makeSubgraph();
applyNamedSubgraphs(
namedEntityGraph,
namedAttributeNode.subgraph(),
subgraph
);
}
if ( StringHelper.isNotEmpty( namedAttributeNode.keySubgraph() ) ) {
final SubgraphImpl subgraph = attributeNode.makeKeySubgraph();
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
);
}
}
}
@Override
@ -566,6 +662,47 @@ public class MetamodelImpl implements MetamodelImplementor, Serializable {
return ArrayHelper.toStringArray( entityPersisterMap.keySet() );
}
@Override
public <T> void addNamedEntityGraph(String graphName, EntityGraph<T> entityGraph) {
if ( entityGraph instanceof EntityGraphImplementor ) {
entityGraph = ( (EntityGraphImplementor<T>) entityGraph ).makeImmutableCopy( graphName );
}
final EntityGraph old = entityGraphMap.put( graphName, entityGraph );
if ( old != null ) {
log.debugf( "EntityGraph being replaced on EntityManagerFactory for name %s", graphName );
}
}
@Override
@SuppressWarnings("unchecked")
public <T> EntityGraph<T> findEntityGraphByName(String name) {
return entityGraphMap.get( name );
}
@Override
@SuppressWarnings("unchecked")
public <T> List<EntityGraph<? super T>> findEntityGraphsByType(Class<T> entityClass) {
final EntityType<T> entityType = entity( entityClass );
if ( entityType == null ) {
throw new IllegalArgumentException( "Given class is not an entity : " + entityClass.getName() );
}
final List<EntityGraph<? super T>> results = new ArrayList<>();
for ( EntityGraph entityGraph : entityGraphMap.values() ) {
if ( !EntityGraphImplementor.class.isInstance( entityGraph ) ) {
continue;
}
final EntityGraphImplementor egi = (EntityGraphImplementor) entityGraph;
if ( egi.appliesTo( entityType ) ) {
results.add( egi );
}
}
return results;
}
@Override
public void close() {
// anything to do ?

View File

@ -7,8 +7,10 @@
package org.hibernate.metamodel.spi;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.persistence.EntityGraph;
import org.hibernate.EntityNameResolver;
import org.hibernate.MappingException;
@ -121,5 +123,11 @@ public interface MetamodelImplementor extends Metamodel {
*/
String[] getAllCollectionRoles();
<T> void addNamedEntityGraph(String graphName, EntityGraph<T> entityGraph);
<T> EntityGraph<T> findEntityGraphByName(String name);
<T> List<EntityGraph<? super T>> findEntityGraphsByType(Class<T> entityClass);
void close();
}

View File

@ -11,6 +11,8 @@ import org.hibernate.testing.TestForIssue;
import org.junit.Assert;
import org.junit.Test;
import static org.junit.Assert.assertNotNull;
public class SubgraphOrmNamedEntityGraphTest extends BaseEntityManagerFunctionalTestCase {
@Override
@ -23,16 +25,17 @@ public class SubgraphOrmNamedEntityGraphTest extends BaseEntityManagerFunctional
public void testSubgraphsAreLoadededFromOrmXml() throws Exception {
EntityManager entityManager = getOrCreateEntityManager();
List<EntityGraph<? super Book>> lneg = entityManager.getEntityGraphs( Book.class );
assertNotNull( lneg );
Assert.assertEquals(2, lneg.size());
for (EntityGraph<? super Book> neg : lneg){
if (neg.getName().equalsIgnoreCase( "full" )){
Assert.assertNotNull( neg.getAttributeNodes() );
assertNotNull( neg.getAttributeNodes() );
for (AttributeNode<?> n : neg.getAttributeNodes()){
if (n.getAttributeName().equalsIgnoreCase( "authors" )) {
Assert.assertEquals(1, n.getSubgraphs().size());
java.util.List<javax.persistence.AttributeNode<?>> attributeNodes = n.getSubgraphs().get(Author.class).getAttributeNodes();
Assert.assertNotNull("Subgraph attributes missing", attributeNodes);
assertNotNull("Subgraph attributes missing", attributeNodes);
Assert.assertEquals("Subgraph wrong number of attributes ", 3, attributeNodes.size());
}
}

View File

@ -101,7 +101,7 @@ public class AddNamedQueryTest extends BaseEntityManagerFunctionalTestCase {
// then lets create a query by name and check its setting
q = em.createNamedQuery( name );
assertEquals( FlushMode.COMMIT, q.unwrap( org.hibernate.Query.class ).getFlushMode() );
assertEquals( FlushMode.COMMIT, q.unwrap( org.hibernate.Query.class ).getHibernateFlushMode() );
assertEquals( FlushModeType.COMMIT, q.getFlushMode() );
em.getTransaction().commit();