HHH-7759 named queries not read by hbm source

This commit is contained in:
Strong Liu 2013-01-17 14:52:09 +08:00
parent f2ad705fc0
commit 948b14f866
5 changed files with 38 additions and 22 deletions

View File

@ -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) {

View File

@ -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( "<return alias='" + alias
+ "'> must specify either a class or entity-name" );
throw bindingContext.makeMappingException(
"<return alias='" + alias
+ "'> 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 );

View File

@ -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 ) );
}

View File

@ -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();

View File

@ -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();