Skip to content

HHH-14469 - Support schema-tooling on sub-sets of the relational mode… #3763

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions hibernate-core/src/main/java/org/hibernate/boot/Metadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package org.hibernate.boot;

import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.function.Consumer;

Expand Down Expand Up @@ -192,4 +193,9 @@ public interface Metadata extends Mapping {
java.util.Collection<Table> collectTableMappings();

Map<String, SqmFunctionDescriptor> getSqlFunctionMap();

/**
* All of the known model contributors
*/
Set<String> getContributors();
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@
import org.hibernate.boot.model.relational.QualifiedTableName;
import org.hibernate.boot.model.source.internal.ImplicitColumnNamingSecondPass;
import org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext;
import org.hibernate.boot.spi.BootstrapContext;
import org.hibernate.boot.spi.InFlightMetadataCollector;
import org.hibernate.boot.spi.MetadataBuildingContext;
import org.hibernate.boot.spi.MetadataBuildingOptions;
import org.hibernate.boot.query.NamedHqlQueryDefinition;
import org.hibernate.boot.query.NamedNativeQueryDefinition;
import org.hibernate.boot.query.NamedProcedureCallDefinition;
import org.hibernate.boot.query.NamedResultSetMappingDescriptor;
import org.hibernate.boot.spi.BootstrapContext;
import org.hibernate.boot.spi.InFlightMetadataCollector;
import org.hibernate.boot.spi.MetadataBuildingContext;
import org.hibernate.boot.spi.MetadataBuildingOptions;
import org.hibernate.boot.spi.NaturalIdUniqueKeyBinder;
import org.hibernate.cfg.AnnotatedClassType;
import org.hibernate.cfg.AvailableSettings;
Expand Down Expand Up @@ -231,6 +231,11 @@ public Map<String, SqmFunctionDescriptor> getSqlFunctionMap() {
return sqlFunctionMap;
}

@Override
public Set<String> getContributors() {
throw new UnsupportedOperationException();
}

@Override
public void validate() throws MappingException {
// nothing to do
Expand Down Expand Up @@ -744,7 +749,8 @@ public Table addTable(
String catalogName,
String name,
String subselectFragment,
boolean isAbstract) {
boolean isAbstract,
MetadataBuildingContext buildingContext) {
final Namespace namespace = getDatabase().locateNamespace(
getDatabase().toIdentifier( catalogName ),
getDatabase().toIdentifier( schemaName )
Expand All @@ -761,17 +767,21 @@ public Table addTable(
}

if ( subselectFragment != null ) {
return new Table( namespace, logicalName, subselectFragment, isAbstract );
return new Table( buildingContext.getCurrentContributorName(), namespace, logicalName, subselectFragment, isAbstract );
}
else {
Table table = namespace.locateTable( logicalName );
if ( table != null ) {
final Table existing = namespace.locateTable( logicalName );
if ( existing != null ) {
if ( !isAbstract ) {
table.setAbstract( false );
existing.setAbstract( false );
}
return table;
return existing;
}
return namespace.createTable( logicalName, isAbstract );

return namespace.createTable(
logicalName,
(physicalName) -> new Table( buildingContext.getCurrentContributorName(), namespace, physicalName, isAbstract )
);
}
}

Expand All @@ -782,7 +792,8 @@ public Table addDenormalizedTable(
String name,
boolean isAbstract,
String subselectFragment,
Table includedTable) throws DuplicateMappingException {
Table includedTable,
MetadataBuildingContext buildingContext) throws DuplicateMappingException {
final Namespace namespace = getDatabase().locateNamespace(
getDatabase().toIdentifier( catalogName ),
getDatabase().toIdentifier( schemaName )
Expand All @@ -799,15 +810,34 @@ public Table addDenormalizedTable(
}

if ( subselectFragment != null ) {
return new DenormalizedTable( namespace, logicalName, subselectFragment, isAbstract, includedTable );
return namespace.createDenormalizedTable(
logicalName,
(physicalName) -> new DenormalizedTable(
buildingContext.getCurrentContributorName(),
namespace,
logicalName,
subselectFragment,
isAbstract,
includedTable
)
);
}
else {
Table table = namespace.locateTable( logicalName );
if ( table != null ) {
throw new DuplicateMappingException( DuplicateMappingException.Type.TABLE, logicalName.toString() );
}
else {
table = namespace.createDenormalizedTable( logicalName, isAbstract, includedTable );
table = namespace.createDenormalizedTable(
logicalName,
(physicalTableName) -> new DenormalizedTable(
buildingContext.getCurrentContributorName(),
namespace,
physicalTableName,
isAbstract,
includedTable
)
);
}
return table;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
import org.hibernate.boot.spi.MetadataBuildingOptions;

/**
* @author Steve Ebersole
* Root MetadataBuildingContext
*/
public class MetadataBuildingContextRootImpl implements MetadataBuildingContext {
private final String contributor;
private final BootstrapContext bootstrapContext;
private final MetadataBuildingOptions options;
private final MappingDefaults mappingDefaults;
Expand All @@ -27,9 +28,11 @@ public class MetadataBuildingContextRootImpl implements MetadataBuildingContext
private final TypeDefinitionRegistryStandardImpl typeDefinitionRegistry;

public MetadataBuildingContextRootImpl(
String contributor,
BootstrapContext bootstrapContext,
MetadataBuildingOptions options,
InFlightMetadataCollector metadataCollector) {
this.contributor = contributor;
this.bootstrapContext = bootstrapContext;
this.options = options;
this.mappingDefaults = options.getMappingDefaults();
Expand Down Expand Up @@ -77,4 +80,9 @@ public ObjectNameNormalizer getObjectNameNormalizer() {
public TypeDefinitionRegistryStandardImpl getTypeDefinitionRegistry() {
return typeDefinitionRegistry;
}

@Override
public String getCurrentContributorName() {
return contributor;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.hibernate.boot.model.TypeDefinition;
import org.hibernate.boot.model.relational.Database;
import org.hibernate.boot.model.relational.Namespace;
import org.hibernate.boot.model.relational.Sequence;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.boot.spi.BootstrapContext;
import org.hibernate.boot.spi.MetadataBuildingOptions;
Expand Down Expand Up @@ -310,6 +311,27 @@ public Map<String, SqmFunctionDescriptor> getSqlFunctionMap() {
return sqlFunctionMap;
}

@Override
public Set<String> getContributors() {
final HashSet<String> contributors = new HashSet<>();

entityBindingMap.forEach(
(s, persistentClass) -> contributors.add( persistentClass.getContributor() )
);

for ( Namespace namespace : database.getNamespaces() ) {
for ( Table table : namespace.getTables() ) {
contributors.add( table.getContributor() );
}

for ( Sequence sequence : namespace.getSequences() ) {
contributors.add( sequence.getContributor() );
}
}

return contributors;
}

@Override
public java.util.Collection<Table> collectTableMappings() {
ArrayList<Table> tables = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.boot.spi.AdditionalJaxbMappingProducer;
import org.hibernate.boot.spi.BootstrapContext;
import org.hibernate.boot.spi.MetadataBuildingContext;
import org.hibernate.boot.spi.MetadataBuildingOptions;
import org.hibernate.boot.spi.MetadataContributor;
import org.hibernate.boot.spi.MetadataImplementor;
Expand Down Expand Up @@ -133,6 +134,7 @@ public static MetadataImplementor complete(
final ClassLoaderService classLoaderService = options.getServiceRegistry().getService( ClassLoaderService.class );

final MetadataBuildingContextRootImpl rootMetadataBuildingContext = new MetadataBuildingContextRootImpl(
"orm",
bootstrapContext,
options,
metadataCollector
Expand Down Expand Up @@ -290,7 +292,7 @@ public void finishUp() {
metadataCollector.processSecondPasses( rootMetadataBuildingContext );

if ( options.isXmlMappingEnabled() ) {
Iterable<AdditionalJaxbMappingProducer> producers = classLoaderService.loadJavaServices( AdditionalJaxbMappingProducer.class );
final Iterable<AdditionalJaxbMappingProducer> producers = classLoaderService.loadJavaServices( AdditionalJaxbMappingProducer.class );
if ( producers != null ) {
final EntityHierarchyBuilder hierarchyBuilder = new EntityHierarchyBuilder();
// final MappingBinder mappingBinder = new MappingBinder( true );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.boot.model.relational;

import org.hibernate.mapping.Contributable;

/**
* Contributable specialization for Tables and Sequences
*/
public interface ContributableDatabaseObject extends Contributable, Exportable {
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
import java.util.function.Function;

import org.hibernate.HibernateException;
import org.hibernate.boot.model.naming.Identifier;
Expand All @@ -33,8 +34,8 @@ public class Namespace {
private final Name name;
private final Name physicalName;

private Map<Identifier, Table> tables = new TreeMap<>();
private Map<Identifier, Sequence> sequences = new TreeMap<>();
private final Map<Identifier, Table> tables = new TreeMap<>();
private final Map<Identifier, Sequence> sequences = new TreeMap<>();

public Namespace(PhysicalNamingStrategy physicalNamingStrategy, JdbcEnvironment jdbcEnvironment, Name name) {
this.physicalNamingStrategy = physicalNamingStrategy;
Expand Down Expand Up @@ -89,50 +90,45 @@ public Table locateTable(Identifier logicalTableName) {
*
* @return the created table.
*/
public Table createTable(Identifier logicalTableName, boolean isAbstract) {
public Table createTable(Identifier logicalTableName, Function<Identifier,Table> creator) {
final Table existing = tables.get( logicalTableName );
if ( existing != null ) {
return existing;
}

final Identifier physicalTableName = physicalNamingStrategy.toPhysicalTableName( logicalTableName, jdbcEnvironment );
Table table = new Table( this, physicalTableName, isAbstract );
final Table table = creator.apply( physicalTableName );
tables.put( logicalTableName, table );

return table;
}

public DenormalizedTable createDenormalizedTable(Identifier logicalTableName, boolean isAbstract, Table includedTable) {
public DenormalizedTable createDenormalizedTable(Identifier logicalTableName, Function<Identifier,DenormalizedTable> creator) {
final Table existing = tables.get( logicalTableName );
if ( existing != null ) {
// for now assume it is
return (DenormalizedTable) existing;
}

final Identifier physicalTableName = physicalNamingStrategy.toPhysicalTableName( logicalTableName, jdbcEnvironment );
DenormalizedTable table = new DenormalizedTable( this, physicalTableName, isAbstract, includedTable );
final DenormalizedTable table = creator.apply( physicalTableName );
tables.put( logicalTableName, table );

return table;
}

public Sequence locateSequence(Identifier name) {
return sequences.get( name );
}

public Sequence createSequence(Identifier logicalName, int initialValue, int increment) {
public Sequence createSequence(Identifier logicalName, Function<Identifier,Sequence> creator) {
if ( sequences.containsKey( logicalName ) ) {
throw new HibernateException( "Sequence was already registered with that name [" + logicalName.toString() + "]" );
}

final Identifier physicalName = physicalNamingStrategy.toPhysicalSequenceName( logicalName, jdbcEnvironment );

Sequence sequence = new Sequence(
this.physicalName.getCatalog(),
this.physicalName.getSchema(),
physicalName,
initialValue,
increment
);
final Sequence sequence = creator.apply( physicalName );
sequences.put( logicalName, sequence );

return sequence;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @author Steve Ebersole
*/
public class Sequence implements Exportable {
public class Sequence implements ContributableDatabaseObject {
public static class Name extends QualifiedNameParser.NameParts {
public Name(
Identifier catalogIdentifier,
Expand All @@ -26,21 +26,33 @@ public Name(

private final QualifiedSequenceName name;
private final String exportIdentifier;
private final String contributor;

private int initialValue = 1;
private int incrementSize = 1;

public Sequence(Identifier catalogName, Identifier schemaName, Identifier sequenceName) {
this.name = new QualifiedSequenceName( catalogName, schemaName, sequenceName );
public Sequence(
String contributor,
Identifier catalogName,
Identifier schemaName,
Identifier sequenceName) {
this.contributor = contributor;
this.name = new QualifiedSequenceName(
catalogName,
schemaName,
sequenceName
);
this.exportIdentifier = name.render();
}

public Sequence(
String contributor,
Identifier catalogName,
Identifier schemaName,
Identifier sequenceName,
int initialValue,
int incrementSize) {
this( catalogName, schemaName, sequenceName );
this( contributor, catalogName, schemaName, sequenceName );
this.initialValue = initialValue;
this.incrementSize = incrementSize;
}
Expand All @@ -54,6 +66,11 @@ public String getExportIdentifier() {
return exportIdentifier;
}

@Override
public String getContributor() {
return contributor;
}

public int getInitialValue() {
return initialValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ public void indexMappingDocument(MappingDocument mappingDocument) {
final RootEntitySourceImpl rootEntitySource = new RootEntitySourceImpl( mappingDocument, jaxbRootEntity );
entitySourceByNameMap.put( rootEntitySource.getEntityNamingSource().getEntityName(), rootEntitySource );

final EntityHierarchySourceImpl hierarchy = new EntityHierarchySourceImpl( rootEntitySource );
final EntityHierarchySourceImpl hierarchy = new EntityHierarchySourceImpl(
rootEntitySource,
mappingDocument
);
entityHierarchyList.add( hierarchy );

linkAnyWaiting( mappingDocument, rootEntitySource );
Expand Down
Loading