HHH-12954 Refactor boot/model/relational/Database to avoid holding references to MetadataBuildingOptions

This commit is contained in:
Sanne Grinovero 2018-09-04 11:07:58 +01:00
parent 4eb726ef4c
commit 50990dd76b
4 changed files with 30 additions and 29 deletions

View File

@ -21,20 +21,21 @@ import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.H2Dialect; import org.hibernate.dialect.H2Dialect;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment; import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.engine.jdbc.spi.JdbcServices; import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.service.ServiceRegistry;
/** /**
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class Database { public class Database {
private final Dialect dialect; private final Dialect dialect;
private final MetadataBuildingOptions buildingOptions;
private final JdbcEnvironment jdbcEnvironment; private final JdbcEnvironment jdbcEnvironment;
private final Map<Namespace.Name,Namespace> namespaceMap = new TreeMap<Namespace.Name, Namespace>();
private final Map<String,AuxiliaryDatabaseObject> auxiliaryDatabaseObjects = new HashMap<String,AuxiliaryDatabaseObject>();
private final ServiceRegistry serviceRegistry;
private final PhysicalNamingStrategy physicalNamingStrategy;
private Namespace implicitNamespace; private Namespace implicitNamespace;
private final Map<Namespace.Name,Namespace> namespaceMap = new TreeMap<Namespace.Name, Namespace>();
private Map<String,AuxiliaryDatabaseObject> auxiliaryDatabaseObjects;
private List<InitCommand> initCommands; private List<InitCommand> initCommands;
public Database(MetadataBuildingOptions buildingOptions) { public Database(MetadataBuildingOptions buildingOptions) {
@ -42,10 +43,9 @@ public class Database {
} }
public Database(MetadataBuildingOptions buildingOptions, JdbcEnvironment jdbcEnvironment) { public Database(MetadataBuildingOptions buildingOptions, JdbcEnvironment jdbcEnvironment) {
this.buildingOptions = buildingOptions; this.serviceRegistry = buildingOptions.getServiceRegistry();
this.jdbcEnvironment = jdbcEnvironment; this.jdbcEnvironment = jdbcEnvironment;
this.physicalNamingStrategy = buildingOptions.getPhysicalNamingStrategy();
this.dialect = determineDialect( buildingOptions ); this.dialect = determineDialect( buildingOptions );
this.implicitNamespace = makeNamespace( this.implicitNamespace = makeNamespace(
@ -68,15 +68,11 @@ public class Database {
private Namespace makeNamespace(Namespace.Name name) { private Namespace makeNamespace(Namespace.Name name) {
Namespace namespace; Namespace namespace;
namespace = new Namespace( this, name ); namespace = new Namespace( this.getPhysicalNamingStrategy(), this.getJdbcEnvironment(), name );
namespaceMap.put( name, namespace ); namespaceMap.put( name, namespace );
return namespace; return namespace;
} }
public MetadataBuildingOptions getBuildingOptions() {
return buildingOptions;
}
public Dialect getDialect() { public Dialect getDialect() {
return dialect; return dialect;
} }
@ -105,7 +101,7 @@ public class Database {
} }
public PhysicalNamingStrategy getPhysicalNamingStrategy() { public PhysicalNamingStrategy getPhysicalNamingStrategy() {
return getBuildingOptions().getPhysicalNamingStrategy(); return physicalNamingStrategy;
} }
public Iterable<Namespace> getNamespaces() { public Iterable<Namespace> getNamespaces() {
@ -148,9 +144,6 @@ public class Database {
} }
public void addAuxiliaryDatabaseObject(AuxiliaryDatabaseObject auxiliaryDatabaseObject) { public void addAuxiliaryDatabaseObject(AuxiliaryDatabaseObject auxiliaryDatabaseObject) {
if ( auxiliaryDatabaseObjects == null ) {
auxiliaryDatabaseObjects = new HashMap<String,AuxiliaryDatabaseObject>();
}
auxiliaryDatabaseObjects.put( auxiliaryDatabaseObject.getExportIdentifier(), auxiliaryDatabaseObject ); auxiliaryDatabaseObjects.put( auxiliaryDatabaseObject.getExportIdentifier(), auxiliaryDatabaseObject );
} }
@ -172,4 +165,8 @@ public class Database {
} }
initCommands.add( initCommand ); initCommands.add( initCommand );
} }
public ServiceRegistry getServiceRegistry() {
return serviceRegistry;
}
} }

View File

@ -13,6 +13,8 @@ import java.util.TreeMap;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.boot.model.naming.Identifier; import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.naming.PhysicalNamingStrategy;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.internal.CoreLogging; import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.mapping.DenormalizedTable; import org.hibernate.mapping.DenormalizedTable;
@ -26,22 +28,24 @@ import org.hibernate.mapping.Table;
public class Namespace { public class Namespace {
private static final CoreMessageLogger log = CoreLogging.messageLogger( Namespace.class ); private static final CoreMessageLogger log = CoreLogging.messageLogger( Namespace.class );
private final Database database; private final PhysicalNamingStrategy physicalNamingStrategy;
private final JdbcEnvironment jdbcEnvironment;
private final Name name; private final Name name;
private final Name physicalName; private final Name physicalName;
private Map<Identifier, Table> tables = new TreeMap<Identifier, Table>(); private Map<Identifier, Table> tables = new TreeMap<Identifier, Table>();
private Map<Identifier, Sequence> sequences = new TreeMap<Identifier, Sequence>(); private Map<Identifier, Sequence> sequences = new TreeMap<Identifier, Sequence>();
public Namespace(Database database, Name name) { public Namespace(PhysicalNamingStrategy physicalNamingStrategy, JdbcEnvironment jdbcEnvironment, Name name) {
this.database = database; this.physicalNamingStrategy = physicalNamingStrategy;
this.jdbcEnvironment = jdbcEnvironment;
this.name = name; this.name = name;
this.physicalName = new Name( this.physicalName = new Name(
database.getPhysicalNamingStrategy() physicalNamingStrategy
.toPhysicalCatalogName( name.getCatalog(), database.getJdbcEnvironment() ), .toPhysicalCatalogName( name.getCatalog(), jdbcEnvironment ),
database.getPhysicalNamingStrategy() physicalNamingStrategy
.toPhysicalSchemaName( name.getSchema(), database.getJdbcEnvironment() ) .toPhysicalSchemaName( name.getSchema(), jdbcEnvironment )
); );
log.debugf( log.debugf(
@ -89,7 +93,7 @@ public class Namespace {
return existing; return existing;
} }
final Identifier physicalTableName = database.getPhysicalNamingStrategy().toPhysicalTableName( logicalTableName, database.getJdbcEnvironment() ); final Identifier physicalTableName = physicalNamingStrategy.toPhysicalTableName( logicalTableName, jdbcEnvironment );
Table table = new Table( this, physicalTableName, isAbstract ); Table table = new Table( this, physicalTableName, isAbstract );
tables.put( logicalTableName, table ); tables.put( logicalTableName, table );
return table; return table;
@ -102,7 +106,7 @@ public class Namespace {
return (DenormalizedTable) existing; return (DenormalizedTable) existing;
} }
final Identifier physicalTableName = database.getPhysicalNamingStrategy().toPhysicalTableName( logicalTableName, database.getJdbcEnvironment() ); final Identifier physicalTableName = physicalNamingStrategy.toPhysicalTableName( logicalTableName, jdbcEnvironment );
DenormalizedTable table = new DenormalizedTable( this, physicalTableName, isAbstract, includedTable ); DenormalizedTable table = new DenormalizedTable( this, physicalTableName, isAbstract, includedTable );
tables.put( logicalTableName, table ); tables.put( logicalTableName, table );
return table; return table;
@ -117,7 +121,7 @@ public class Namespace {
throw new HibernateException( "Sequence was already registered with that name [" + logicalName.toString() + "]" ); throw new HibernateException( "Sequence was already registered with that name [" + logicalName.toString() + "]" );
} }
final Identifier physicalName = database.getPhysicalNamingStrategy().toPhysicalSequenceName( logicalName, database.getJdbcEnvironment() ); final Identifier physicalName = physicalNamingStrategy.toPhysicalSequenceName( logicalName, jdbcEnvironment );
Sequence sequence = new Sequence( Sequence sequence = new Sequence(
this.physicalName.getCatalog(), this.physicalName.getCatalog(),

View File

@ -141,7 +141,7 @@ public class ExportableColumn extends Column {
@Override @Override
public ServiceRegistry getServiceRegistry() { public ServiceRegistry getServiceRegistry() {
return database.getBuildingOptions().getServiceRegistry(); return database.getServiceRegistry();
} }
} }

View File

@ -44,7 +44,7 @@ public class NamespaceTest {
@Test @Test
public void testPhysicalNameSchemaAndCatalog() { public void testPhysicalNameSchemaAndCatalog() {
Namespace namespace = new Namespace( mockDatabase, name ); Namespace namespace = new Namespace( mockDatabase.getPhysicalNamingStrategy(), mockDatabase.getJdbcEnvironment(), name );
final Namespace.Name physicalName = namespace.getPhysicalName(); final Namespace.Name physicalName = namespace.getPhysicalName();