Skip to content

Commit 755ea99

Browse files
committed
add ability to target a given schema/catalog in SchemaManager
1 parent 8b8d4bb commit 755ea99

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

hibernate-core/src/main/java/org/hibernate/relational/SchemaManager.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,23 @@ public interface SchemaManager extends jakarta.persistence.SchemaManager {
8989
*/
9090
@Incubating
9191
void populate();
92+
93+
/**
94+
* Obtain an instance which targets the given schema.
95+
* @param schemaName The name of the schema to target
96+
*
97+
* @since 7.1
98+
*/
99+
@Incubating
100+
SchemaManager forSchema(String schemaName);
101+
102+
/**
103+
* Obtain an instance which targets the given schema of the given catalog.
104+
* @param schemaName The name of the schema to target
105+
* @param catalogName The name of the catalog to target
106+
*
107+
* @since 7.1
108+
*/
109+
@Incubating
110+
SchemaManager forSchemaAndCatalog(String schemaName, String catalogName);
92111
}

hibernate-core/src/main/java/org/hibernate/relational/internal/SchemaManagerImpl.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,33 @@
2626
public class SchemaManagerImpl implements SchemaManager {
2727
private final SessionFactoryImplementor sessionFactory;
2828
private final MetadataImplementor metadata;
29+
private final String schemaName;
30+
private final String catalogName;
2931

3032
public SchemaManagerImpl(
3133
SessionFactoryImplementor sessionFactory,
3234
MetadataImplementor metadata) {
35+
this( sessionFactory, metadata, null, null );
36+
}
37+
38+
public SchemaManagerImpl(
39+
SessionFactoryImplementor sessionFactory,
40+
MetadataImplementor metadata,
41+
String schemaName, String catalogName) {
3342
this.sessionFactory = sessionFactory;
3443
this.metadata = metadata;
44+
this.schemaName = schemaName;
45+
this.catalogName = catalogName;
46+
}
47+
48+
@Override
49+
public SchemaManager forSchema(String schemaName) {
50+
return new SchemaManagerImpl( sessionFactory, metadata, schemaName, null );
51+
}
52+
53+
@Override
54+
public SchemaManager forSchemaAndCatalog(String schemaName, String catalogName) {
55+
return new SchemaManagerImpl( sessionFactory, metadata, schemaName, catalogName );
3556
}
3657

3758
@Override
@@ -40,6 +61,8 @@ public void exportMappedObjects(boolean createSchemas) {
4061
properties.put( AvailableSettings.JAKARTA_HBM2DDL_DATABASE_ACTION, Action.CREATE_ONLY );
4162
properties.put( AvailableSettings.JAKARTA_HBM2DDL_SCRIPTS_ACTION, Action.NONE );
4263
properties.put( AvailableSettings.JAKARTA_HBM2DDL_CREATE_SCHEMAS, createSchemas );
64+
properties.put( AvailableSettings.DEFAULT_SCHEMA, schemaName );
65+
properties.put( AvailableSettings.DEFAULT_CATALOG, catalogName );
4366
SchemaManagementToolCoordinator.process(
4467
metadata,
4568
sessionFactory.getServiceRegistry(),
@@ -54,6 +77,8 @@ public void dropMappedObjects(boolean dropSchemas) {
5477
properties.put( AvailableSettings.JAKARTA_HBM2DDL_DATABASE_ACTION, Action.DROP );
5578
properties.put( AvailableSettings.JAKARTA_HBM2DDL_SCRIPTS_ACTION, Action.NONE );
5679
properties.put( AvailableSettings.JAKARTA_HBM2DDL_CREATE_SCHEMAS, dropSchemas );
80+
properties.put( AvailableSettings.DEFAULT_SCHEMA, schemaName );
81+
properties.put( AvailableSettings.DEFAULT_CATALOG, catalogName );
5782
SchemaManagementToolCoordinator.process(
5883
metadata,
5984
sessionFactory.getServiceRegistry(),
@@ -68,6 +93,8 @@ public void validateMappedObjects() {
6893
properties.put( AvailableSettings.JAKARTA_HBM2DDL_DATABASE_ACTION, Action.VALIDATE );
6994
properties.put( AvailableSettings.JAKARTA_HBM2DDL_SCRIPTS_ACTION, Action.NONE );
7095
properties.put( AvailableSettings.JAKARTA_HBM2DDL_CREATE_SCHEMAS, false );
96+
properties.put( AvailableSettings.DEFAULT_SCHEMA, schemaName );
97+
properties.put( AvailableSettings.DEFAULT_CATALOG, catalogName );
7198
SchemaManagementToolCoordinator.process(
7299
metadata,
73100
sessionFactory.getServiceRegistry(),
@@ -81,6 +108,8 @@ public void truncateMappedObjects() {
81108
Map<String, Object> properties = new HashMap<>( sessionFactory.getProperties() );
82109
properties.put( AvailableSettings.JAKARTA_HBM2DDL_DATABASE_ACTION, Action.TRUNCATE );
83110
properties.put( AvailableSettings.JAKARTA_HBM2DDL_SCRIPTS_ACTION, Action.NONE );
111+
properties.put( AvailableSettings.DEFAULT_SCHEMA, schemaName );
112+
properties.put( AvailableSettings.DEFAULT_CATALOG, catalogName );
84113
SchemaManagementToolCoordinator.process(
85114
metadata,
86115
sessionFactory.getServiceRegistry(),
@@ -94,6 +123,8 @@ public void populate() {
94123
Map<String, Object> properties = new HashMap<>( sessionFactory.getProperties() );
95124
properties.put( AvailableSettings.JAKARTA_HBM2DDL_DATABASE_ACTION, Action.POPULATE );
96125
properties.put( AvailableSettings.JAKARTA_HBM2DDL_SCRIPTS_ACTION, Action.NONE );
126+
properties.put( AvailableSettings.DEFAULT_SCHEMA, schemaName );
127+
properties.put( AvailableSettings.DEFAULT_CATALOG, catalogName );
97128
SchemaManagementToolCoordinator.process(
98129
metadata,
99130
sessionFactory.getServiceRegistry(),

0 commit comments

Comments
 (0)