From 948b14f86655c2dcf08eb02ef12717e2be0f4b27 Mon Sep 17 00:00:00 2001 From: Strong Liu Date: Thu, 17 Jan 2013 14:52:09 +0800 Subject: [PATCH] HHH-7759 named queries not read by hbm source --- .../metamodel/internal/MetadataImpl.java | 15 ++++++++ .../internal/source/hbm/BindHelper.java | 35 +++++++++++-------- .../parser/SQLQueryElementContentParser.java | 5 +-- .../NativeSqlAndQuotedIdentifiersTest.java | 3 -- .../org/hibernate/test/stats/StatsTest.java | 2 -- 5 files changed, 38 insertions(+), 22 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/MetadataImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/MetadataImpl.java index 50c5dbb36a..f7576640a3 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/MetadataImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/MetadataImpl.java @@ -32,6 +32,7 @@ import java.util.Map; import org.hibernate.AssertionFailure; import org.hibernate.DuplicateMappingException; +import org.hibernate.EntityMode; import org.hibernate.MappingException; import org.hibernate.SessionFactory; import org.hibernate.annotations.common.util.StringHelper; @@ -628,6 +629,20 @@ public class MetadataImpl implements MetadataImplementor, Serializable { throw new DuplicateMappingException( DuplicateMappingException.Type.ENTITY, entityName ); } entityBindingMap.put( entityName, entityBinding ); + final boolean isPOJO = entityBinding.getHierarchyDetails().getEntityMode() == EntityMode.POJO; + final String className = isPOJO ? entityBinding.getEntity().getClassName() : null; + if ( isPOJO && StringHelper.isEmpty( className ) ) { + throw new MappingException( "Entity[" + entityName + "] is mapped as pojo but don't have a class name" ); + } + if ( StringHelper.isEmpty( className ) || entityName.equals( className ) ) { + //ignore + } + else if ( entityBindingMap.containsKey( className ) ) { + throw new DuplicateMappingException( DuplicateMappingException.Type.ENTITY, entityName ); + } + else { + entityBindingMap.put( className, entityBinding ); + } } public PluralAttributeBinding getCollection(String collectionRole) { diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/BindHelper.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/BindHelper.java index 495067db56..56a04e8fea 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/BindHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/BindHelper.java @@ -62,6 +62,7 @@ import org.hibernate.metamodel.spi.binding.EntityBinding; import org.hibernate.metamodel.spi.binding.SingularAssociationAttributeBinding; import org.hibernate.metamodel.spi.binding.SingularAttributeBinding; import org.hibernate.metamodel.spi.source.BindingContext; +import org.hibernate.metamodel.spi.source.LocalBindingContext; import org.hibernate.metamodel.spi.source.MappingException; import org.hibernate.type.Type; @@ -82,7 +83,7 @@ public class BindHelper { } public static void bindNamedSQLQuery(final JaxbSqlQueryElement queryElement, - Origin origin, BindingContext bindingContext, + Origin origin, LocalBindingContext bindingContext, MetadataImplementor metadata) { final NamedSQLQueryDefinitionBuilder builder = new NamedSQLQueryDefinitionBuilder(); @@ -107,32 +108,36 @@ public class BindHelper { public static NativeSQLQueryRootReturn bindReturn( JaxbReturnElement returnElement, int elementCount, - Origin origin, MetadataImplementor metadata ) { + Origin origin, MetadataImplementor metadata, LocalBindingContext bindingContext) { final String alias = getAlias( returnElement, elementCount ); final String clazz = returnElement.getClazz(); final String entityName = returnElement.getEntityName(); if ( StringHelper.isEmpty( clazz ) && StringHelper.isEmpty( entityName ) ) { - throw new org.hibernate.MappingException( " must specify either a class or entity-name" ); + throw bindingContext.makeMappingException( + " must specify either a class or entity-name" + ); } final LockMode lockMode = Helper.interpretLockMode( - returnElement.getLockMode(), origin ); - EntityBinding entityBinding = null; - if ( StringHelper.isNotEmpty( entityName ) ) { - entityBinding = metadata.getEntityBinding( entityName ); + returnElement.getLockMode(), origin + ); + final EntityBinding entityBinding = metadata.getEntityBinding( + StringHelper.isNotEmpty( entityName ) ? entityName : bindingContext.qualifyClassName( clazz ) + ); + if ( entityBinding == null ) { + throw bindingContext.makeMappingException( "Can't locate entitybinding" ); } - if ( StringHelper.isNotEmpty( clazz ) ) { - // todo look up entitybinding by class name - } - return new NativeSQLQueryRootReturn( alias, entityName, + return new NativeSQLQueryRootReturn( + alias, entityBinding.getEntityName(), bindPropertyResults( alias, returnElement, entityBinding ), - lockMode ); + lockMode + ); } public static void bindResultSetMappingDefinitions( JaxbResultsetElement element, Origin origin, - BindingContext bindingContext, MetadataImplementor metadata) { + LocalBindingContext bindingContext, MetadataImplementor metadata) { final ResultSetMappingDefinition definition = new ResultSetMappingDefinition( element.getName() ); int cnt = 0; @@ -157,7 +162,7 @@ public class BindHelper { } for ( final JaxbReturnElement r : element.getReturn() ) { definition.addQueryReturn( bindReturn( - r, cnt++, origin, metadata ) ); + r, cnt++, origin, metadata, bindingContext ) ); } metadata.addResultSetMapping( definition ); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/parser/SQLQueryElementContentParser.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/parser/SQLQueryElementContentParser.java index 04e375b1cb..b1db9d85c5 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/parser/SQLQueryElementContentParser.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/parser/SQLQueryElementContentParser.java @@ -41,6 +41,7 @@ import org.hibernate.jaxb.spi.hbm.JaxbSynchronizeElement; import org.hibernate.metamodel.internal.source.hbm.BindHelper; import org.hibernate.metamodel.spi.MetadataImplementor; import org.hibernate.metamodel.spi.source.BindingContext; +import org.hibernate.metamodel.spi.source.LocalBindingContext; import org.hibernate.type.Type; /** @@ -87,7 +88,7 @@ public class SQLQueryElementContentParser extends AbstractQueryElementContentsPa public NamedSQLQueryDefinition buildQueryReturns( String name, NamedSQLQueryDefinitionBuilder builder, - Origin origin, BindingContext bindingContext, + Origin origin, LocalBindingContext bindingContext, MetadataImplementor metadata ) { final ResultSetMappingDefinition definition = new ResultSetMappingDefinition( name ); @@ -114,7 +115,7 @@ public class SQLQueryElementContentParser extends AbstractQueryElementContentsPa } for ( final JaxbReturnElement r : returnElements ) { definition.addQueryReturn( BindHelper.bindReturn( - r, cnt++, origin, metadata ) ); + r, cnt++, origin, metadata, bindingContext ) ); } diff --git a/hibernate-core/src/test/java/org/hibernate/test/sql/hand/quotedidentifiers/NativeSqlAndQuotedIdentifiersTest.java b/hibernate-core/src/test/java/org/hibernate/test/sql/hand/quotedidentifiers/NativeSqlAndQuotedIdentifiersTest.java index f288ee304d..6986690f4a 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/sql/hand/quotedidentifiers/NativeSqlAndQuotedIdentifiersTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/sql/hand/quotedidentifiers/NativeSqlAndQuotedIdentifiersTest.java @@ -29,7 +29,6 @@ import org.hibernate.SQLQuery; import org.hibernate.Session; import org.hibernate.dialect.Dialect; import org.hibernate.testing.DialectCheck; -import org.hibernate.testing.FailureExpectedWithNewMetamodel; import org.hibernate.testing.RequiresDialectFeature; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; @@ -93,7 +92,6 @@ public class NativeSqlAndQuotedIdentifiersTest extends BaseCoreFunctionalTestCas } @Test - @FailureExpectedWithNewMetamodel public void testBasicEntityMapping() { Session session = openSession(); session.beginTransaction(); @@ -105,7 +103,6 @@ public class NativeSqlAndQuotedIdentifiersTest extends BaseCoreFunctionalTestCas } @Test - @FailureExpectedWithNewMetamodel public void testExpandedEntityMapping() { Session session = openSession(); session.beginTransaction(); diff --git a/hibernate-core/src/test/java/org/hibernate/test/stats/StatsTest.java b/hibernate-core/src/test/java/org/hibernate/test/stats/StatsTest.java index 18aba5c7ae..f72e39fb89 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/stats/StatsTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/stats/StatsTest.java @@ -41,7 +41,6 @@ import org.hibernate.metamodel.spi.binding.PluralAttributeBinding; import org.hibernate.stat.QueryStatistics; import org.hibernate.stat.Statistics; import org.hibernate.test.util.SchemaUtil; -import org.hibernate.testing.FailureExpectedWithNewMetamodel; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import static org.junit.Assert.assertEquals; @@ -67,7 +66,6 @@ public class StatsTest extends BaseCoreFunctionalTestCase { @Test @SuppressWarnings( {"UnusedAssignment"}) -// @FailureExpectedWithNewMetamodel public void testCollectionFetchVsLoad() throws Exception { Statistics stats = sessionFactory().getStatistics(); stats.clear();