From ef168c1f5dd16112da2b9efdb9ea70d8386b63cf Mon Sep 17 00:00:00 2001 From: Strong Liu Date: Fri, 13 Jul 2012 17:01:47 +0800 Subject: [PATCH] HHH-7448 hbm mapping xsd improve --- .../internal/SessionFactoryImpl.java | 11 +- .../jaxb/mapping/hbm/CustomSqlElement.java | 33 -- .../jaxb/mapping/hbm/EntityElement.java | 12 +- .../mapping/hbm/PluralAttributeElement.java | 10 +- .../source/hbm/AbstractEntitySourceImpl.java | 6 +- .../AbstractPluralAttributeSourceImpl.java | 2 +- .../internal/source/hbm/ColumnSourceImpl.java | 6 +- .../metamodel/internal/source/hbm/Helper.java | 61 ++-- .../source/hbm/HibernateMappingProcessor.java | 306 +++++++++++++++- .../hbm/ManyToOneAttributeSourceImpl.java | 2 +- .../hbm/PluralAttributeKeySourceImpl.java | 4 +- .../hbm/PropertyAttributeSourceImpl.java | 14 +- .../source/hbm/RootEntitySourceImpl.java | 2 +- .../hbm/VersionAttributeSourceImpl.java | 4 +- .../org/hibernate/hibernate-mapping-4.0.xsd | 326 +++++++----------- .../src/main/xjb/hbm-mapping-bindings.xjb | 47 +-- 16 files changed, 504 insertions(+), 342 deletions(-) delete mode 100644 hibernate-core/src/main/java/org/hibernate/internal/jaxb/mapping/hbm/CustomSqlElement.java diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java index 7e6e65f35f..8c5efb72f8 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java @@ -1216,26 +1216,27 @@ public final class SessionFactoryImpl null ); } - + @Override public NamedQueryDefinition getNamedQuery(String queryName) { return namedQueries.get( queryName ); } - + @Override public NamedSQLQueryDefinition getNamedSQLQuery(String queryName) { return namedSqlQueries.get( queryName ); } - + @Override public ResultSetMappingDefinition getResultSetMapping(String resultSetName) { return sqlResultSetMappings.get( resultSetName ); } - + @Override public Type getIdentifierType(String className) throws MappingException { return getEntityPersister(className).getIdentifierType(); } + @Override public String getIdentifierPropertyName(String className) throws MappingException { return getEntityPersister(className).getIdentifierPropertyName(); } - + @Override public Type[] getReturnTypes(String queryString) throws HibernateException { return queryPlanCache.getHQLQueryPlan( queryString, false, Collections.EMPTY_MAP ) .getReturnMetadata() diff --git a/hibernate-core/src/main/java/org/hibernate/internal/jaxb/mapping/hbm/CustomSqlElement.java b/hibernate-core/src/main/java/org/hibernate/internal/jaxb/mapping/hbm/CustomSqlElement.java deleted file mode 100644 index 5213c84d36..0000000000 --- a/hibernate-core/src/main/java/org/hibernate/internal/jaxb/mapping/hbm/CustomSqlElement.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * Copyright (c) 2011, 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.internal.jaxb.mapping.hbm; - -/** - * @author Steve Ebersole - */ -public interface CustomSqlElement { - public String getValue(); - public boolean isCallable(); - public JaxbCheckAttribute getCheck(); -} diff --git a/hibernate-core/src/main/java/org/hibernate/internal/jaxb/mapping/hbm/EntityElement.java b/hibernate-core/src/main/java/org/hibernate/internal/jaxb/mapping/hbm/EntityElement.java index a0280bc69c..0335c14ec6 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/jaxb/mapping/hbm/EntityElement.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/jaxb/mapping/hbm/EntityElement.java @@ -32,10 +32,10 @@ public interface EntityElement extends MetaAttributeContainer { public String getName(); public String getEntityName(); - public Boolean isAbstract(); - public Boolean isLazy(); + public boolean isAbstract(); + public boolean isLazy(); public String getProxy(); - public String getBatchSize(); + public int getBatchSize(); public boolean isDynamicInsert(); public boolean isDynamicUpdate(); public boolean isSelectBeforeUpdate(); @@ -44,9 +44,9 @@ public interface EntityElement extends MetaAttributeContainer { public String getPersister(); public JaxbLoaderElement getLoader(); - public JaxbSqlInsertElement getSqlInsert(); - public JaxbSqlUpdateElement getSqlUpdate(); - public JaxbSqlDeleteElement getSqlDelete(); + public JaxbSqlDmlElement getSqlInsert(); + public JaxbSqlDmlElement getSqlUpdate(); + public JaxbSqlDmlElement getSqlDelete(); public List getSynchronize(); diff --git a/hibernate-core/src/main/java/org/hibernate/internal/jaxb/mapping/hbm/PluralAttributeElement.java b/hibernate-core/src/main/java/org/hibernate/internal/jaxb/mapping/hbm/PluralAttributeElement.java index c6f05dfe64..5eababb087 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/jaxb/mapping/hbm/PluralAttributeElement.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/jaxb/mapping/hbm/PluralAttributeElement.java @@ -47,10 +47,10 @@ public interface PluralAttributeElement extends TableInformationSource, MetaAttr public String getWhere(); public JaxbLoaderElement getLoader(); - public JaxbSqlInsertElement getSqlInsert(); - public JaxbSqlUpdateElement getSqlUpdate(); - public JaxbSqlDeleteElement getSqlDelete(); - public JaxbSqlDeleteAllElement getSqlDeleteAll(); + public JaxbSqlDmlElement getSqlInsert(); + public JaxbSqlDmlElement getSqlUpdate(); + public JaxbSqlDmlElement getSqlDelete(); + public JaxbSqlDmlElement getSqlDeleteAll(); public List getSynchronize(); @@ -62,7 +62,7 @@ public interface PluralAttributeElement extends TableInformationSource, MetaAttr public JaxbLazyAttributeWithExtra getLazy(); public JaxbOuterJoinAttribute getOuterJoin(); - public String getBatchSize(); + public int getBatchSize(); public boolean isInverse(); public boolean isMutable(); public boolean isOptimisticLock(); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/AbstractEntitySourceImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/AbstractEntitySourceImpl.java index 0f2cdea4ee..149d9449ec 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/AbstractEntitySourceImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/AbstractEntitySourceImpl.java @@ -252,12 +252,12 @@ public abstract class AbstractEntitySourceImpl @Override public boolean isAbstract() { - return Helper.getBooleanValue( entityElement.isAbstract(), false ); + return entityElement().isAbstract(); } @Override public boolean isLazy() { - return Helper.getBooleanValue( entityElement.isAbstract(), true ); + return entityElement().isLazy(); } @Override @@ -267,7 +267,7 @@ public abstract class AbstractEntitySourceImpl @Override public int getBatchSize() { - return Helper.getIntValue( entityElement.getBatchSize(), -1 ); + return entityElement.getBatchSize(); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/AbstractPluralAttributeSourceImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/AbstractPluralAttributeSourceImpl.java index 0c5855632e..8ee635f2b4 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/AbstractPluralAttributeSourceImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/AbstractPluralAttributeSourceImpl.java @@ -300,7 +300,7 @@ public abstract class AbstractPluralAttributeSourceImpl final String outerJoinSelection = pluralAttributeElement.getOuterJoin() != null ? pluralAttributeElement.getOuterJoin().value() : null; - final int batchSize = Helper.getIntValue( pluralAttributeElement.getBatchSize(), -1 ); + final int batchSize = pluralAttributeElement.getBatchSize(); if ( fetchSelection == null ) { if ( outerJoinSelection == null ) { diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/ColumnSourceImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/ColumnSourceImpl.java index efc349faa9..0258f07126 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/ColumnSourceImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/ColumnSourceImpl.java @@ -98,9 +98,9 @@ class ColumnSourceImpl @Override public Size getSize() { return new Size( - Helper.getIntValue( columnElement.getPrecision(), -1 ), - Helper.getIntValue( columnElement.getScale(), -1 ), - Helper.getLongValue( columnElement.getLength(), -1 ), + Helper.getValue( columnElement.getPrecision(), -1 ), + Helper.getValue( columnElement.getScale(), -1 ), + Helper.getValue( columnElement.getLength(), -1 ), Size.LobMultiplier.NONE ); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/Helper.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/Helper.java index 189d320a80..dca2f007ee 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/Helper.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/Helper.java @@ -30,18 +30,21 @@ import java.util.List; import java.util.Map; import java.util.Set; +import org.hibernate.LockMode; import org.hibernate.TruthValue; import org.hibernate.cache.spi.access.AccessType; import org.hibernate.engine.spi.CascadeStyle; import org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle; -import org.hibernate.internal.jaxb.mapping.hbm.CustomSqlElement; +import org.hibernate.internal.jaxb.Origin; import org.hibernate.internal.jaxb.mapping.hbm.EntityElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbCacheElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbColumnElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbJoinedSubclassElement; +import org.hibernate.internal.jaxb.mapping.hbm.JaxbLockModeAttribute; import org.hibernate.internal.jaxb.mapping.hbm.JaxbMetaElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbNaturalIdCacheElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbParamElement; +import org.hibernate.internal.jaxb.mapping.hbm.JaxbSqlDmlElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbSubclassElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbUnionSubclassElement; import org.hibernate.internal.jaxb.mapping.hbm.TableInformationSource; @@ -80,6 +83,36 @@ public class Helper { } }; + public static LockMode interpretLockMode(JaxbLockModeAttribute lockModeAttribute, Origin origin){ + if(lockModeAttribute==null)return LockMode.READ; + switch ( lockModeAttribute ) { + case NONE: + return LockMode.NONE; + case UPGRADE: + return LockMode.UPGRADE; + case UPGRADE_NOWAIT: + return LockMode.UPGRADE_NOWAIT; + case READ: + return LockMode.READ; + case WRITE: + return LockMode.WRITE; + case FORCE: + return LockMode.FORCE; + case OPTIMISTIC: + return LockMode.OPTIMISTIC; + case OPTIMISTIC_FORCE_INCREMENT: + return LockMode.OPTIMISTIC_FORCE_INCREMENT; + case PESSIMISTIC_READ: + return LockMode.PESSIMISTIC_READ; + case PESSIMISTIC_WRITE: + return LockMode.PESSIMISTIC_WRITE; + case PESSIMISTIC_FORCE_INCREMENT: + return LockMode.PESSIMISTIC_FORCE_INCREMENT; + default: + throw new MappingException( "unknown lock mode: "+lockModeAttribute, origin ); + } + } + public static InheritanceType interpretInheritanceType(EntityElement entityElement) { if ( JaxbSubclassElement.class.isInstance( entityElement ) ) { return InheritanceType.SINGLE_TABLE; @@ -102,7 +135,7 @@ public class Helper { * * @return The {@link CustomSQL} representation */ - public static CustomSQL buildCustomSql(CustomSqlElement customSqlElement) { + public static CustomSQL buildCustomSql(JaxbSqlDmlElement customSqlElement) { if ( customSqlElement == null ) { return null; } @@ -148,12 +181,8 @@ public class Helper { } final String region; if ( StringHelper.isEmpty( cacheElement.getRegion() ) ) { - if ( entityCache != null ) { - region = entityCache.getRegion() + NATURAL_ID_CACHE_SUFFIX; - } - else { - region = entityName + NATURAL_ID_CACHE_SUFFIX; - } + String temp = entityCache != null ? entityCache.getRegion() : entityName; + region = temp + NATURAL_ID_CACHE_SUFFIX; } else { region = cacheElement.getRegion(); @@ -183,7 +212,7 @@ public class Helper { } public static String getPropertyAccessorName(String access, boolean isEmbedded, String defaultAccess) { - return getStringValue( access, isEmbedded ? "embedded" : defaultAccess ); + return getValue( access, isEmbedded ? "embedded" : defaultAccess ); } public static MetaAttributeContext extractMetaAttributeContext( @@ -210,19 +239,7 @@ public class Helper { return subContext; } - public static String getStringValue(String value, String defaultValue) { - return value == null ? defaultValue : value; - } - - public static int getIntValue(String value, int defaultValue) { - return value == null ? defaultValue : Integer.parseInt( value ); - } - - public static long getLongValue(String value, long defaultValue) { - return value == null ? defaultValue : Long.parseLong( value ); - } - - public static boolean getBooleanValue(Boolean value, boolean defaultValue) { + public static T getValue(T value, T defaultValue){ return value == null ? defaultValue : value; } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/HibernateMappingProcessor.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/HibernateMappingProcessor.java index 151205a32f..5a00612cde 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/HibernateMappingProcessor.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/HibernateMappingProcessor.java @@ -23,23 +23,47 @@ */ package org.hibernate.metamodel.internal.source.hbm; +import java.io.Serializable; import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; +import org.hibernate.CacheMode; +import org.hibernate.FlushMode; +import org.hibernate.LockMode; +import org.hibernate.bytecode.buildtime.spi.Logger; +import org.hibernate.cfg.HbmBinder; import org.hibernate.engine.ResultSetMappingDefinition; +import org.hibernate.engine.query.spi.sql.NativeSQLQueryJoinReturn; +import org.hibernate.engine.query.spi.sql.NativeSQLQueryReturn; +import org.hibernate.engine.query.spi.sql.NativeSQLQueryRootReturn; +import org.hibernate.engine.query.spi.sql.NativeSQLQueryScalarReturn; +import org.hibernate.engine.spi.NamedQueryDefinition; +import org.hibernate.engine.spi.NamedSQLQueryDefinition; +import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.jaxb.Origin; import org.hibernate.internal.jaxb.mapping.hbm.EntityElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbFetchProfileElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbHibernateMapping; import org.hibernate.internal.jaxb.mapping.hbm.JaxbHibernateMapping.JaxbImport; +import org.hibernate.internal.jaxb.mapping.hbm.JaxbLoadCollectionElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbQueryElement; +import org.hibernate.internal.jaxb.mapping.hbm.JaxbQueryParamElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbResultsetElement; +import org.hibernate.internal.jaxb.mapping.hbm.JaxbReturnElement; +import org.hibernate.internal.jaxb.mapping.hbm.JaxbReturnJoinElement; +import org.hibernate.internal.jaxb.mapping.hbm.JaxbReturnScalarElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbSqlQueryElement; +import org.hibernate.internal.jaxb.mapping.hbm.JaxbSynchronizeElement; import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.Value; import org.hibernate.internal.util.collections.CollectionHelper; +import org.hibernate.mapping.PersistentClass; +import org.hibernate.metamodel.spi.binding.EntityBinding; import org.hibernate.metamodel.spi.binding.FetchProfile; import org.hibernate.metamodel.spi.relational.AuxiliaryDatabaseObject; import org.hibernate.metamodel.spi.relational.BasicAuxiliaryDatabaseObjectImpl; @@ -49,6 +73,7 @@ import org.hibernate.metamodel.spi.source.MetadataImplementor; import org.hibernate.metamodel.spi.source.TypeDescriptorSource; import org.hibernate.service.classloading.spi.ClassLoaderService; import org.hibernate.service.classloading.spi.ClassLoadingException; +import org.hibernate.type.Type; /** * Responsible for processing a {@code } element. Allows processing to be coordinated across @@ -56,8 +81,13 @@ import org.hibernate.service.classloading.spi.ClassLoadingException; * {@link org.hibernate.metamodel.spi.MetadataSourceProcessor} * * @author Steve Ebersole + * @author Strong Liu */ public class HibernateMappingProcessor { + private static final CoreMessageLogger LOG = org.jboss + .logging + .Logger + .getMessageLogger( CoreMessageLogger.class, HibernateMappingProcessor.class.getName() ); private final MetadataImplementor metadata; private final MappingDocument mappingDocument; @@ -241,21 +271,141 @@ public class HibernateMappingProcessor { } private void bindResultSetMappingDefinitions(JaxbResultsetElement element) { - ResultSetMappingDefinition definition = new ResultSetMappingDefinition( element.getName() ); + final ResultSetMappingDefinition definition = new ResultSetMappingDefinition( element.getName() ); + final List returns = element.getReturnScalarOrReturnOrReturnJoin(); + int cnt=0; + for ( final Object obj : returns ) { + cnt++; + final NativeSQLQueryReturn nativeSQLQueryReturn; + if ( JaxbReturnScalarElement.class.isInstance( obj ) ) { + JaxbReturnScalarElement scalarElement = JaxbReturnScalarElement.class.cast( obj ); + String column = scalarElement.getColumn(); + String typeFromXML = scalarElement.getType(); + Type type = metadata.getTypeResolver().heuristicType( typeFromXML ); + nativeSQLQueryReturn = new NativeSQLQueryScalarReturn( column, type ); + } + else if ( JaxbReturnJoinElement.class.isInstance( obj ) ) { + nativeSQLQueryReturn = bindReturnJoin(JaxbReturnJoinElement.class.cast( obj ), cnt); + + } + else if ( JaxbLoadCollectionElement.class.isInstance( obj ) ) { + nativeSQLQueryReturn = bindLoadCollection(JaxbLoadCollectionElement.class.cast( obj ), cnt); + } + else if ( JaxbReturnElement.class.isInstance( obj ) ) { + nativeSQLQueryReturn= bindReturn(JaxbReturnElement.class.cast( obj ), cnt); + + }else { + throw new MappingException( "unknown type of Result set mapping return: "+obj.getClass().getName() , origin()); + } + definition.addQueryReturn( nativeSQLQueryReturn ); + } metadata.addResultSetMapping( definition ); + + } + + private NativeSQLQueryReturn bindLoadCollection(JaxbLoadCollectionElement returnElement, int cnt) { + final String alias = returnElement.getAlias(); + final String collectionAttribute = returnElement.getRole(); + final LockMode lockMode = Helper.interpretLockMode( returnElement.getLockMode(), origin() ); + int dot = collectionAttribute.lastIndexOf( '.' ); + if ( dot == -1 ) { + throw new MappingException( + "Collection attribute for sql query return [alias=" + alias + + "] not formatted correctly {OwnerClassName.propertyName}", origin() + ); + } + final String ownerClassName = HbmBinder.getClassName( collectionAttribute.substring( 0, dot ), bindingContext().getMappingDefaults().getPackageName() ); + final String ownerPropertyName = collectionAttribute.substring( dot + 1 ); + +// //FIXME: get the PersistentClass +// java.util.Map propertyResults = bindPropertyResults(alias, returnElem, null, mappings ); +// +// return new NativeSQLQueryCollectionReturn( +// alias, +// ownerClassName, +// ownerPropertyName, +// propertyResults, +// lockMode +// ); + return null; + } + + private NativeSQLQueryReturn bindReturnJoin(JaxbReturnJoinElement returnJoinElement, int cnt) { + final String alias = returnJoinElement.getAlias(); + final String roleAttribute = returnJoinElement.getProperty(); + final LockMode lockMode = Helper.interpretLockMode( returnJoinElement.getLockMode(), origin() ); + int dot = roleAttribute.lastIndexOf( '.' ); + if ( dot == -1 ) { + throw new MappingException( + "Role attribute for sql query return [alias=" + alias + + "] not formatted correctly {owningAlias.propertyName}", origin() + ); + } + String roleOwnerAlias = roleAttribute.substring( 0, dot ); + String roleProperty = roleAttribute.substring( dot + 1 ); + + //FIXME: get the PersistentClass +// java.util.Map propertyResults = bindPropertyResults( alias, returnJoinElement, null ); + +// return new NativeSQLQueryJoinReturn( +// alias, +// roleOwnerAlias, +// roleProperty, +// propertyResults, // TODO: bindpropertyresults(alias, returnElem) +// lockMode +// ); + return null; + } + + private NativeSQLQueryRootReturn bindReturn(JaxbReturnElement returnElement,int elementCount) { + String alias = returnElement.getAlias(); + if( StringHelper.isEmpty( alias )) { + alias = "alias_" + elementCount; // hack/workaround as sqlquery impl depend on having a key. + } + String clazz = returnElement.getClazz(); + String entityName = returnElement.getEntityName(); + if(StringHelper.isEmpty( clazz ) && StringHelper.isEmpty( entityName )) { + throw new org.hibernate.MappingException( " must specify either a class or entity-name"); + } + LockMode lockMode = Helper.interpretLockMode( returnElement.getLockMode(), origin() ); + + + EntityBinding entityBinding = null; + + if ( StringHelper.isNotEmpty( entityName ) ) { + entityBinding = metadata.getEntityBinding( entityName ); + } + if ( StringHelper.isNotEmpty( clazz ) ) { + //todo look up entitybinding by class name + } + java.util.Map propertyResults = bindPropertyResults( alias, returnElement, entityBinding ); + + return new NativeSQLQueryRootReturn( + alias, + entityName, + propertyResults, + lockMode + ); + + + } + //TODO impl this, see org.hibernate.metamodel.internal.source.annotations.global.SqlResultSetProcessor.bindEntityResult() + // and org.hibernate.cfg.ResultSetMappingBinder.bindPropertyResults() + private Map bindPropertyResults(String alias, JaxbReturnElement returnElement, EntityBinding entityBinding) { + return null; } private void processNamedQueries() { - if ( mappingRoot().getQueryOrSqlQuery() == null ) { + if ( CollectionHelper.isEmpty( mappingRoot().getQueryOrSqlQuery() ) ) { return; } for ( Object queryOrSqlQuery : mappingRoot().getQueryOrSqlQuery() ) { if ( JaxbQueryElement.class.isInstance( queryOrSqlQuery ) ) { -// bindNamedQuery( element, null, mappings ); + bindNamedQuery( JaxbQueryElement.class.cast( queryOrSqlQuery ) ); } else if ( JaxbSqlQueryElement.class.isInstance( queryOrSqlQuery ) ) { -// bindNamedSQLQuery( element, null, mappings ); + bindNamedSQLQuery( JaxbSqlQueryElement.class.cast( queryOrSqlQuery ) ); } else { throw new MappingException( @@ -265,4 +415,152 @@ public class HibernateMappingProcessor { } } } + + private void bindNamedQuery(JaxbQueryElement queryElement) { + final String queryName = queryElement.getName(); + //path?? + List list = queryElement.getContent(); + final Map queryParam; + String query = ""; + if ( CollectionHelper.isNotEmpty( list ) ) { + queryParam = new HashMap( list.size() ); + for ( Serializable obj : list ) { + if ( JaxbQueryParamElement.class.isInstance( obj ) ) { + JaxbQueryParamElement element = JaxbQueryParamElement.class.cast( obj ); + queryParam.put( element.getName(), element.getType() ); + }else if(String.class.isInstance( obj )){ + query = obj.toString(); + } + } + } + else { + queryParam = Collections.emptyMap(); + } + if ( StringHelper.isEmpty( query ) ) { + throw new MappingException( "Named query[" + queryName + "] has no hql defined", origin() ); + } + final boolean cacheable = queryElement.isCacheable(); + final String region = queryElement.getCacheRegion(); + final Integer timeout = queryElement.getTimeout(); + final Integer fetchSize = queryElement.getFetchSize(); + final boolean readonly = queryElement.isReadOnly(); + final CacheMode cacheMode = queryElement.getCacheMode() == null ? null : CacheMode.valueOf( + queryElement.getCacheMode() + .value() + .toUpperCase() + ); + final String comment = queryElement.getComment(); + final FlushMode flushMode = queryElement.getFlushMode() == null ? null : FlushMode.valueOf( + queryElement.getFlushMode() + .value() + .toUpperCase() + ); + NamedQueryDefinition namedQuery = new NamedQueryDefinition( + queryName, + query, + cacheable, + region, + timeout, + fetchSize, + flushMode, + cacheMode, + readonly, + comment, + queryParam + ); + metadata.addNamedQuery( namedQuery ); + + } + + private void bindNamedSQLQuery(JaxbSqlQueryElement queryElement) { + final String queryName = queryElement.getName(); + //todo patch + final boolean cacheable = queryElement.isCacheable(); + String query = ""; + final String region = queryElement.getCacheRegion(); + final Integer timeout = queryElement.getTimeout(); + final Integer fetchSize = queryElement.getFetchSize(); + final boolean readonly = queryElement.isReadOnly(); + final CacheMode cacheMode = queryElement.getCacheMode()==null ? null : CacheMode.valueOf( queryElement.getCacheMode().value().toUpperCase() ); + final FlushMode flushMode = queryElement.getFlushMode() ==null ? null : FlushMode.valueOf( queryElement.getFlushMode().value().toUpperCase() ); + final String comment = queryElement.getComment(); + final boolean callable = queryElement.isCallable(); + final String resultSetRef = queryElement.getResultsetRef(); + final java.util.List synchronizedTables = new ArrayList(); + final Map queryParam = new HashMap( ); + List list = queryElement.getContent(); + for ( Serializable obj : list ) { + if ( JaxbSynchronizeElement.class.isInstance( obj ) ) { + JaxbSynchronizeElement element = JaxbSynchronizeElement.class.cast( obj ); + synchronizedTables.add( element.getTable() ); + } + else if ( JaxbQueryParamElement.class.isInstance( obj ) ) { + JaxbQueryParamElement element = JaxbQueryParamElement.class.cast( obj ); + queryParam.put( element.getName(), element.getType() ); + } + else if ( JaxbLoadCollectionElement.class.isInstance( obj ) ) { + + } + else if ( JaxbReturnScalarElement.class.isInstance( obj ) ) { + + } + else if ( JaxbReturnElement.class.isInstance( obj ) ) { + + } + else if ( JaxbReturnJoinElement.class.isInstance( obj ) ) { + + } + else if ( String.class.isInstance( obj ) ){ + query = obj.toString(); + } + + } + if ( StringHelper.isEmpty( query ) ) { + throw new MappingException( "Named sql query[" + queryName + "] has no sql defined", origin() ); + } + NamedSQLQueryDefinition namedQuery=null; + if(StringHelper.isNotEmpty( resultSetRef )){ + namedQuery = new NamedSQLQueryDefinition( + queryName, + query, + resultSetRef, + synchronizedTables, + cacheable, + region, + timeout, + fetchSize, + flushMode, + cacheMode, + readonly, + comment, + queryParam, + callable + ); + //TODO check there is no actual definition elemnents when a ref is defined + } else { +// ResultSetMappingDefinition definition = buildResultSetMappingDefinition( queryElem, path, mappings ); +// namedQuery = new NamedSQLQueryDefinition( +// queryName, +// query, +// definition.getQueryReturns(), +// synchronizedTables, +// cacheable, +// region, +// timeout, +// fetchSize, +// flushMode, +// cacheMode, +// readonly, +// comment, +// queryParam, +// callable +// ); + + } + if ( LOG.isDebugEnabled() ) { + LOG.debugf( "Named SQL query: %s -> %s", namedQuery.getName(), namedQuery.getQueryString() ); + } + metadata.addNamedNativeQuery( namedQuery ); + + } } \ No newline at end of file diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/ManyToOneAttributeSourceImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/ManyToOneAttributeSourceImpl.java index b858937643..36f5a37984 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/ManyToOneAttributeSourceImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/ManyToOneAttributeSourceImpl.java @@ -239,7 +239,7 @@ class ManyToOneAttributeSourceImpl extends AbstractHbmSourceNode implements ToOn @Override public boolean areValuesNullableByDefault() { - return ! Helper.getBooleanValue( manyToOneElement.isNotNull(), false ); + return ! Helper.getValue( manyToOneElement.isNotNull(), false ); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/PluralAttributeKeySourceImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/PluralAttributeKeySourceImpl.java index abf0762362..7a72fe7f01 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/PluralAttributeKeySourceImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/PluralAttributeKeySourceImpl.java @@ -64,7 +64,7 @@ public class PluralAttributeKeySourceImpl @Override public boolean isIncludedInUpdateByDefault() { - return Helper.getBooleanValue( keyElement.isUpdate(), true ); + return Helper.getValue( keyElement.isUpdate(), true ); } @Override @@ -84,7 +84,7 @@ public class PluralAttributeKeySourceImpl @Override public boolean isForceNotNull() { - return Helper.getBooleanValue( keyElement.isNotNull(), false ); + return Helper.getValue( keyElement.isNotNull(), false ); } } ); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/PropertyAttributeSourceImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/PropertyAttributeSourceImpl.java index 8ce0cc5d5b..3bcb59769b 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/PropertyAttributeSourceImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/PropertyAttributeSourceImpl.java @@ -98,12 +98,12 @@ class PropertyAttributeSourceImpl extends AbstractHbmSourceNode implements Singu @Override public boolean isIncludedInInsertByDefault() { - return Helper.getBooleanValue( propertyElement.isInsert(), true ); + return Helper.getValue( propertyElement.isInsert(), true ); } @Override public boolean isIncludedInUpdateByDefault() { - return Helper.getBooleanValue( propertyElement.isUpdate(), true ); + return Helper.getValue( propertyElement.isUpdate(), true ); } } ); @@ -132,7 +132,7 @@ class PropertyAttributeSourceImpl extends AbstractHbmSourceNode implements Singu @Override public boolean isLazy() { - return Helper.getBooleanValue( propertyElement.isLazy(), false ); + return Helper.getValue( propertyElement.isLazy(), false ); } @Override @@ -142,7 +142,7 @@ class PropertyAttributeSourceImpl extends AbstractHbmSourceNode implements Singu @Override public boolean isIncludedInOptimisticLocking() { - return Helper.getBooleanValue( propertyElement.isOptimisticLock(), true ); + return Helper.getValue( propertyElement.isOptimisticLock(), true ); } @Override @@ -157,17 +157,17 @@ class PropertyAttributeSourceImpl extends AbstractHbmSourceNode implements Singu @Override public boolean areValuesIncludedInInsertByDefault() { - return Helper.getBooleanValue( propertyElement.isInsert(), true ); + return Helper.getValue( propertyElement.isInsert(), true ); } @Override public boolean areValuesIncludedInUpdateByDefault() { - return Helper.getBooleanValue( propertyElement.isUpdate(), true ); + return Helper.getValue( propertyElement.isUpdate(), true ); } @Override public boolean areValuesNullableByDefault() { - return ! Helper.getBooleanValue( propertyElement.isNotNull(), false ); + return ! Helper.getValue( propertyElement.isNotNull(), false ); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/RootEntitySourceImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/RootEntitySourceImpl.java index b335116c9b..6954192312 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/RootEntitySourceImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/RootEntitySourceImpl.java @@ -172,7 +172,7 @@ public class RootEntitySourceImpl extends AbstractEntitySourceImpl implements Ro @Override public OptimisticLockStyle getOptimisticLockStyle() { - final String optimisticLockModeString = Helper.getStringValue( entityElement().getOptimisticLock(), "version" ); + final String optimisticLockModeString = Helper.getValue( entityElement().getOptimisticLock(), "version" ); try { return OptimisticLockStyle.valueOf( optimisticLockModeString.toUpperCase() ); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/VersionAttributeSourceImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/VersionAttributeSourceImpl.java index 296e12afdf..c091a0a0a5 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/VersionAttributeSourceImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/source/hbm/VersionAttributeSourceImpl.java @@ -79,7 +79,7 @@ class VersionAttributeSourceImpl @Override public boolean isIncludedInInsertByDefault() { - return Helper.getBooleanValue( versionElement.isInsert(), true ); + return Helper.getValue( versionElement.isInsert(), true ); } @Override @@ -169,7 +169,7 @@ class VersionAttributeSourceImpl @Override public boolean areValuesIncludedInInsertByDefault() { - return Helper.getBooleanValue( versionElement.isInsert(), true ); + return Helper.getValue( versionElement.isInsert(), true ); } @Override diff --git a/hibernate-core/src/main/resources/org/hibernate/hibernate-mapping-4.0.xsd b/hibernate-core/src/main/resources/org/hibernate/hibernate-mapping-4.0.xsd index d020511912..334f712999 100644 --- a/hibernate-core/src/main/resources/org/hibernate/hibernate-mapping-4.0.xsd +++ b/hibernate-core/src/main/resources/org/hibernate/hibernate-mapping-4.0.xsd @@ -54,12 +54,12 @@ arbitrary number of queries, and import declarations of arbitrary classes. FILTER-PARAM element; qualifies parameters found within a FILTER-DEF condition. --> - - - - - - + + + + + + @@ -106,7 +106,7 @@ arbitrary number of queries, and import declarations of arbitrary classes. - + @@ -159,7 +159,7 @@ arbitrary number of queries, and import declarations of arbitrary classes. - + @@ -276,9 +276,9 @@ arbitrary number of queries, and import declarations of arbitrary classes. - - - + + + @@ -287,15 +287,15 @@ arbitrary number of queries, and import declarations of arbitrary classes. - - + + - + @@ -433,15 +433,15 @@ arbitrary number of queries, and import declarations of arbitrary classes. - - - - + + + + + - + - @@ -454,9 +454,6 @@ arbitrary number of queries, and import declarations of arbitrary classes. - - - @@ -477,33 +474,20 @@ arbitrary number of queries, and import declarations of arbitrary classes. - - - - + + + + - - - - - - + + - - - - - - - - - @@ -542,12 +526,12 @@ arbitrary number of queries, and import declarations of arbitrary classes. - + - + - + @@ -653,11 +637,11 @@ arbitrary number of queries, and import declarations of arbitrary classes. - + - - + + @@ -728,7 +712,7 @@ arbitrary number of queries, and import declarations of arbitrary classes. - + @@ -740,32 +724,20 @@ arbitrary number of queries, and import declarations of arbitrary classes. - - - - + + + + - - - - - - + + - - - - - - - - @@ -773,7 +745,7 @@ arbitrary number of queries, and import declarations of arbitrary classes. - + @@ -791,17 +763,14 @@ arbitrary number of queries, and import declarations of arbitrary classes. - - - + + + - + - - - - + - - - @@ -910,7 +876,7 @@ arbitrary number of queries, and import declarations of arbitrary classes. - + @@ -937,32 +903,19 @@ arbitrary number of queries, and import declarations of arbitrary classes. - - - - + + + + - - - - - - + + - - - - - - - - - - - + + - - - - - - - - - + @@ -1318,9 +1258,6 @@ arbitrary number of queries, and import declarations of arbitrary classes. - - - @@ -1371,7 +1308,7 @@ arbitrary number of queries, and import declarations of arbitrary classes. - + @@ -1393,11 +1330,11 @@ arbitrary number of queries, and import declarations of arbitrary classes. - + - + + + + + + + + + + + @@ -1485,56 +1436,24 @@ arbitrary number of queries, and import declarations of arbitrary classes. - - - - + + + + - - - - - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -1558,21 +1477,12 @@ arbitrary number of queries, and import declarations of arbitrary classes. - + - - - - - - - - - - + - + @@ -1657,6 +1567,13 @@ arbitrary number of queries, and import declarations of arbitrary classes. + + + + + + + - + - - - @@ -1794,6 +1708,12 @@ arbitrary number of queries, and import declarations of arbitrary classes. + + + + + + diff --git a/hibernate-core/src/main/xjb/hbm-mapping-bindings.xjb b/hibernate-core/src/main/xjb/hbm-mapping-bindings.xjb index 240b645d3a..dc5d0d1c72 100644 --- a/hibernate-core/src/main/xjb/hbm-mapping-bindings.xjb +++ b/hibernate-core/src/main/xjb/hbm-mapping-bindings.xjb @@ -38,20 +38,6 @@ org.hibernate.internal.jaxb.mapping.hbm.TableInformationSource - - - org.hibernate.internal.jaxb.mapping.hbm.CustomSqlElement - - - org.hibernate.internal.jaxb.mapping.hbm.CustomSqlElement - - - org.hibernate.internal.jaxb.mapping.hbm.CustomSqlElement - - - org.hibernate.internal.jaxb.mapping.hbm.CustomSqlElement - - org.hibernate.internal.jaxb.mapping.hbm.SingularAttributeSource @@ -76,7 +62,6 @@ org.hibernate.internal.jaxb.mapping.hbm.PluralAttributeElement - org.hibernate.internal.jaxb.mapping.hbm.PluralAttributeElement org.hibernate.internal.jaxb.mapping.hbm.IdBagPluralAttributeElementAdapter @@ -108,30 +93,15 @@ - - - - - - - - - - - - - - - @@ -144,15 +114,9 @@ - - - - - - @@ -162,16 +126,12 @@ - - - + - - - - + + @@ -181,7 +141,6 @@ - \ No newline at end of file