Skip to content

Commit 3dabce4

Browse files
committed
introduce @DefaultSchema
to allow setting the schema at the package or outer class level
1 parent a7d76b2 commit 3dabce4

File tree

3 files changed

+97
-5
lines changed

3 files changed

+97
-5
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.hibernate.annotations;
2+
3+
import java.lang.annotation.Retention;
4+
import java.lang.annotation.Target;
5+
6+
import static java.lang.annotation.ElementType.PACKAGE;
7+
import static java.lang.annotation.ElementType.TYPE;
8+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
9+
10+
/**
11+
* Specifies a default schema for all table mappings occurring
12+
* within the scope of a certain package or class, overriding the
13+
* effect of {@link org.hibernate.cfg.AvailableSettings#DEFAULT_SCHEMA}
14+
* and/or {@link org.hibernate.cfg.AvailableSettings#DEFAULT_CATALOG}
15+
* if either of those configuration settings is specified.
16+
* <p>
17+
* The effect of this annotation may be overridden by explicitly
18+
* specifying {@link jakarta.persistence.Table#schema()}.
19+
*
20+
* @author Gavin King
21+
*/
22+
@Target({TYPE,PACKAGE})
23+
@Retention(RUNTIME)
24+
public @interface DefaultSchema {
25+
/**
26+
* A schema to use when no schema is explicitly specified using
27+
* {@link jakarta.persistence.Table#schema()}.
28+
*/
29+
String schema() default "";
30+
/**
31+
* A catalog to use when no schema is explicitly specified using
32+
* {@link jakarta.persistence.Table#catalog()}.
33+
*/
34+
String catalog() default "";
35+
}

hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.hibernate.annotations.CollectionTypeRegistrations;
3737
import org.hibernate.annotations.Columns;
3838
import org.hibernate.annotations.Comment;
39+
import org.hibernate.annotations.DefaultSchema;
3940
import org.hibernate.annotations.DiscriminatorFormula;
4041
import org.hibernate.annotations.DiscriminatorOptions;
4142
import org.hibernate.annotations.EmbeddableInstantiatorRegistration;
@@ -589,9 +590,9 @@ public static void bindClass(
589590
bindQueries( clazzToProcess, context );
590591
bindFilterDefs( clazzToProcess, context );
591592

592-
String schema = "";
593-
String table = ""; //might be no @Table annotation on the annotated class
594-
String catalog = "";
593+
String schema;
594+
String catalog;
595+
String table;
595596
List<UniqueConstraintHolder> uniqueConstraints = new ArrayList<>();
596597
jakarta.persistence.Table tabAnn = null;
597598
if ( clazzToProcess.isAnnotationPresent( jakarta.persistence.Table.class ) ) {
@@ -601,6 +602,19 @@ public static void bindClass(
601602
catalog = tabAnn.catalog();
602603
uniqueConstraints = TableBinder.buildUniqueConstraintHolders( tabAnn.uniqueConstraints() );
603604
}
605+
else {
606+
//might be no @Table annotation on the annotated class
607+
schema = "";
608+
catalog = "";
609+
table = "";
610+
}
611+
612+
if ( schema.isEmpty() ) {
613+
schema = defaultSchema( clazzToProcess );
614+
}
615+
if ( catalog.isEmpty() ) {
616+
catalog = defaultCatalog( clazzToProcess );
617+
}
604618

605619
AnnotatedJoinColumn[] inheritanceJoinedColumns = makeInheritanceJoinColumns(
606620
clazzToProcess,
@@ -842,6 +856,34 @@ else if ( InheritanceType.SINGLE_TABLE.equals( inheritanceState.getType() ) ) {
842856
bindCallbacks( clazzToProcess, persistentClass, context );
843857
}
844858

859+
public static String defaultSchema(XClass clazzToProcess) {
860+
XAnnotatedElement annotatedElement = clazzToProcess;
861+
while (annotatedElement instanceof XClass) {
862+
if ( clazzToProcess.isAnnotationPresent(DefaultSchema.class) ) {
863+
return clazzToProcess.getAnnotation(DefaultSchema.class).schema();
864+
}
865+
annotatedElement = clazzToProcess.getContainingElement();
866+
}
867+
if ( annotatedElement.isAnnotationPresent(DefaultSchema.class) ) {
868+
return annotatedElement.getAnnotation(DefaultSchema.class).schema();
869+
}
870+
return "";
871+
}
872+
873+
public static String defaultCatalog(XClass clazzToProcess) {
874+
XAnnotatedElement annotatedElement = clazzToProcess;
875+
while (annotatedElement instanceof XClass) {
876+
if ( clazzToProcess.isAnnotationPresent(DefaultSchema.class) ) {
877+
return clazzToProcess.getAnnotation(DefaultSchema.class).catalog();
878+
}
879+
annotatedElement = clazzToProcess.getContainingElement();
880+
}
881+
if ( annotatedElement.isAnnotationPresent(DefaultSchema.class) ) {
882+
return annotatedElement.getAnnotation(DefaultSchema.class).catalog();
883+
}
884+
return "";
885+
}
886+
845887
private static void handleTypeDescriptorRegistrations(XAnnotatedElement annotatedElement, MetadataBuildingContext context) {
846888
final ManagedBeanRegistry managedBeanRegistry = context.getBootstrapContext()
847889
.getServiceRegistry()
@@ -2654,9 +2696,15 @@ private static void bindJoinedTableAssociation(
26542696
if ( !BinderHelper.isEmptyAnnotationValue( schema ) ) {
26552697
associationTableBinder.setSchema( schema );
26562698
}
2699+
else {
2700+
associationTableBinder.setSchema( defaultSchema( property.getDeclaringClass() ) );
2701+
}
26572702
if ( !BinderHelper.isEmptyAnnotationValue( catalog ) ) {
26582703
associationTableBinder.setCatalog( catalog );
26592704
}
2705+
else {
2706+
associationTableBinder.setCatalog( defaultCatalog( property.getDeclaringClass() ) );
2707+
}
26602708
if ( !BinderHelper.isEmptyAnnotationValue( tableName ) ) {
26612709
associationTableBinder.setName( tableName );
26622710
}
@@ -2669,6 +2717,8 @@ private static void bindJoinedTableAssociation(
26692717
else {
26702718
annJoins = null;
26712719
annInverseJoins = null;
2720+
associationTableBinder.setSchema( defaultSchema( property.getDeclaringClass() ) );
2721+
associationTableBinder.setCatalog( defaultCatalog( property.getDeclaringClass() ) );
26722722
}
26732723
AnnotatedJoinColumn[] joinColumns = AnnotatedJoinColumn.buildJoinTableJoinColumns(
26742724
annJoins,

hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,8 +1055,8 @@ private Join addJoin(
10551055
Join join = new Join();
10561056
join.setPersistentClass( persistentClass );
10571057

1058-
final String schema;
1059-
final String catalog;
1058+
String schema;
1059+
String catalog;
10601060
final Object joinColumns;
10611061
final List<UniqueConstraintHolder> uniqueConstraintHolders;
10621062

@@ -1095,6 +1095,13 @@ else if ( joinTable != null ) {
10951095
throw new AssertionFailure( "Both JoinTable and SecondaryTable are null" );
10961096
}
10971097

1098+
if ( schema.isEmpty() ) {
1099+
schema = AnnotationBinder.defaultSchema( annotatedClass );
1100+
}
1101+
if ( catalog.isEmpty() ) {
1102+
catalog = AnnotationBinder.defaultCatalog( annotatedClass );
1103+
}
1104+
10981105
final Table table = TableBinder.buildAndFillTable(
10991106
schema,
11001107
catalog,

0 commit comments

Comments
 (0)