HHH-2578 - redesign SessionFactory building

This commit is contained in:
Steve Ebersole 2011-05-02 11:47:28 -05:00
parent 99cec1404f
commit cfb4f1ded8
12 changed files with 169 additions and 30 deletions

View File

@ -22,15 +22,11 @@
* Boston, MA 02110-1301 USA * Boston, MA 02110-1301 USA
*/ */
package org.hibernate.metamodel.source; package org.hibernate.metamodel;
/** /**
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public interface Metadata { public interface Metadata {
public static enum ProcessingOrder {
ANNOTATIONS_FIRST,
HBM_FIRST
}
} }

View File

@ -0,0 +1,35 @@
/*
* 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;
import org.hibernate.cfg.NamingStrategy;
/**
* @author Steve Ebersole
*/
public interface MetadataBuilder {
public MetadataBuilder with(NamingStrategy namingStrategy);
public MetadataBuilder with(SourceProcessingOrder sourceProcessingOrder);
public Metadata buildMetadata();
}

View File

@ -21,7 +21,7 @@
* 51 Franklin Street, Fifth Floor * 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA * Boston, MA 02110-1301 USA
*/ */
package org.hibernate.metamodel.source; package org.hibernate.metamodel;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
@ -43,9 +43,13 @@ import org.xml.sax.EntityResolver;
import org.hibernate.cfg.EJB3DTDEntityResolver; import org.hibernate.cfg.EJB3DTDEntityResolver;
import org.hibernate.cfg.EJB3NamingStrategy; import org.hibernate.cfg.EJB3NamingStrategy;
import org.hibernate.cfg.NamingStrategy; import org.hibernate.cfg.NamingStrategy;
import org.hibernate.metamodel.source.MappingException;
import org.hibernate.metamodel.source.MappingNotFoundException;
import org.hibernate.metamodel.source.Origin;
import org.hibernate.metamodel.source.SourceType;
import org.hibernate.metamodel.source.internal.JaxbHelper; import org.hibernate.metamodel.source.internal.JaxbHelper;
import org.hibernate.metamodel.source.internal.JaxbRoot; import org.hibernate.metamodel.source.internal.JaxbRoot;
import org.hibernate.metamodel.source.internal.MetadataImpl; import org.hibernate.metamodel.source.internal.MetadataBuilderImpl;
import org.hibernate.service.BasicServiceRegistry; import org.hibernate.service.BasicServiceRegistry;
import org.hibernate.service.classloading.spi.ClassLoaderService; import org.hibernate.service.classloading.spi.ClassLoaderService;
@ -97,12 +101,12 @@ public class MetadataSources {
return namingStrategy; return namingStrategy;
} }
public Metadata buildMetadata() { public MetadataBuilder getMetadataBuilder() {
return buildMetadata( Metadata.ProcessingOrder.ANNOTATIONS_FIRST ); return new MetadataBuilderImpl( this );
} }
public Metadata buildMetadata(Metadata.ProcessingOrder preferredProcessingOrder) { public Metadata buildMetadata() {
return new MetadataImpl( this, preferredProcessingOrder ); return getMetadataBuilder().buildMetadata();
} }
/** /**

View File

@ -0,0 +1,35 @@
/*
* 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;
/**
* Enumeration of the possible orders for processing metadata sources. The implication is in terms of precedence;
* for duplicate information in different sources, whichever is processed first has precedence.
*
* @author Steve Ebersole
*/
public enum SourceProcessingOrder {
ANNOTATIONS_FIRST,
HBM_FIRST
}

View File

@ -50,8 +50,8 @@ import org.w3c.dom.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.source.MappingException; import org.hibernate.metamodel.source.MappingException;
import org.hibernate.metamodel.source.MetadataSources;
import org.hibernate.metamodel.source.Origin; import org.hibernate.metamodel.source.Origin;
import org.hibernate.metamodel.source.XsdException; import org.hibernate.metamodel.source.XsdException;
import org.hibernate.metamodel.source.annotation.xml.XMLEntityMappings; import org.hibernate.metamodel.source.annotation.xml.XMLEntityMappings;

View File

@ -0,0 +1,74 @@
/*
* 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.internal;
import org.hibernate.cfg.EJB3NamingStrategy;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.metamodel.Metadata;
import org.hibernate.metamodel.MetadataBuilder;
import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.SourceProcessingOrder;
/**
* @author Steve Ebersole
*/
public class MetadataBuilderImpl implements MetadataBuilder {
private final MetadataSources sources;
private NamingStrategy namingStrategy = EJB3NamingStrategy.INSTANCE;
private SourceProcessingOrder sourceProcessingOrder = SourceProcessingOrder.HBM_FIRST;
public MetadataBuilderImpl(MetadataSources sources) {
this.sources = sources;
}
public MetadataSources getSources() {
return sources;
}
public NamingStrategy getNamingStrategy() {
return namingStrategy;
}
public SourceProcessingOrder getSourceProcessingOrder() {
return sourceProcessingOrder;
}
@Override
public MetadataBuilder with(NamingStrategy namingStrategy) {
this.namingStrategy = namingStrategy;
return this;
}
@Override
public MetadataBuilder with(SourceProcessingOrder sourceProcessingOrder) {
this.sourceProcessingOrder = sourceProcessingOrder;
return this;
}
@Override
public Metadata buildMetadata() {
return new MetadataImpl( this );
}
}

View File

@ -40,13 +40,13 @@ import org.hibernate.HibernateException;
import org.hibernate.cfg.NamingStrategy; import org.hibernate.cfg.NamingStrategy;
import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.mapping.MetadataSource; import org.hibernate.mapping.MetadataSource;
import org.hibernate.metamodel.SourceProcessingOrder;
import org.hibernate.metamodel.binding.EntityBinding; import org.hibernate.metamodel.binding.EntityBinding;
import org.hibernate.metamodel.binding.EntityReferencingAttributeBinding;
import org.hibernate.metamodel.binding.FetchProfile; import org.hibernate.metamodel.binding.FetchProfile;
import org.hibernate.metamodel.binding.PluralAttributeBinding; import org.hibernate.metamodel.binding.PluralAttributeBinding;
import org.hibernate.metamodel.relational.Database; import org.hibernate.metamodel.relational.Database;
import org.hibernate.metamodel.source.Metadata; import org.hibernate.metamodel.Metadata;
import org.hibernate.metamodel.source.MetadataSources; import org.hibernate.metamodel.MetadataSources;
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;
import org.hibernate.metamodel.source.annotations.xml.OrmXmlParser; import org.hibernate.metamodel.source.annotations.xml.OrmXmlParser;
@ -76,12 +76,14 @@ public class MetadataImpl implements Metadata, MetadataImplementor, Serializable
private Map<String, FetchProfile> fetchProfiles = new HashMap<String, FetchProfile>(); private Map<String, FetchProfile> fetchProfiles = new HashMap<String, FetchProfile>();
private Map<String, String> imports; private Map<String, String> imports;
public MetadataImpl(MetadataSources metadataSources, ProcessingOrder preferredProcessingOrder) { public MetadataImpl(MetadataBuilderImpl builder) {
final MetadataSources metadataSources = builder.getSources();
this.serviceRegistry = metadataSources.getServiceRegistry(); this.serviceRegistry = metadataSources.getServiceRegistry();
this.namingStrategy = metadataSources.getNamingStrategy(); this.namingStrategy = builder.getNamingStrategy();
final ArrayList<String> processedEntityNames = new ArrayList<String>(); final ArrayList<String> processedEntityNames = new ArrayList<String>();
if ( preferredProcessingOrder == ProcessingOrder.HBM_FIRST ) { if ( builder.getSourceProcessingOrder() == SourceProcessingOrder.HBM_FIRST ) {
applyHibernateMappings( metadataSources, processedEntityNames ); applyHibernateMappings( metadataSources, processedEntityNames );
applyAnnotationMappings( metadataSources, processedEntityNames ); applyAnnotationMappings( metadataSources, processedEntityNames );
} }

View File

@ -25,8 +25,7 @@ package org.hibernate.metamodel.binding;
import org.junit.Test; import org.junit.Test;
import org.hibernate.metamodel.source.Metadata; import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.source.MetadataSources;
import org.hibernate.metamodel.source.internal.MetadataImpl; import org.hibernate.metamodel.source.internal.MetadataImpl;
import org.hibernate.metamodel.source.spi.MetadataImplementor; import org.hibernate.metamodel.source.spi.MetadataImplementor;
import org.hibernate.service.ServiceRegistryBuilder; import org.hibernate.service.ServiceRegistryBuilder;

View File

@ -23,14 +23,10 @@
*/ */
package org.hibernate.metamodel.binding; package org.hibernate.metamodel.binding;
import org.jboss.logging.Logger; import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.source.MetadataSources;
import org.hibernate.metamodel.source.internal.MetadataImpl; import org.hibernate.metamodel.source.internal.MetadataImpl;
import org.hibernate.metamodel.source.spi.MetadataImplementor; import org.hibernate.metamodel.source.spi.MetadataImplementor;
import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
/** /**
@ -39,8 +35,6 @@ import static org.junit.Assert.assertEquals;
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class BasicHbmBindingTests extends AbstractBasicBindingTests { public class BasicHbmBindingTests extends AbstractBasicBindingTests {
private static final Logger log = Logger.getLogger( BasicHbmBindingTests.class.getName() );
public EntityBinding buildSimpleEntityBinding() { public EntityBinding buildSimpleEntityBinding() {
return getEntityBinding( return getEntityBinding(
"org/hibernate/metamodel/binding/SimpleEntity.hbm.xml", "org/hibernate/metamodel/binding/SimpleEntity.hbm.xml",

View File

@ -31,7 +31,7 @@ import org.hibernate.MappingException;
import org.hibernate.annotations.FetchMode; import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.FetchProfile; import org.hibernate.annotations.FetchProfile;
import org.hibernate.annotations.FetchProfiles; import org.hibernate.annotations.FetchProfiles;
import org.hibernate.metamodel.source.MetadataSources; import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.source.annotations.util.JandexHelper; import org.hibernate.metamodel.source.annotations.util.JandexHelper;
import org.hibernate.metamodel.source.internal.MetadataImpl; import org.hibernate.metamodel.source.internal.MetadataImpl;
import org.hibernate.service.ServiceRegistryBuilder; import org.hibernate.service.ServiceRegistryBuilder;

View File

@ -25,8 +25,8 @@ package org.hibernate.metamodel.source.annotations.xml;
import org.junit.Test; import org.junit.Test;
import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.source.MappingException; import org.hibernate.metamodel.source.MappingException;
import org.hibernate.metamodel.source.MetadataSources;
import org.hibernate.metamodel.source.internal.MetadataImpl; import org.hibernate.metamodel.source.internal.MetadataImpl;
import org.hibernate.service.ServiceRegistryBuilder; import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.testing.junit4.BaseUnitTestCase; import org.hibernate.testing.junit4.BaseUnitTestCase;

View File

@ -28,8 +28,8 @@ import java.util.Iterator;
import org.junit.Test; import org.junit.Test;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.binding.FetchProfile; import org.hibernate.metamodel.binding.FetchProfile;
import org.hibernate.metamodel.source.MetadataSources;
import org.hibernate.service.ServiceRegistryBuilder; import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.testing.junit4.BaseUnitTestCase; import org.hibernate.testing.junit4.BaseUnitTestCase;