HHH-6291 - Basic MetadataImpl redesign

This commit is contained in:
Steve Ebersole 2011-06-05 18:45:30 -05:00
parent b8b003efa3
commit baeb6dc400
16 changed files with 592 additions and 704 deletions

View File

@ -37,6 +37,7 @@ import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.metamodel.domain.Entity;
import org.hibernate.metamodel.relational.Column;
import org.hibernate.metamodel.relational.TableSpecification;
import org.hibernate.metamodel.source.hbm.HbmBindingContext;
import org.hibernate.metamodel.source.hbm.HbmHelper;
import org.hibernate.metamodel.source.hbm.util.MappingHelper;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLClass;
@ -44,7 +45,6 @@ import org.hibernate.metamodel.source.hbm.xml.mapping.XMLSqlDeleteElement;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLSqlInsertElement;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLSqlUpdateElement;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLSynchronizeElement;
import org.hibernate.metamodel.source.spi.BindingContext;
import org.hibernate.metamodel.source.spi.MetaAttributeContext;
/**
@ -101,7 +101,7 @@ public class EntityBinding {
}
// TODO: change to intialize from Doimain
public void fromHbmXml(BindingContext bindingContext, XMLClass entityClazz, Entity entity) {
public void fromHbmXml(HbmBindingContext bindingContext, XMLClass entityClazz, Entity entity) {
this.entity = entity;
metaAttributeContext = HbmHelper.extractMetaAttributeContext( entityClazz.getMeta(), true, bindingContext.getMetaAttributeContext() );

View File

@ -23,11 +23,18 @@
*/
package org.hibernate.metamodel.source.annotations;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.hibernate.HibernateException;
import org.hibernate.internal.CoreMessageLogger;
import org.jboss.jandex.Index;
import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.source.annotation.xml.XMLEntityMappings;
import org.hibernate.metamodel.source.annotations.entity.ConfiguredClass;
import org.hibernate.metamodel.source.annotations.entity.ConfiguredClassHierarchy;
import org.hibernate.metamodel.source.annotations.entity.EntityBinder;
@ -38,7 +45,14 @@ import org.hibernate.metamodel.source.annotations.global.TypeDefBinder;
import org.hibernate.metamodel.source.annotations.global.FetchProfileBinder;
import org.hibernate.metamodel.source.annotations.global.TableBinder;
import org.hibernate.metamodel.source.annotations.util.ConfiguredClassHierarchyBuilder;
import org.hibernate.metamodel.source.annotations.xml.OrmXmlParser;
import org.hibernate.metamodel.source.internal.JaxbRoot;
import org.hibernate.metamodel.source.internal.MetadataImpl;
import org.hibernate.metamodel.source.spi.Binder;
import org.hibernate.metamodel.source.spi.MetadataImplementor;
import org.hibernate.service.classloading.spi.ClassLoaderService;
import org.jboss.jandex.Indexer;
import org.jboss.logging.Logger;
/**
@ -48,35 +62,77 @@ import org.jboss.logging.Logger;
*
* @author Hardy Ferentschik
*/
public class AnnotationBinder {
public class AnnotationBinder implements Binder {
private static final CoreMessageLogger LOG = Logger.getMessageLogger( CoreMessageLogger.class, AnnotationBinder.class.getName() );
private final MetadataImpl metadata;
private final Index index;
public AnnotationBinder(MetadataImpl metadata, Index index) {
private final MetadataImplementor metadata;
private Index index;
private ClassLoaderService classLoaderService;
public AnnotationBinder(MetadataImpl metadata) {
this.metadata = metadata;
this.index = index;
}
public void bind() {
preEntityBindings();
bindMappedClasses();
postEntityBindings();
@Override
@SuppressWarnings( {"unchecked"})
public void prepare(MetadataSources sources) {
// create a jandex index from the annotated classes
Indexer indexer = new Indexer();
for ( Class<?> clazz : sources.getAnnotatedClasses() ) {
indexClass( indexer, clazz.getName().replace( '.', '/' ) + ".class" );
}
/**
* Binds global configuration data prior to entity binding. This includes generators and type definitions.
*/
private void preEntityBindings() {
TypeDefBinder.bind(metadata, index);
IdGeneratorBinder.bind(metadata, index);
// add package-info from the configured packages
for ( String packageName : sources.getAnnotatedPackages() ) {
indexClass( indexer, packageName.replace( '.', '/' ) + "/package-info.class" );
}
/**
* Does the actual entity binding (see {@link org.hibernate.metamodel.binding.EntityBinding}.
*/
private void bindMappedClasses() {
index = indexer.complete();
List<JaxbRoot<XMLEntityMappings>> mappings = new ArrayList<JaxbRoot<XMLEntityMappings>>();
for ( JaxbRoot<?> root : sources.getJaxbRootList() ) {
if ( root.getRoot() instanceof XMLEntityMappings ) {
mappings.add( (JaxbRoot<XMLEntityMappings>) root );
}
}
if ( !mappings.isEmpty() ) {
// process the xml configuration
final OrmXmlParser ormParser = new OrmXmlParser( metadata );
index = ormParser.parseAndUpdateIndex( mappings, index );
}
}
private void indexClass(Indexer indexer, String className) {
InputStream stream = classLoaderService().locateResourceStream( className );
try {
indexer.index( stream );
}
catch ( IOException e ) {
throw new HibernateException( "Unable to open input stream for class " + className, e );
}
}
private ClassLoaderService classLoaderService(){
if ( classLoaderService == null ) {
classLoaderService = metadata.getServiceRegistry().getService( ClassLoaderService.class );
}
return classLoaderService;
}
@Override
public void bindIndependentMetadata(MetadataSources sources) {
TypeDefBinder.bind( metadata, index );
}
@Override
public void bindTypeDependentMetadata(MetadataSources sources) {
IdGeneratorBinder.bind( metadata, index );
}
@Override
public void bindMappingMetadata(MetadataSources sources, List<String> processedEntityNames) {
// need to order our annotated entities into an order we can process
Set<ConfiguredClassHierarchy> hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies(
index, metadata.getServiceRegistry()
@ -92,15 +148,12 @@ public class AnnotationBinder {
}
}
/**
* Binds global configuration data post entity binding. This includes mappings which live outside of the configuration for a single
* entity or entity hierarchy, for example sequence generators, fetch profiles, etc
*/
private void postEntityBindings() {
@Override
public void bindMappingDependentMetadata(MetadataSources sources) {
TableBinder.bind( metadata, index );
FetchProfileBinder.bind( metadata, index );
QueryBinder.bind(metadata, index);
FilterDefBinder.bind(metadata, index);
QueryBinder.bind( metadata, index );
FilterDefBinder.bind( metadata, index );
}
}

View File

@ -1,121 +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.metamodel.source.annotations;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.jboss.jandex.Index;
import org.jboss.jandex.Indexer;
import org.hibernate.HibernateException;
import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.source.annotation.xml.XMLEntityMappings;
import org.hibernate.metamodel.source.annotations.xml.OrmXmlParser;
import org.hibernate.metamodel.source.internal.JaxbRoot;
import org.hibernate.metamodel.source.spi.Binder;
import org.hibernate.metamodel.source.spi.MetadataImplementor;
import org.hibernate.service.classloading.spi.ClassLoaderService;
/**
* @author Steve Ebersole
*/
public class JpaBinder implements Binder {
private final MetadataImplementor metadata;
private Index index;
private ClassLoaderService classLoaderService;
public JpaBinder(MetadataImplementor metadata) {
this.metadata = metadata;
}
@Override
@SuppressWarnings( {"unchecked"})
public void prepare(MetadataSources sources) {
// create a jandex index from the annotated classes
Indexer indexer = new Indexer();
for ( Class<?> clazz : sources.getAnnotatedClasses() ) {
indexClass( indexer, clazz.getName().replace( '.', '/' ) + ".class" );
}
// add package-info from the configured packages
for ( String packageName : sources.getAnnotatedPackages() ) {
indexClass( indexer, packageName.replace( '.', '/' ) + "/package-info.class" );
}
index = indexer.complete();
List<JaxbRoot<XMLEntityMappings>> mappings = new ArrayList<JaxbRoot<XMLEntityMappings>>();
for ( JaxbRoot<?> root : sources.getJaxbRootList() ) {
if ( root.getRoot() instanceof XMLEntityMappings ) {
mappings.add( (JaxbRoot<XMLEntityMappings>) root );
}
}
if ( !mappings.isEmpty() ) {
// process the xml configuration
final OrmXmlParser ormParser = new OrmXmlParser( metadata );
index = ormParser.parseAndUpdateIndex( mappings, index );
}
}
private void indexClass(Indexer indexer, String className) {
InputStream stream = classLoaderService().locateResourceStream( className );
try {
indexer.index( stream );
}
catch ( IOException e ) {
throw new HibernateException( "Unable to open input stream for class " + className, e );
}
}
private ClassLoaderService classLoaderService(){
if ( classLoaderService == null ) {
classLoaderService = metadata.getServiceRegistry().getService( ClassLoaderService.class );
}
return classLoaderService;
}
@Override
public void bindIndependentMetadata(MetadataSources sources) {
// todo : implement method body
}
@Override
public void bindTypeDependentMetadata(MetadataSources sources) {
// todo : implement method body
}
@Override
public void bindMappingMetadata(MetadataSources sources, List<String> processedEntityNames) {
// todo : implement method body
}
@Override
public void bindMappingDependentMetadata(MetadataSources sources) {
// todo : implement method body
}
}

View File

@ -36,7 +36,7 @@ import org.hibernate.metamodel.binding.FetchProfile;
import org.hibernate.metamodel.binding.FetchProfile.Fetch;
import org.hibernate.metamodel.source.annotations.HibernateDotNames;
import org.hibernate.metamodel.source.annotations.util.JandexHelper;
import org.hibernate.metamodel.source.internal.MetadataImpl;
import org.hibernate.metamodel.source.spi.MetadataImplementor;
/**
* Binds fetch profiles found in annotations.
@ -52,8 +52,7 @@ public class FetchProfileBinder {
* @param jandex the jandex index
*/
// TODO verify that association exists. See former VerifyFetchProfileReferenceSecondPass
public static void bind(MetadataImpl metadata,
Index jandex) {
public static void bind(MetadataImplementor metadata, Index jandex) {
for ( AnnotationInstance fetchProfile : jandex.getAnnotations( HibernateDotNames.FETCH_PROFILE ) ) {
bind( metadata, fetchProfile );
}
@ -64,8 +63,7 @@ public class FetchProfileBinder {
}
}
private static void bind(MetadataImpl metadata,
AnnotationInstance fetchProfile) {
private static void bind(MetadataImplementor metadata, AnnotationInstance fetchProfile) {
String name = JandexHelper.getValueAsString( fetchProfile, "name" );
Set<Fetch> fetches = new HashSet<Fetch>();
for ( AnnotationInstance override : JandexHelper.getValueAsArray( fetchProfile, "fetchOverrides" ) ) {

View File

@ -36,7 +36,7 @@ import org.hibernate.engine.spi.FilterDefinition;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.metamodel.source.annotations.HibernateDotNames;
import org.hibernate.metamodel.source.annotations.util.JandexHelper;
import org.hibernate.metamodel.source.internal.MetadataImpl;
import org.hibernate.metamodel.source.spi.MetadataImplementor;
import org.hibernate.type.Type;
public class FilterDefBinder {
@ -52,8 +52,7 @@ public class FilterDefBinder {
* @param metadata the global metadata
* @param jandex the jandex index
*/
public static void bind(MetadataImpl metadata,
Index jandex) {
public static void bind(MetadataImplementor metadata, Index jandex) {
for ( AnnotationInstance filterDef : jandex.getAnnotations( HibernateDotNames.FILTER_DEF ) ) {
bind( metadata, filterDef );
}
@ -64,8 +63,7 @@ public class FilterDefBinder {
}
}
private static void bind(MetadataImpl metadata,
AnnotationInstance filterDef) {
private static void bind(MetadataImplementor metadata, AnnotationInstance filterDef) {
String name = JandexHelper.getValueAsString( filterDef, "name" );
Map<String, Type> prms = new HashMap<String, Type>();
for ( AnnotationInstance prm : JandexHelper.getValueAsArray( filterDef, "parameters" ) ) {

View File

@ -23,10 +23,10 @@
*/
package org.hibernate.metamodel.source.annotations.global;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.GenerationType;
import javax.persistence.SequenceGenerator;
import java.util.HashMap;
import java.util.Map;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.Index;
@ -48,7 +48,7 @@ import org.hibernate.metamodel.binding.IdGenerator;
import org.hibernate.metamodel.source.annotations.HibernateDotNames;
import org.hibernate.metamodel.source.annotations.JPADotNames;
import org.hibernate.metamodel.source.annotations.util.JandexHelper;
import org.hibernate.metamodel.source.internal.MetadataImpl;
import org.hibernate.metamodel.source.spi.MetadataImplementor;
public class IdGeneratorBinder {
@ -57,6 +57,9 @@ public class IdGeneratorBinder {
IdGeneratorBinder.class.getName()
);
private IdGeneratorBinder() {
}
private static void addStringParameter(AnnotationInstance annotation,
String element,
Map<String, String> parameters,
@ -74,8 +77,7 @@ public class IdGeneratorBinder {
* @param metadata the global metadata
* @param jandex the jandex index
*/
public static void bind(MetadataImpl metadata,
Index jandex) {
public static void bind(MetadataImplementor metadata, Index jandex) {
for ( AnnotationInstance generator : jandex.getAnnotations( JPADotNames.SEQUENCE_GENERATOR ) ) {
bindSequenceGenerator( metadata, generator );
}
@ -92,8 +94,7 @@ public class IdGeneratorBinder {
}
}
private static void bindGenericGenerator(MetadataImpl metadata,
AnnotationInstance generator) {
private static void bindGenericGenerator(MetadataImplementor metadata, AnnotationInstance generator) {
String name = JandexHelper.getValueAsString( generator, "name" );
Map<String, String> prms = new HashMap<String, String>();
for ( AnnotationInstance prm : JandexHelper.getValueAsArray( generator, "parameters" ) ) {
@ -109,8 +110,7 @@ public class IdGeneratorBinder {
LOG.tracef( "Add generic generator with name: %s", name );
}
private static void bindSequenceGenerator(MetadataImpl metadata,
AnnotationInstance generator) {
private static void bindSequenceGenerator(MetadataImplementor metadata, AnnotationInstance generator) {
String name = JandexHelper.getValueAsString( generator, "name" );
String strategy;
Map<String, String> prms = new HashMap<String, String>();
@ -142,8 +142,7 @@ public class IdGeneratorBinder {
LOG.tracef( "Add sequence generator with name: %s", name );
}
private static void bindTableGenerator(MetadataImpl metadata,
AnnotationInstance generator) {
private static void bindTableGenerator(MetadataImplementor metadata, AnnotationInstance generator) {
String name = JandexHelper.getValueAsString( generator, "name" );
String strategy;
Map<String, String> prms = new HashMap<String, String>();
@ -183,9 +182,6 @@ public class IdGeneratorBinder {
LOG.tracef( "Add table generator with name: %s", name );
}
private IdGeneratorBinder() {
}
public static String generatorType(GenerationType generatorEnum, boolean useNewGeneratorMappings) {
switch ( generatorEnum ) {
case IDENTITY:

View File

@ -23,11 +23,11 @@
*/
package org.hibernate.metamodel.source.annotations.global;
import java.util.HashMap;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import java.util.HashMap;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.Index;
@ -47,7 +47,7 @@ import org.hibernate.internal.util.StringHelper;
import org.hibernate.metamodel.source.annotations.HibernateDotNames;
import org.hibernate.metamodel.source.annotations.JPADotNames;
import org.hibernate.metamodel.source.annotations.util.JandexHelper;
import org.hibernate.metamodel.source.internal.MetadataImpl;
import org.hibernate.metamodel.source.spi.MetadataImplementor;
public class QueryBinder {
@ -65,8 +65,7 @@ public class QueryBinder {
* @param metadata the global metadata
* @param jandex the jandex index
*/
public static void bind(MetadataImpl metadata,
Index jandex) {
public static void bind(MetadataImplementor metadata, Index jandex) {
for ( AnnotationInstance query : jandex.getAnnotations( JPADotNames.NAMED_QUERY ) ) {
bindNamedQuery( metadata, query );
}
@ -101,8 +100,7 @@ public class QueryBinder {
}
}
private static void bindNamedQuery(MetadataImpl metadata,
AnnotationInstance annotation) {
private static void bindNamedQuery(MetadataImplementor metadata, AnnotationInstance annotation) {
String name = JandexHelper.getValueAsString( annotation, "name" );
if ( StringHelper.isEmpty( name ) ) {
throw new AnnotationException( "A named query must have a name when used in class or package level" );
@ -137,8 +135,7 @@ public class QueryBinder {
LOG.debugf( "Binding named query: %s => %s", name, query );
}
private static void bindNamedNativeQuery(MetadataImpl metadata,
AnnotationInstance annotation) {
private static void bindNamedNativeQuery(MetadataImplementor metadata, AnnotationInstance annotation) {
String name = JandexHelper.getValueAsString( annotation, "name" );
if ( StringHelper.isEmpty( name ) ) {
throw new AnnotationException( "A named native query must have a name when used in class or package level" );

View File

@ -37,7 +37,7 @@ import org.hibernate.metamodel.relational.SimpleValue;
import org.hibernate.metamodel.relational.Table;
import org.hibernate.metamodel.source.annotations.HibernateDotNames;
import org.hibernate.metamodel.source.annotations.util.JandexHelper;
import org.hibernate.metamodel.source.internal.MetadataImpl;
import org.hibernate.metamodel.source.spi.MetadataImplementor;
/**
* Binds table related information. This binder is called after the entities are bound.
@ -58,8 +58,7 @@ public class TableBinder {
* @param metadata the global metadata
* @param jandex the annotation index repository
*/
public static void bind(MetadataImpl metadata,
Index jandex) {
public static void bind(MetadataImplementor metadata, Index jandex) {
for ( AnnotationInstance tableAnnotation : jandex.getAnnotations( HibernateDotNames.TABLE ) ) {
bind( metadata, tableAnnotation );
}
@ -70,8 +69,7 @@ public class TableBinder {
}
}
private static void bind(MetadataImpl metadata,
AnnotationInstance tableAnnotation) {
private static void bind(MetadataImplementor metadata, AnnotationInstance tableAnnotation) {
String tableName = JandexHelper.getValueAsString( tableAnnotation, "appliesTo" );
ObjectName objectName = new ObjectName( tableName );
Schema schema = metadata.getDatabase().getSchema( objectName.getSchema(), objectName.getCatalog() );
@ -81,8 +79,7 @@ public class TableBinder {
}
}
private static void bindHibernateTableAnnotation(Table table,
AnnotationInstance tableAnnotation) {
private static void bindHibernateTableAnnotation(Table table, AnnotationInstance tableAnnotation) {
for ( AnnotationInstance indexAnnotation : JandexHelper.getValueAsArray( tableAnnotation, "indexes" ) ) {
bindIndexAnnotation( table, indexAnnotation );
}
@ -92,8 +89,7 @@ public class TableBinder {
}
}
private static void bindIndexAnnotation(Table table,
AnnotationInstance indexAnnotation) {
private static void bindIndexAnnotation(Table table, AnnotationInstance indexAnnotation) {
String indexName = JandexHelper.getValueAsString( indexAnnotation, "appliesTo" );
String[] columnNames = (String[]) JandexHelper.getValue( indexAnnotation, "columnNames" );
if ( columnNames == null ) {
@ -110,8 +106,7 @@ public class TableBinder {
}
}
private static Column findColumn(Table table,
String columnName) {
private static Column findColumn(Table table, String columnName) {
Column column = null;
for ( SimpleValue value : table.values() ) {
if ( value instanceof Column && ( (Column) value ).getName().equals( columnName ) ) {

View File

@ -37,7 +37,7 @@ import org.hibernate.internal.util.StringHelper;
import org.hibernate.metamodel.binding.TypeDef;
import org.hibernate.metamodel.source.annotations.HibernateDotNames;
import org.hibernate.metamodel.source.annotations.util.JandexHelper;
import org.hibernate.metamodel.source.internal.MetadataImpl;
import org.hibernate.metamodel.source.spi.MetadataImplementor;
public class TypeDefBinder {
@ -52,8 +52,7 @@ public class TypeDefBinder {
* @param metadata the global metadata
* @param jandex the jandex jandex
*/
public static void bind(MetadataImpl metadata,
Index jandex) {
public static void bind(MetadataImplementor metadata, Index jandex) {
for ( AnnotationInstance typeDef : jandex.getAnnotations( HibernateDotNames.TYPE_DEF ) ) {
bind( metadata, typeDef );
}
@ -64,8 +63,7 @@ public class TypeDefBinder {
}
}
private static void bind(MetadataImpl metadata,
AnnotationInstance typeDef) {
private static void bind(MetadataImplementor metadata, AnnotationInstance typeDef) {
String name = JandexHelper.getValueAsString( typeDef, "name" );
String defaultForType = JandexHelper.getValueAsString( typeDef, "defaultForType" );
String typeClass = JandexHelper.getValueAsString( typeDef, "typeClass" );
@ -89,10 +87,11 @@ public class TypeDefBinder {
}
}
private static void bind(String name,
private static void bind(
String name,
String typeClass,
Map<String, String> prms,
MetadataImpl metadata) {
MetadataImplementor metadata) {
LOG.debugf( "Binding type definition: %s", name );
metadata.addTypeDefinition( new TypeDef( name, typeClass, prms ) );
}

View File

@ -76,6 +76,7 @@ import org.hibernate.metamodel.binding.state.PluralAttributeBindingState;
import org.hibernate.metamodel.binding.state.SimpleAttributeBindingState;
import org.hibernate.metamodel.relational.state.TupleRelationalState;
import org.hibernate.metamodel.relational.state.ValueRelationalState;
import org.hibernate.metamodel.source.spi.BindingContext;
import org.hibernate.metamodel.source.spi.MetadataImplementor;
/**
@ -84,28 +85,27 @@ import org.hibernate.metamodel.source.spi.MetadataImplementor;
* @author Steve Ebersole
*/
abstract class AbstractEntityBinder {
private final HbmBinder hibernateMappingBinder;
private final HbmBindingContext bindingContext;
private final Schema.Name schemaName;
AbstractEntityBinder(HbmBinder hibernateMappingBinder,
XMLHibernateMapping.XMLClass entityClazz) {
this.hibernateMappingBinder = hibernateMappingBinder;
AbstractEntityBinder(HbmBindingContext bindingContext, XMLHibernateMapping.XMLClass entityClazz) {
this.bindingContext = bindingContext;
this.schemaName = new Schema.Name(
( entityClazz.getSchema() == null ?
hibernateMappingBinder.getMappingDefaults().getDefaultSchemaName() :
entityClazz.getSchema() ),
( entityClazz.getCatalog() == null ?
hibernateMappingBinder.getMappingDefaults().getDefaultCatalogName() :
entityClazz.getCatalog() )
entityClazz.getSchema() == null
? bindingContext.getMappingDefaults().getDefaultSchemaName()
: entityClazz.getSchema(),
entityClazz.getCatalog() == null
? bindingContext.getMappingDefaults().getDefaultCatalogName() :
entityClazz.getCatalog()
);
}
public HbmBinder getHibernateMappingBinder() {
return hibernateMappingBinder;
public HbmBindingContext getBindingContext() {
return bindingContext;
}
protected MetadataImplementor getMetadata() {
return hibernateMappingBinder.getMetadata();
return bindingContext.getMetadataImplementor();
}
protected Schema.Name getSchemaName() {
@ -116,13 +116,14 @@ abstract class AbstractEntityBinder {
return getMetadata().getOptions().getNamingStrategy();
}
protected void basicEntityBinding(XMLHibernateMapping.XMLClass entityClazz,
protected void basicEntityBinding(
XMLHibernateMapping.XMLClass entityClazz,
EntityBinding entityBinding,
Hierarchical superType) {
entityBinding.fromHbmXml(
hibernateMappingBinder,
bindingContext,
entityClazz,
new Entity( hibernateMappingBinder.extractEntityName( entityClazz ), superType )
new Entity( bindingContext.extractEntityName( entityClazz ), superType )
);
// TODO: move this stuff out
// transfer an explicitly defined lazy attribute
@ -133,11 +134,11 @@ abstract class AbstractEntityBinder {
final String entityName = entityBinding.getEntity().getName();
if ( entityClazz.getFetchProfile() != null ) {
hibernateMappingBinder.bindFetchProfiles( entityClazz.getFetchProfile(), entityName );
bindingContext.bindFetchProfiles( entityClazz.getFetchProfile(), entityName );
}
getMetadata().addImport( entityName, entityName );
if ( hibernateMappingBinder.isAutoImport() ) {
if ( bindingContext.isAutoImport() ) {
if ( entityName.indexOf( '.' ) > 0 ) {
getMetadata().addImport( StringHelper.unqualify( entityName ), entityName );
}
@ -145,12 +146,12 @@ abstract class AbstractEntityBinder {
}
protected String getDefaultAccess() {
return hibernateMappingBinder.getMappingDefaults().getDefaultAccess();
return bindingContext.getMappingDefaults().getDefaultAccess();
}
private void bindPojoRepresentation(XMLHibernateMapping.XMLClass entityClazz,
EntityBinding entityBinding) {
String className = hibernateMappingBinder.getClassName( entityClazz.getName() );
String className = bindingContext.getClassName( entityClazz.getName() );
String proxyName = entityBinding.getProxyInterfaceName();
entityBinding.getEntity().getPojoEntitySpecifics().setClassName( className );
@ -291,7 +292,7 @@ abstract class AbstractEntityBinder {
if ( XMLBagElement.class.isInstance( attribute ) ) {
XMLBagElement collection = XMLBagElement.class.cast( attribute );
BagBinding collectionBinding = makeBagAttributeBinding( collection, entityBinding );
hibernateMappingBinder.getMetadata().addCollection( collectionBinding );
bindingContext.getMetadataImplementor().addCollection( collectionBinding );
attributeBinding = collectionBinding;
}
else if ( XMLIdbagElement.class.isInstance( attribute ) ) {
@ -444,7 +445,7 @@ PrimitiveArray
EntityBinding entityBinding) {
SimpleAttributeBindingState bindingState = new HbmSimpleAttributeBindingState(
entityBinding.getEntity().getPojoEntitySpecifics().getClassName(),
hibernateMappingBinder,
bindingContext,
entityBinding.getMetaAttributeContext(),
property
);
@ -453,7 +454,7 @@ PrimitiveArray
ValueRelationalState relationalState =
convertToSimpleValueRelationalStateIfPossible(
new HbmSimpleValueRelationalStateContainer(
hibernateMappingBinder,
bindingContext,
true,
property
)
@ -483,7 +484,7 @@ PrimitiveArray
PluralAttributeBindingState bindingState =
new HbmPluralAttributeBindingState(
entityBinding.getEntity().getPojoEntitySpecifics().getClassName(),
hibernateMappingBinder,
bindingContext,
entityBinding.getMetaAttributeContext(),
collection
);
@ -523,7 +524,7 @@ PrimitiveArray
ManyToOneAttributeBindingState bindingState =
new HbmManyToOneAttributeBindingState(
entityBinding.getEntity().getPojoEntitySpecifics().getClassName(),
hibernateMappingBinder,
bindingContext,
entityBinding.getMetaAttributeContext(),
manyToOne
);
@ -531,7 +532,7 @@ PrimitiveArray
// boolean (true here) indicates that by default column names should be guessed
ManyToOneRelationalState relationalState =
new HbmManyToOneRelationalStateContainer(
hibernateMappingBinder,
bindingContext,
true,
manyToOne
);

View File

@ -23,362 +23,62 @@
*/
package org.hibernate.metamodel.source.hbm;
import java.util.HashMap;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.engine.spi.FilterDefinition;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.metamodel.binding.FetchProfile;
import org.hibernate.metamodel.binding.FetchProfile.Fetch;
import org.hibernate.metamodel.binding.TypeDef;
import org.hibernate.metamodel.relational.AuxiliaryDatabaseObject;
import org.hibernate.metamodel.relational.BasicAuxiliaryDatabaseObjectImpl;
import org.hibernate.metamodel.source.MappingException;
import org.hibernate.metamodel.source.Origin;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLFetchProfileElement;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLFetchProfileElement.XMLFetch;
import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLClass;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLImport;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLJoinedSubclassElement;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLParamElement;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLQueryElement;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLSqlQueryElement;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLSubclassElement;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLUnionSubclassElement;
import org.hibernate.metamodel.source.internal.JaxbRoot;
import org.hibernate.metamodel.source.internal.OverriddenMappingDefaults;
import org.hibernate.metamodel.source.spi.BindingContext;
import org.hibernate.metamodel.source.spi.MappingDefaults;
import org.hibernate.metamodel.source.spi.MetaAttributeContext;
import org.hibernate.metamodel.source.spi.Binder;
import org.hibernate.metamodel.source.spi.MetadataImplementor;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.classloading.spi.ClassLoaderService;
import org.hibernate.service.classloading.spi.ClassLoadingException;
import org.hibernate.type.Type;
/**
* Responsible for performing binding of hbm xml.
*/
public class HbmBinder implements BindingContext {
private final JaxbRoot<XMLHibernateMapping> jaxbRoot;
private final XMLHibernateMapping hibernateMapping;
private final MappingDefaults mappingDefaults;
private final MetaAttributeContext metaAttributeContext;
private final boolean autoImport;
public class HbmBinder implements Binder {
private final MetadataImplementor metadata;
private List<HibernateMappingProcessor> processors;
public HbmBinder(MetadataImplementor metadata, JaxbRoot<XMLHibernateMapping> jaxbRoot) {
this.jaxbRoot = jaxbRoot;
this.hibernateMapping = jaxbRoot.getRoot();
public HbmBinder(MetadataImplementor metadata) {
this.metadata = metadata;
this.mappingDefaults = new OverriddenMappingDefaults(
metadata.getMappingDefaults(),
hibernateMapping.getPackage(),
hibernateMapping.getSchema(),
hibernateMapping.getCatalog(),
null,
null,
hibernateMapping.getDefaultCascade(),
hibernateMapping.getDefaultAccess(),
hibernateMapping.isDefaultLazy()
);
autoImport = hibernateMapping.isAutoImport();
metaAttributeContext = extractMetaAttributes();
}
private MetaAttributeContext extractMetaAttributes() {
return hibernateMapping.getMeta() == null
? new MetaAttributeContext( metadata.getMetaAttributeContext() )
: HbmHelper.extractMetaAttributeContext( hibernateMapping.getMeta(), true, metadata.getMetaAttributeContext() );
}
public MetadataImplementor getMetadata() {
return metadata;
}
Origin getOrigin() {
return jaxbRoot.getOrigin();
}
@Override
public ServiceRegistry getServiceRegistry() {
return metadata.getServiceRegistry();
@SuppressWarnings( {"unchecked"})
public void prepare(MetadataSources sources) {
this.processors = new ArrayList<HibernateMappingProcessor>();
for ( JaxbRoot jaxbRoot : sources.getJaxbRootList() ) {
if ( jaxbRoot.getRoot() instanceof XMLHibernateMapping ) {
processors.add( new HibernateMappingProcessor( metadata, (JaxbRoot<XMLHibernateMapping>) jaxbRoot ) );
}
}
}
@Override
public NamingStrategy getNamingStrategy() {
return metadata.getOptions().getNamingStrategy();
public void bindIndependentMetadata(MetadataSources sources) {
for ( HibernateMappingProcessor processor : processors ) {
processor.bindIndependentMetadata();
}
}
@Override
public MappingDefaults getMappingDefaults() {
return mappingDefaults;
public void bindTypeDependentMetadata(MetadataSources sources) {
for ( HibernateMappingProcessor processor : processors ) {
processor.bindTypeDependentMetadata();
}
}
@Override
public MetaAttributeContext getMetaAttributeContext() {
return metaAttributeContext;
public void bindMappingMetadata(MetadataSources sources, List<String> processedEntityNames) {
for ( HibernateMappingProcessor processor : processors ) {
processor.bindMappingMetadata( processedEntityNames );
}
}
@Override
public MetadataImplementor getMetadataImplementor() {
return metadata;
public void bindMappingDependentMetadata(MetadataSources sources) {
for ( HibernateMappingProcessor processor : processors ) {
processor.bindMappingDependentMetadata();
}
boolean isAutoImport() {
return autoImport;
}
public void processHibernateMapping() {
// no pre-requisites
bindDatabaseObjectDefinitions();
// no pre-requisites
bindTypeDefinitions();
// potentially depends on type definitions
bindFilterDefinitions();
// potentially depends on type definitions
bindIdentifierGenerators();
// potentially depends on type definitions and identifier generators
bindMappings();
// depends on mappings
bindFetchProfiles();
// depends on mappings
bindImports();
// depends on mappings and potentially on type definitions
bindResultSetMappings();
// depends on mappings and potentially on type definitions and result set mappings
bindNamedQueries();
}
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 bindFilterDefinitions() {
if(hibernateMapping.getFilterDef() == null){
return;
}
for ( XMLHibernateMapping.XMLFilterDef filterDefinition : hibernateMapping.getFilterDef() ) {
final String name = filterDefinition.getName();
final Map<String,Type> parameters = new HashMap<String, Type>();
String condition = null;
for ( Object o : filterDefinition.getContent() ) {
if ( o instanceof String ) {
// represents the condition
if ( condition != null ) {
// log?
}
condition = (String) o;
}
else if ( o instanceof XMLHibernateMapping.XMLFilterDef.XMLFilterParam ) {
final XMLHibernateMapping.XMLFilterDef.XMLFilterParam paramElement = (XMLHibernateMapping.XMLFilterDef.XMLFilterParam) o;
// todo : should really delay this resolution until later to allow typedef names
parameters.put(
paramElement.getName(),
metadata.getTypeResolver().heuristicType( paramElement.getType() )
);
}
else {
throw new MappingException( "Unrecognized nested filter content", jaxbRoot.getOrigin() );
}
}
if ( condition == null ) {
condition = filterDefinition.getCondition();
}
metadata.addFilterDefinition( new FilterDefinition( name, condition, parameters ) );
}
}
private void bindIdentifierGenerators() {
if ( hibernateMapping.getIdentifierGenerator() == null ) {
return;
}
for ( XMLHibernateMapping.XMLIdentifierGenerator identifierGeneratorElement : hibernateMapping.getIdentifierGenerator() ) {
metadata.registerIdentifierGenerator(
identifierGeneratorElement.getName(),
identifierGeneratorElement.getClazz()
);
}
}
private void bindMappings() {
if ( hibernateMapping.getClazzOrSubclassOrJoinedSubclass() == null ) {
return;
}
for ( Object clazzOrSubclass : hibernateMapping.getClazzOrSubclassOrJoinedSubclass() ) {
if ( XMLClass.class.isInstance( clazzOrSubclass ) ) {
XMLClass clazz =
XMLClass.class.cast( clazzOrSubclass );
new RootEntityBinder( this, clazz ).process( clazz );
}
else if ( XMLSubclassElement.class.isInstance( clazzOrSubclass ) ) {
// PersistentClass superModel = getSuperclass( mappings, element );
// handleSubclass( superModel, mappings, element, inheritedMetas );
}
else if ( XMLJoinedSubclassElement.class.isInstance( clazzOrSubclass ) ) {
// PersistentClass superModel = getSuperclass( mappings, element );
// handleJoinedSubclass( superModel, mappings, element, inheritedMetas );
}
else if ( XMLUnionSubclassElement.class.isInstance( clazzOrSubclass ) ) {
// PersistentClass superModel = getSuperclass( mappings, element );
// handleUnionSubclass( superModel, mappings, element, inheritedMetas );
}
else {
throw new org.hibernate.metamodel.source.MappingException(
"unknown type of class or subclass: " +
clazzOrSubclass.getClass().getName(), jaxbRoot.getOrigin()
);
}
}
}
private void bindFetchProfiles(){
if(hibernateMapping.getFetchProfile() == null){
return;
}
bindFetchProfiles( hibernateMapping.getFetchProfile(),null );
}
protected void bindFetchProfiles(List<XMLFetchProfileElement> fetchProfiles, String containingEntityName) {
for ( XMLFetchProfileElement fetchProfile : fetchProfiles ) {
String profileName = fetchProfile.getName();
Set<Fetch> fetches = new HashSet<Fetch>();
for ( XMLFetch fetch : fetchProfile.getFetch() ) {
String entityName = fetch.getEntity() == null ? containingEntityName : fetch.getEntity();
if ( entityName == null ) {
throw new MappingException(
"could not determine entity for fetch-profile fetch [" + profileName + "]:[" +
fetch.getAssociation() + "]",
jaxbRoot.getOrigin()
);
}
fetches.add( new Fetch( entityName, fetch.getAssociation(), fetch.getStyle() ) );
}
metadata.addFetchProfile( new FetchProfile( profileName, fetches ) );
}
}
private void bindImports() {
if ( hibernateMapping.getImport() == null ) {
return;
}
for ( XMLImport importValue : hibernateMapping.getImport() ) {
String className = getClassName( importValue.getClazz() );
String rename = importValue.getRename();
rename = ( rename == null ) ? StringHelper.unqualify( className ) : rename;
metadata.addImport( className, rename );
}
}
private void bindResultSetMappings() {
if ( hibernateMapping.getResultset() == null ) {
return;
}
// bindResultSetMappingDefinitions( element, null, mappings );
}
private void bindNamedQueries() {
if ( hibernateMapping.getQueryOrSqlQuery() == null ) {
return;
}
for ( Object queryOrSqlQuery : hibernateMapping.getQueryOrSqlQuery() ) {
if ( XMLQueryElement.class.isInstance( queryOrSqlQuery ) ) {
// bindNamedQuery( element, null, mappings );
}
else if ( XMLSqlQueryElement.class.isInstance( queryOrSqlQuery ) ) {
// bindNamedSQLQuery( element, null, mappings );
}
else {
throw new MappingException(
"unknown type of query: " +
queryOrSqlQuery.getClass().getName(), jaxbRoot.getOrigin()
);
}
}
}
private ClassLoaderService classLoaderService;
private ClassLoaderService classLoaderService() {
if ( classLoaderService == null ) {
classLoaderService = metadata.getServiceRegistry().getService( ClassLoaderService.class );
}
return classLoaderService;
}
String extractEntityName(XMLClass entityClazz) {
return HbmHelper.extractEntityName( entityClazz, mappingDefaults.getPackageName() );
}
String getClassName(String unqualifiedName) {
return HbmHelper.getClassName( unqualifiedName, mappingDefaults.getPackageName() );
}
}

View File

@ -25,42 +25,23 @@ package org.hibernate.metamodel.source.hbm;
import java.util.List;
import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.source.spi.Binder;
import org.hibernate.metamodel.source.spi.MetadataImplementor;
import org.hibernate.internal.util.xml.XmlDocument;
import org.hibernate.metamodel.source.Origin;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLFetchProfileElement;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping;
import org.hibernate.metamodel.source.spi.BindingContext;
/**
* @author Steve Ebersole
*/
public class HibernateMappingBinder implements Binder {
private final MetadataImplementor metadata;
public interface HbmBindingContext extends BindingContext {
public boolean isAutoImport();
public HibernateMappingBinder(MetadataImplementor metadata) {
this.metadata = metadata;
}
public String extractEntityName(XMLHibernateMapping.XMLClass entityClazz);
@Override
public void prepare(MetadataSources sources) {
// nothing to do here.
}
public String getClassName(String unqualifiedName);
@Override
public void bindIndependentMetadata(MetadataSources sources) {
// todo : implement method body
}
public void bindFetchProfiles(List<XMLFetchProfileElement> fetchProfiles, String containingEntityName);
@Override
public void bindTypeDependentMetadata(MetadataSources sources) {
// todo : implement method body
}
@Override
public void bindMappingMetadata(MetadataSources sources, List<String> processedEntityNames) {
// todo : implement method body
}
@Override
public void bindMappingDependentMetadata(MetadataSources sources) {
// todo : implement method body
}
public Origin getOrigin();
}

View File

@ -0,0 +1,369 @@
/*
* 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.source.hbm;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.engine.spi.FilterDefinition;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.metamodel.binding.FetchProfile;
import org.hibernate.metamodel.binding.TypeDef;
import org.hibernate.metamodel.relational.AuxiliaryDatabaseObject;
import org.hibernate.metamodel.relational.BasicAuxiliaryDatabaseObjectImpl;
import org.hibernate.metamodel.source.MappingException;
import org.hibernate.metamodel.source.Origin;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLFetchProfileElement;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLJoinedSubclassElement;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLParamElement;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLQueryElement;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLSqlQueryElement;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLSubclassElement;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLUnionSubclassElement;
import org.hibernate.metamodel.source.internal.JaxbRoot;
import org.hibernate.metamodel.source.internal.OverriddenMappingDefaults;
import org.hibernate.metamodel.source.spi.MappingDefaults;
import org.hibernate.metamodel.source.spi.MetaAttributeContext;
import org.hibernate.metamodel.source.spi.MetadataImplementor;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.classloading.spi.ClassLoaderService;
import org.hibernate.service.classloading.spi.ClassLoadingException;
import org.hibernate.type.Type;
/**
* Responsible for processing a {@code <hibernate-mapping/>} element. Allows processing to be coordinated across
* all hbm files in an ordered fashion. The order is essentially the same as defined in
* {@link org.hibernate.metamodel.source.spi.Binder}
*
* @author Steve Ebersole
*/
public class HibernateMappingProcessor implements HbmBindingContext {
private final MetadataImplementor metadata;
private final JaxbRoot<XMLHibernateMapping> jaxbRoot;
private final XMLHibernateMapping hibernateMapping;
private final MappingDefaults mappingDefaults;
private final MetaAttributeContext metaAttributeContext;
private final boolean autoImport;
public HibernateMappingProcessor(MetadataImplementor metadata, JaxbRoot<XMLHibernateMapping> jaxbRoot) {
this.metadata = metadata;
this.jaxbRoot = jaxbRoot;
this.hibernateMapping = jaxbRoot.getRoot();
this.mappingDefaults = new OverriddenMappingDefaults(
metadata.getMappingDefaults(),
hibernateMapping.getPackage(),
hibernateMapping.getSchema(),
hibernateMapping.getCatalog(),
null,
null,
hibernateMapping.getDefaultCascade(),
hibernateMapping.getDefaultAccess(),
hibernateMapping.isDefaultLazy()
);
autoImport = hibernateMapping.isAutoImport();
metaAttributeContext = extractMetaAttributes();
}
private MetaAttributeContext extractMetaAttributes() {
return hibernateMapping.getMeta() == null
? new MetaAttributeContext( metadata.getMetaAttributeContext() )
: HbmHelper.extractMetaAttributeContext( hibernateMapping.getMeta(), true, metadata.getMetaAttributeContext() );
}
@Override
public boolean isAutoImport() {
return autoImport;
}
@Override
public Origin getOrigin() {
return jaxbRoot.getOrigin();
}
@Override
public ServiceRegistry getServiceRegistry() {
return metadata.getServiceRegistry();
}
@Override
public NamingStrategy getNamingStrategy() {
return metadata.getOptions().getNamingStrategy();
}
@Override
public MappingDefaults getMappingDefaults() {
return mappingDefaults;
}
@Override
public MetaAttributeContext getMetaAttributeContext() {
return metaAttributeContext;
}
@Override
public MetadataImplementor getMetadataImplementor() {
return metadata;
}
public void bindIndependentMetadata() {
bindDatabaseObjectDefinitions();
bindTypeDefinitions();
}
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 ) );
}
}
public void bindTypeDependentMetadata() {
bindFilterDefinitions();
bindIdentifierGenerators();
}
private void bindFilterDefinitions() {
if(hibernateMapping.getFilterDef() == null){
return;
}
for ( XMLHibernateMapping.XMLFilterDef filterDefinition : hibernateMapping.getFilterDef() ) {
final String name = filterDefinition.getName();
final Map<String,Type> parameters = new HashMap<String, Type>();
String condition = null;
for ( Object o : filterDefinition.getContent() ) {
if ( o instanceof String ) {
// represents the condition
if ( condition != null ) {
// log?
}
condition = (String) o;
}
else if ( o instanceof XMLHibernateMapping.XMLFilterDef.XMLFilterParam ) {
final XMLHibernateMapping.XMLFilterDef.XMLFilterParam paramElement = (XMLHibernateMapping.XMLFilterDef.XMLFilterParam) o;
// todo : should really delay this resolution until later to allow typedef names
parameters.put(
paramElement.getName(),
metadata.getTypeResolver().heuristicType( paramElement.getType() )
);
}
else {
throw new MappingException( "Unrecognized nested filter content", jaxbRoot.getOrigin() );
}
}
if ( condition == null ) {
condition = filterDefinition.getCondition();
}
metadata.addFilterDefinition( new FilterDefinition( name, condition, parameters ) );
}
}
private void bindIdentifierGenerators() {
if ( hibernateMapping.getIdentifierGenerator() == null ) {
return;
}
for ( XMLHibernateMapping.XMLIdentifierGenerator identifierGeneratorElement : hibernateMapping.getIdentifierGenerator() ) {
metadata.registerIdentifierGenerator(
identifierGeneratorElement.getName(),
identifierGeneratorElement.getClazz()
);
}
}
public void bindMappingMetadata(List<String> processedEntityNames) {
if ( hibernateMapping.getClazzOrSubclassOrJoinedSubclass() == null ) {
return;
}
for ( Object clazzOrSubclass : hibernateMapping.getClazzOrSubclassOrJoinedSubclass() ) {
if ( XMLHibernateMapping.XMLClass.class.isInstance( clazzOrSubclass ) ) {
XMLHibernateMapping.XMLClass clazz =
XMLHibernateMapping.XMLClass.class.cast( clazzOrSubclass );
new RootEntityBinder( this, clazz ).process( clazz );
}
else if ( XMLSubclassElement.class.isInstance( clazzOrSubclass ) ) {
// PersistentClass superModel = getSuperclass( mappings, element );
// handleSubclass( superModel, mappings, element, inheritedMetas );
}
else if ( XMLJoinedSubclassElement.class.isInstance( clazzOrSubclass ) ) {
// PersistentClass superModel = getSuperclass( mappings, element );
// handleJoinedSubclass( superModel, mappings, element, inheritedMetas );
}
else if ( XMLUnionSubclassElement.class.isInstance( clazzOrSubclass ) ) {
// PersistentClass superModel = getSuperclass( mappings, element );
// handleUnionSubclass( superModel, mappings, element, inheritedMetas );
}
else {
throw new org.hibernate.metamodel.source.MappingException(
"unknown type of class or subclass: " +
clazzOrSubclass.getClass().getName(), jaxbRoot.getOrigin()
);
}
}
}
public void bindMappingDependentMetadata() {
bindFetchProfiles();
bindImports();
bindResultSetMappings();
bindNamedQueries();
}
private void bindFetchProfiles(){
if(hibernateMapping.getFetchProfile() == null){
return;
}
bindFetchProfiles( hibernateMapping.getFetchProfile(),null );
}
public void bindFetchProfiles(List<XMLFetchProfileElement> fetchProfiles, String containingEntityName) {
for ( XMLFetchProfileElement fetchProfile : fetchProfiles ) {
String profileName = fetchProfile.getName();
Set<FetchProfile.Fetch> fetches = new HashSet<FetchProfile.Fetch>();
for ( XMLFetchProfileElement.XMLFetch fetch : fetchProfile.getFetch() ) {
String entityName = fetch.getEntity() == null ? containingEntityName : fetch.getEntity();
if ( entityName == null ) {
throw new MappingException(
"could not determine entity for fetch-profile fetch [" + profileName + "]:[" +
fetch.getAssociation() + "]",
jaxbRoot.getOrigin()
);
}
fetches.add( new FetchProfile.Fetch( entityName, fetch.getAssociation(), fetch.getStyle() ) );
}
metadata.addFetchProfile( new FetchProfile( profileName, fetches ) );
}
}
private void bindImports() {
if ( hibernateMapping.getImport() == null ) {
return;
}
for ( XMLHibernateMapping.XMLImport importValue : hibernateMapping.getImport() ) {
String className = getClassName( importValue.getClazz() );
String rename = importValue.getRename();
rename = ( rename == null ) ? StringHelper.unqualify( className ) : rename;
metadata.addImport( className, rename );
}
}
private void bindResultSetMappings() {
if ( hibernateMapping.getResultset() == null ) {
return;
}
// bindResultSetMappingDefinitions( element, null, mappings );
}
private void bindNamedQueries() {
if ( hibernateMapping.getQueryOrSqlQuery() == null ) {
return;
}
for ( Object queryOrSqlQuery : hibernateMapping.getQueryOrSqlQuery() ) {
if ( XMLQueryElement.class.isInstance( queryOrSqlQuery ) ) {
// bindNamedQuery( element, null, mappings );
}
else if ( XMLSqlQueryElement.class.isInstance( queryOrSqlQuery ) ) {
// bindNamedSQLQuery( element, null, mappings );
}
else {
throw new MappingException(
"unknown type of query: " +
queryOrSqlQuery.getClass().getName(), jaxbRoot.getOrigin()
);
}
}
}
private ClassLoaderService classLoaderService;
private ClassLoaderService classLoaderService() {
if ( classLoaderService == null ) {
classLoaderService = metadata.getServiceRegistry().getService( ClassLoaderService.class );
}
return classLoaderService;
}
@Override
public String extractEntityName(XMLHibernateMapping.XMLClass entityClazz) {
return HbmHelper.extractEntityName( entityClazz, mappingDefaults.getPackageName() );
}
@Override
public String getClassName(String unqualifiedName) {
return HbmHelper.getClassName( unqualifiedName, mappingDefaults.getPackageName() );
}
}

View File

@ -50,12 +50,12 @@ import org.hibernate.metamodel.binding.state.DiscriminatorBindingState;
*/
class RootEntityBinder extends AbstractEntityBinder {
RootEntityBinder(HbmBinder hibernateMappingBinder, XMLClass xmlClazz) {
super( hibernateMappingBinder, xmlClazz );
RootEntityBinder(HbmBindingContext bindingContext, XMLClass xmlClazz) {
super( bindingContext, xmlClazz );
}
public void process(XMLClass xmlClazz) {
String entityName = getHibernateMappingBinder().extractEntityName( xmlClazz );
String entityName = getBindingContext().extractEntityName( xmlClazz );
if ( entityName == null ) {
throw new MappingException( "Unable to determine entity name" );
}
@ -134,20 +134,20 @@ class RootEntityBinder extends AbstractEntityBinder {
throw new InvalidMappingException(
"Entity [" + entityBinding.getEntity().getName() + "] did not contain identifier mapping",
getHibernateMappingBinder().getOrigin()
getBindingContext().getOrigin()
);
}
private void bindSimpleId(XMLId id, EntityBinding entityBinding) {
SimpleAttributeBindingState bindingState = new HbmSimpleAttributeBindingState(
entityBinding.getEntity().getPojoEntitySpecifics().getClassName(),
getHibernateMappingBinder(),
getBindingContext(),
entityBinding.getMetaAttributeContext(),
id
);
// boolean (true here) indicates that by default column names should be guessed
HbmSimpleValueRelationalStateContainer relationalStateContainer = new HbmSimpleValueRelationalStateContainer(
getHibernateMappingBinder(), true, id
getBindingContext(), true, id
);
if ( relationalStateContainer.getRelationalStates().size() > 1 ) {
throw new MappingException( "ID is expected to be a single column, but has more than 1 value" );
@ -247,14 +247,14 @@ class RootEntityBinder extends AbstractEntityBinder {
DiscriminatorBindingState bindingState = new HbmDiscriminatorBindingState(
entityBinding.getEntity().getPojoEntitySpecifics().getClassName(),
entityBinding.getEntity().getName(),
getHibernateMappingBinder(),
getBindingContext(),
xmlEntityClazz
);
// boolean (true here) indicates that by default column names should be guessed
ValueRelationalState relationalState = convertToSimpleValueRelationalStateIfPossible(
new HbmSimpleValueRelationalStateContainer(
getHibernateMappingBinder(),
getBindingContext(),
true,
xmlEntityClazz.getDiscriminator()
)
@ -288,7 +288,7 @@ class RootEntityBinder extends AbstractEntityBinder {
SimpleAttributeBindingState bindingState =
new HbmSimpleAttributeBindingState(
entityBinding.getEntity().getPojoEntitySpecifics().getClassName(),
getHibernateMappingBinder(),
getBindingContext(),
entityBinding.getMetaAttributeContext(),
version
);
@ -297,7 +297,7 @@ class RootEntityBinder extends AbstractEntityBinder {
ValueRelationalState relationalState =
convertToSimpleValueRelationalStateIfPossible(
new HbmSimpleValueRelationalStateContainer(
getHibernateMappingBinder(),
getBindingContext(),
true,
version
)
@ -315,7 +315,7 @@ class RootEntityBinder extends AbstractEntityBinder {
SimpleAttributeBindingState bindingState =
new HbmSimpleAttributeBindingState(
entityBinding.getEntity().getPojoEntitySpecifics().getClassName(),
getHibernateMappingBinder(),
getBindingContext(),
entityBinding.getMetaAttributeContext(),
timestamp
);
@ -325,7 +325,7 @@ class RootEntityBinder extends AbstractEntityBinder {
ValueRelationalState relationalState =
convertToSimpleValueRelationalStateIfPossible(
new HbmSimpleValueRelationalStateContainer(
getHibernateMappingBinder(),
getBindingContext(),
true,
timestamp
)

View File

@ -23,8 +23,6 @@
*/
package org.hibernate.metamodel.source.internal;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
@ -32,12 +30,9 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jboss.jandex.Index;
import org.jboss.jandex.Indexer;
import org.jboss.logging.Logger;
import org.hibernate.DuplicateMappingException;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.engine.spi.FilterDefinition;
@ -55,13 +50,8 @@ import org.hibernate.metamodel.binding.TypeDef;
import org.hibernate.metamodel.domain.MetaAttribute;
import org.hibernate.metamodel.relational.AuxiliaryDatabaseObject;
import org.hibernate.metamodel.relational.Database;
import org.hibernate.metamodel.source.annotation.xml.XMLEntityMappings;
import org.hibernate.metamodel.source.annotations.AnnotationBinder;
import org.hibernate.metamodel.source.annotations.JpaBinder;
import org.hibernate.metamodel.source.annotations.xml.OrmXmlParser;
import org.hibernate.metamodel.source.hbm.HbmBinder;
import org.hibernate.metamodel.source.hbm.HibernateMappingBinder;
import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping;
import org.hibernate.metamodel.source.spi.Binder;
import org.hibernate.metamodel.source.spi.MappingDefaults;
import org.hibernate.metamodel.source.spi.MetaAttributeContext;
@ -119,33 +109,26 @@ public class MetadataImpl implements MetadataImplementor, Serializable {
final Binder[] binders;
if ( options.getSourceProcessingOrder() == SourceProcessingOrder.HBM_FIRST ) {
binders = new Binder[] {
new HibernateMappingBinder( this ),
new JpaBinder( this )
new HbmBinder( this ),
new AnnotationBinder( this )
};
}
else {
binders = new Binder[] {
new JpaBinder( this ),
new HibernateMappingBinder( this )
new AnnotationBinder( this ),
new HbmBinder( this )
};
}
final ArrayList<String> processedEntityNames = new ArrayList<String>();
prepare( binders, metadataSources );
bindIndependentMetadata( binders, metadataSources );
bindTypeDependentMetadata( binders, metadataSources );
bindMappingMetadata( binders, metadataSources );
bindMappingMetadata( binders, metadataSources, processedEntityNames );
bindMappingDependentMetadata( binders, metadataSources );
final ArrayList<String> processedEntityNames = new ArrayList<String>();
if ( options.getSourceProcessingOrder() == SourceProcessingOrder.HBM_FIRST ) {
applyHibernateMappings( metadataSources, processedEntityNames );
applyAnnotationMappings( metadataSources, processedEntityNames );
}
else {
applyAnnotationMappings( metadataSources, processedEntityNames );
applyHibernateMappings( metadataSources, processedEntityNames );
}
// todo : remove this by coordinated ordering of entity processing
new EntityReferenceResolver( this ).resolve();
}
@ -155,21 +138,6 @@ public class MetadataImpl implements MetadataImplementor, Serializable {
}
}
private Index prepareIndex(MetadataSources metadataSources) {
// create a jandex index from the annotated classes
Indexer indexer = new Indexer();
for ( Class<?> clazz : metadataSources.getAnnotatedClasses() ) {
indexClass( indexer, clazz.getName().replace( '.', '/' ) + ".class" );
}
// add package-info from the configured packages
for ( String packageName : metadataSources.getAnnotatedPackages() ) {
indexClass( indexer, packageName.replace( '.', '/' ) + "/package-info.class" );
}
return indexer.complete();
}
private void bindIndependentMetadata(Binder[] binders, MetadataSources metadataSources) {
for ( Binder binder : binders ) {
binder.bindIndependentMetadata( metadataSources );
@ -182,8 +150,7 @@ public class MetadataImpl implements MetadataImplementor, Serializable {
}
}
private void bindMappingMetadata(Binder[] binders, MetadataSources metadataSources) {
final ArrayList<String> processedEntityNames = new ArrayList<String>();
private void bindMappingMetadata(Binder[] binders, MetadataSources metadataSources, List<String> processedEntityNames) {
for ( Binder binder : binders ) {
binder.bindMappingMetadata( metadataSources, processedEntityNames );
}
@ -209,9 +176,11 @@ public class MetadataImpl implements MetadataImplementor, Serializable {
return filterDefs.values();
}
@Override
public void addIdGenerator(IdGenerator generator) {
idGenerators.put( generator.getName(), generator );
}
@Override
public IdGenerator getIdGenerator(String name) {
if ( name == null ) {
@ -229,6 +198,7 @@ public class MetadataImpl implements MetadataImplementor, Serializable {
auxiliaryDatabaseObjects.add( auxiliaryDatabaseObject );
}
@Override
public void addNamedNativeQuery(String name, NamedSQLQueryDefinition def) {
namedNativeQueryDefs.put( name, def );
}
@ -240,6 +210,7 @@ public class MetadataImpl implements MetadataImplementor, Serializable {
return namedNativeQueryDefs.get( name );
}
@Override
public void addNamedQuery(String name, NamedQueryDefinition def) {
namedQueryDefs.put( name, def );
}
@ -268,64 +239,6 @@ public class MetadataImpl implements MetadataImplementor, Serializable {
return typeDefs.get( name );
}
private void applyHibernateMappings(MetadataSources metadataSources, List<String> processedEntityNames) {
for ( JaxbRoot jaxbRoot : metadataSources.getJaxbRootList() ) {
// filter to just hbm-based roots
if ( jaxbRoot.getRoot() instanceof XMLHibernateMapping ) {
final HbmBinder mappingBinder = new HbmBinder( this, jaxbRoot );
mappingBinder.processHibernateMapping();
}
}
}
private void applyAnnotationMappings(MetadataSources metadataSources, List<String> processedEntityNames) {
// create a jandex index from the annotated classes
Indexer indexer = new Indexer();
for ( Class<?> clazz : metadataSources.getAnnotatedClasses() ) {
indexClass( indexer, clazz.getName().replace( '.', '/' ) + ".class" );
}
// add package-info from the configured packages
for ( String packageName : metadataSources.getAnnotatedPackages() ) {
indexClass( indexer, packageName.replace( '.', '/' ) + "/package-info.class" );
}
Index index = indexer.complete();
List<JaxbRoot<XMLEntityMappings>> mappings = new ArrayList<JaxbRoot<XMLEntityMappings>>();
for ( JaxbRoot<?> root : metadataSources.getJaxbRootList() ) {
if ( root.getRoot() instanceof XMLEntityMappings ) {
mappings.add( (JaxbRoot<XMLEntityMappings>) root );
}
}
if ( !mappings.isEmpty() ) {
// process the xml configuration
final OrmXmlParser ormParser = new OrmXmlParser( this );
index = ormParser.parseAndUpdateIndex( mappings, index );
}
// create the annotation binder and pass it the final annotation index
final AnnotationBinder annotationBinder = new AnnotationBinder( this, index );
annotationBinder.bind();
}
/**
* Adds a single class to the jandex index
*
* @param indexer the jandex indexer
* @param className the fully qualified name of the class
*/
private void indexClass(Indexer indexer, String className) {
InputStream stream = classLoaderService().locateResourceStream( className );
try {
indexer.index( stream );
}
catch ( IOException e ) {
throw new HibernateException( "Unable to open input stream for class " + className, e );
}
}
private ClassLoaderService classLoaderService(){
if(classLoaderService==null){
classLoaderService = serviceRegistry.getService( ClassLoaderService.class );

View File

@ -24,9 +24,12 @@
package org.hibernate.metamodel.source.spi;
import org.hibernate.engine.spi.FilterDefinition;
import org.hibernate.engine.spi.NamedQueryDefinition;
import org.hibernate.engine.spi.NamedSQLQueryDefinition;
import org.hibernate.metamodel.Metadata;
import org.hibernate.metamodel.binding.EntityBinding;
import org.hibernate.metamodel.binding.FetchProfile;
import org.hibernate.metamodel.binding.IdGenerator;
import org.hibernate.metamodel.binding.PluralAttributeBinding;
import org.hibernate.metamodel.binding.TypeDef;
import org.hibernate.metamodel.relational.AuxiliaryDatabaseObject;
@ -56,7 +59,13 @@ public interface MetadataImplementor extends Metadata, BindingContext {
public void addFilterDefinition(FilterDefinition filterDefinition);
public void addIdGenerator(IdGenerator generator);
public void registerIdentifierGenerator(String name, String clazz);
public void addNamedNativeQuery(String name, NamedSQLQueryDefinition def);
public void addNamedQuery(String name, NamedQueryDefinition def);
public void addAuxiliaryDatabaseObject(AuxiliaryDatabaseObject auxiliaryDatabaseObject);
}