HHH-6279 - Bind database object definitions

This commit is contained in:
Steve Ebersole 2011-06-01 16:42:23 -05:00
parent 1776207c8b
commit e0d90cee49
6 changed files with 154 additions and 32 deletions

View File

@ -36,14 +36,6 @@ import org.hibernate.mapping.RelationalModel;
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public interface AuxiliaryDatabaseObject extends RelationalModel, Serializable { public interface AuxiliaryDatabaseObject extends RelationalModel, Serializable {
/**
* Add the given dialect name to the scope of dialects to which
* this database object applies.
*
* @param dialectName The name of a dialect.
*/
void addDialectScope(String dialectName);
/** /**
* Does this database object apply to the given dialect? * Does this database object apply to the given dialect?
* *

View File

@ -0,0 +1,60 @@
/*
* 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.metamodel.relational;
import java.util.Set;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.spi.Mapping;
/**
* @author Steve Ebersole
*/
public class BasicAuxiliaryDatabaseObjectImpl implements AuxiliaryDatabaseObject {
private final String createString;
private final String dropString;
private final Set<String> dialectScopes;
public BasicAuxiliaryDatabaseObjectImpl(String createString, String dropString, Set<String> dialectScopes) {
this.createString = createString;
this.dropString = dropString;
this.dialectScopes = dialectScopes;
}
@Override
public boolean appliesToDialect(Dialect dialect) {
// empty means no scoping
return dialectScopes.isEmpty() || dialectScopes.contains( dialect.getClass().getName() );
}
@Override
public String sqlCreateString(Dialect dialect, Mapping p, String defaultCatalog, String defaultSchema) {
return createString;
}
@Override
public String sqlDropString(Dialect dialect, String defaultCatalog, String defaultSchema) {
return dropString;
}
}

View File

@ -94,7 +94,7 @@ public class TypeDefBinder {
Map<String, String> prms, Map<String, String> prms,
MetadataImpl metadata) { MetadataImpl metadata) {
LOG.debugf( "Binding type definition: %s", name ); LOG.debugf( "Binding type definition: %s", name );
metadata.addTypeDef( new TypeDef( name, typeClass, prms ) ); metadata.addTypeDefinition( new TypeDef( name, typeClass, prms ) );
} }
private TypeDefBinder() { private TypeDefBinder() {

View File

@ -32,10 +32,12 @@ import java.util.Set;
import org.hibernate.cfg.NamingStrategy; import org.hibernate.cfg.NamingStrategy;
import org.hibernate.engine.spi.FilterDefinition; import org.hibernate.engine.spi.FilterDefinition;
import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.StringHelper;
import org.hibernate.metamodel.relational.AuxiliaryDatabaseObject;
import org.hibernate.metamodel.binding.FetchProfile; import org.hibernate.metamodel.binding.FetchProfile;
import org.hibernate.metamodel.binding.FetchProfile.Fetch; import org.hibernate.metamodel.binding.FetchProfile.Fetch;
import org.hibernate.metamodel.binding.TypeDef; import org.hibernate.metamodel.binding.TypeDef;
import org.hibernate.metamodel.domain.MetaAttribute; import org.hibernate.metamodel.domain.MetaAttribute;
import org.hibernate.metamodel.relational.BasicAuxiliaryDatabaseObjectImpl;
import org.hibernate.metamodel.source.MappingException; import org.hibernate.metamodel.source.MappingException;
import org.hibernate.metamodel.source.Origin; import org.hibernate.metamodel.source.Origin;
import org.hibernate.metamodel.source.hbm.util.MappingHelper; import org.hibernate.metamodel.source.hbm.util.MappingHelper;
@ -53,6 +55,8 @@ import org.hibernate.metamodel.source.hbm.xml.mapping.XMLUnionSubclassElement;
import org.hibernate.metamodel.source.internal.JaxbRoot; import org.hibernate.metamodel.source.internal.JaxbRoot;
import org.hibernate.metamodel.source.spi.MetadataImplementor; import org.hibernate.metamodel.source.spi.MetadataImplementor;
import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.classloading.spi.ClassLoaderService;
import org.hibernate.service.classloading.spi.ClassLoadingException;
import org.hibernate.type.Type; import org.hibernate.type.Type;
/** /**
@ -154,10 +158,13 @@ public class HbmBinder implements MappingDefaults {
} }
public void processHibernateMapping() { public void processHibernateMapping() {
bindTypeDefinitions( ); // perform bindings with no pre-requisites
bindFilterDefinitions( ); bindDatabaseObjectDefinitions();
bindFetchProfiles( ); bindTypeDefinitions();
bindFilterDefinitions();
bindIdentifierGenerators(); bindIdentifierGenerators();
if ( hibernateMapping.getClazzOrSubclassOrJoinedSubclass() != null ) { if ( hibernateMapping.getClazzOrSubclassOrJoinedSubclass() != null ) {
for ( Object clazzOrSubclass : hibernateMapping.getClazzOrSubclassOrJoinedSubclass() ) { for ( Object clazzOrSubclass : hibernateMapping.getClazzOrSubclassOrJoinedSubclass() ) {
if ( XMLClass.class.isInstance( clazzOrSubclass ) ) { if ( XMLClass.class.isInstance( clazzOrSubclass ) ) {
@ -185,6 +192,10 @@ public class HbmBinder implements MappingDefaults {
} }
} }
} }
bindFetchProfiles();
bindImports();
if ( hibernateMapping.getQueryOrSqlQuery() != null ) { if ( hibernateMapping.getQueryOrSqlQuery() != null ) {
for ( Object queryOrSqlQuery : hibernateMapping.getQueryOrSqlQuery() ) { for ( Object queryOrSqlQuery : hibernateMapping.getQueryOrSqlQuery() ) {
if ( XMLQueryElement.class.isInstance( queryOrSqlQuery ) ) { if ( XMLQueryElement.class.isInstance( queryOrSqlQuery ) ) {
@ -204,10 +215,57 @@ public class HbmBinder implements MappingDefaults {
if ( hibernateMapping.getResultset() != null ) { if ( hibernateMapping.getResultset() != null ) {
// bindResultSetMappingDefinitions( element, null, mappings ); // bindResultSetMappingDefinitions( element, null, mappings );
} }
if ( hibernateMapping.getDatabaseObject() != null ) {
// bindAuxiliaryDatabaseObjects( element, mappings );
} }
bindImports( );
private void bindDatabaseObjectDefinitions() {
if ( hibernateMapping.getDatabaseObject() == null ) {
return;
}
for ( XMLHibernateMapping.XMLDatabaseObject databaseObjectElement : hibernateMapping.getDatabaseObject() ) {
final AuxiliaryDatabaseObject auxiliaryDatabaseObject;
if ( databaseObjectElement.getDefinition() != null ) {
final String className = databaseObjectElement.getDefinition().getClazz();
try {
auxiliaryDatabaseObject = (AuxiliaryDatabaseObject) classLoaderService().classForName( className ).newInstance();
}
catch (ClassLoadingException e) {
throw e;
}
catch (Exception e) {
throw new MappingException(
"could not instantiate custom database object class [" + className + "]",
jaxbRoot.getOrigin()
);
}
}
else {
Set<String> dialectScopes = new HashSet<String>();
if ( databaseObjectElement.getDialectScope() != null ) {
for ( XMLHibernateMapping.XMLDatabaseObject.XMLDialectScope dialectScope : databaseObjectElement.getDialectScope() ) {
dialectScopes.add( dialectScope.getName() );
}
}
auxiliaryDatabaseObject = new BasicAuxiliaryDatabaseObjectImpl(
databaseObjectElement.getCreate(),
databaseObjectElement.getDrop(),
dialectScopes
);
}
metadata.addAuxiliaryDatabaseObject( auxiliaryDatabaseObject );
}
}
private void bindTypeDefinitions() {
if ( hibernateMapping.getTypedef() == null ) {
return;
}
for ( XMLHibernateMapping.XMLTypedef typedef : hibernateMapping.getTypedef() ) {
final Map<String, String> parameters = new HashMap<String, String>();
for ( XMLParamElement paramElement : typedef.getParam() ) {
parameters.put( paramElement.getName(), paramElement.getValue() );
}
metadata.addTypeDefinition( new TypeDef( typedef.getName(), typedef.getClazz(), parameters ) );
}
} }
private void bindIdentifierGenerators() { private void bindIdentifierGenerators() {
@ -234,18 +292,6 @@ public class HbmBinder implements MappingDefaults {
} }
} }
private void bindTypeDefinitions() {
if ( hibernateMapping.getTypedef() == null ) {
return;
}
for ( XMLHibernateMapping.XMLTypedef typedef : hibernateMapping.getTypedef() ) {
final Map<String, String> parameters = new HashMap<String, String>();
for ( XMLParamElement paramElement : typedef.getParam() ) {
parameters.put( paramElement.getName(), paramElement.getValue() );
}
metadata.addTypeDef( new TypeDef( typedef.getName(), typedef.getClazz(), parameters ) );
}
}
private void bindFetchProfiles(){ private void bindFetchProfiles(){
if(hibernateMapping.getFetchProfile() == null){ if(hibernateMapping.getFetchProfile() == null){
return; return;
@ -307,6 +353,15 @@ public class HbmBinder implements MappingDefaults {
} }
} }
private ClassLoaderService classLoaderService;
private ClassLoaderService classLoaderService() {
if ( classLoaderService == null ) {
classLoaderService = metadata.getServiceRegistry().getService( ClassLoaderService.class );
}
return classLoaderService;
}
String extractEntityName(XMLClass entityClazz) { String extractEntityName(XMLClass entityClazz) {
return HbmHelper.extractEntityName( entityClazz, packageName ); return HbmHelper.extractEntityName( entityClazz, packageName );
} }

View File

@ -52,6 +52,7 @@ import org.hibernate.metamodel.binding.IdGenerator;
import org.hibernate.metamodel.binding.PluralAttributeBinding; import org.hibernate.metamodel.binding.PluralAttributeBinding;
import org.hibernate.metamodel.binding.TypeDef; import org.hibernate.metamodel.binding.TypeDef;
import org.hibernate.metamodel.domain.MetaAttribute; import org.hibernate.metamodel.domain.MetaAttribute;
import org.hibernate.metamodel.relational.AuxiliaryDatabaseObject;
import org.hibernate.metamodel.relational.Database; import org.hibernate.metamodel.relational.Database;
import org.hibernate.metamodel.source.annotation.xml.XMLEntityMappings; import org.hibernate.metamodel.source.annotation.xml.XMLEntityMappings;
import org.hibernate.metamodel.source.annotations.AnnotationBinder; import org.hibernate.metamodel.source.annotations.AnnotationBinder;
@ -79,9 +80,12 @@ public class MetadataImpl implements MetadataImplementor, Serializable {
private final BasicServiceRegistry serviceRegistry; private final BasicServiceRegistry serviceRegistry;
private final Options options; private final Options options;
private ClassLoaderService classLoaderService; private ClassLoaderService classLoaderService;
private final Database database = new Database();
private TypeResolver typeResolver = new TypeResolver(); private TypeResolver typeResolver = new TypeResolver();
private DefaultIdentifierGeneratorFactory identifierGeneratorFactory = new DefaultIdentifierGeneratorFactory(); private DefaultIdentifierGeneratorFactory identifierGeneratorFactory = new DefaultIdentifierGeneratorFactory();
private final Database database = new Database();
/** /**
* Maps the fully qualified class name of an entity to its entity binding * Maps the fully qualified class name of an entity to its entity binding
*/ */
@ -95,6 +99,9 @@ public class MetadataImpl implements MetadataImplementor, Serializable {
private Map<String, NamedSQLQueryDefinition> namedNativeQueryDefs = new HashMap<String, NamedSQLQueryDefinition>(); private Map<String, NamedSQLQueryDefinition> namedNativeQueryDefs = new HashMap<String, NamedSQLQueryDefinition>();
private Map<String, FilterDefinition> filterDefs = new HashMap<String, FilterDefinition>(); private Map<String, FilterDefinition> filterDefs = new HashMap<String, FilterDefinition>();
// todo : keep as part of Database?
private List<AuxiliaryDatabaseObject> auxiliaryDatabaseObjects = new ArrayList<AuxiliaryDatabaseObject>();
public MetadataImpl(MetadataSources metadataSources, Options options) { public MetadataImpl(MetadataSources metadataSources, Options options) {
this.serviceRegistry = metadataSources.getServiceRegistry(); this.serviceRegistry = metadataSources.getServiceRegistry();
this.options = options; this.options = options;
@ -141,6 +148,11 @@ public class MetadataImpl implements MetadataImplementor, Serializable {
identifierGeneratorFactory.register( name, classLoaderService().classForName( generatorClassName ) ); identifierGeneratorFactory.register( name, classLoaderService().classForName( generatorClassName ) );
} }
@Override
public void addAuxiliaryDatabaseObject(AuxiliaryDatabaseObject auxiliaryDatabaseObject) {
auxiliaryDatabaseObjects.add( auxiliaryDatabaseObject );
}
public void addNamedNativeQuery(String name, NamedSQLQueryDefinition def) { public void addNamedNativeQuery(String name, NamedSQLQueryDefinition def) {
namedNativeQueryDefs.put( name, def ); namedNativeQueryDefs.put( name, def );
} }
@ -164,7 +176,7 @@ public class MetadataImpl implements MetadataImplementor, Serializable {
} }
@Override @Override
public void addTypeDef(TypeDef typeDef) { public void addTypeDefinition(TypeDef typeDef) {
final TypeDef previous = typeDefs.put( typeDef.getName(), typeDef ); final TypeDef previous = typeDefs.put( typeDef.getName(), typeDef );
if ( previous != null ) { if ( previous != null ) {
LOG.debugf( "Duplicate typedef name [%s] now -> %s", typeDef.getName(), typeDef.getTypeClass() ); LOG.debugf( "Duplicate typedef name [%s] now -> %s", typeDef.getName(), typeDef.getTypeClass() );
@ -172,7 +184,7 @@ public class MetadataImpl implements MetadataImplementor, Serializable {
} }
@Override @Override
public Iterable<TypeDef> getTypeDefs() { public Iterable<TypeDef> getTypeDefinitions() {
return typeDefs.values(); return typeDefs.values();
} }

View File

@ -30,6 +30,7 @@ import org.hibernate.metamodel.binding.FetchProfile;
import org.hibernate.metamodel.binding.IdGenerator; import org.hibernate.metamodel.binding.IdGenerator;
import org.hibernate.metamodel.binding.PluralAttributeBinding; import org.hibernate.metamodel.binding.PluralAttributeBinding;
import org.hibernate.metamodel.binding.TypeDef; import org.hibernate.metamodel.binding.TypeDef;
import org.hibernate.metamodel.relational.AuxiliaryDatabaseObject;
import org.hibernate.metamodel.relational.Database; import org.hibernate.metamodel.relational.Database;
import org.hibernate.service.BasicServiceRegistry; import org.hibernate.service.BasicServiceRegistry;
import org.hibernate.type.TypeResolver; import org.hibernate.type.TypeResolver;
@ -56,9 +57,9 @@ public interface MetadataImplementor extends Metadata {
public void addFetchProfile(FetchProfile profile); public void addFetchProfile(FetchProfile profile);
public void addTypeDef(TypeDef typeDef); public void addTypeDefinition(TypeDef typeDef);
public Iterable<TypeDef> getTypeDefs(); public Iterable<TypeDef> getTypeDefinitions();
public void addFilterDefinition(FilterDefinition filterDefinition); public void addFilterDefinition(FilterDefinition filterDefinition);
@ -67,4 +68,6 @@ public interface MetadataImplementor extends Metadata {
public void registerIdentifierGenerator(String name, String clazz); public void registerIdentifierGenerator(String name, String clazz);
public IdGenerator getIdGenerator(String name); public IdGenerator getIdGenerator(String name);
public void addAuxiliaryDatabaseObject(AuxiliaryDatabaseObject auxiliaryDatabaseObject);
} }