Skip to content

Commit 4f6c449

Browse files
committed
introduce a @DialectOverride annotation
This allows the use of native SQL in annotations of portable programs. We've needed something like this for a long time.
1 parent bf1eec6 commit 4f6c449

File tree

19 files changed

+190
-162
lines changed

19 files changed

+190
-162
lines changed

documentation/src/test/java/org/hibernate/userguide/schema/ColumnDefaultTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
import jakarta.persistence.Id;
1111

1212
import org.hibernate.annotations.ColumnDefault;
13+
import org.hibernate.annotations.DialectOverride;
1314
import org.hibernate.annotations.DynamicInsert;
15+
import org.hibernate.dialect.HSQLDialect;
1416
import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase;
1517

1618
import org.junit.Test;
@@ -57,7 +59,8 @@ public static class Person {
5759
@ColumnDefault("'N/A'")
5860
private String name;
5961

60-
@ColumnDefault("-1")
62+
@ColumnDefault(value = "-1",
63+
overrides = @DialectOverride(dialect = HSQLDialect.class, value = "-1*1") )
6164
private Long clientId;
6265

6366
//Getter and setters omitted for brevity

hibernate-core/src/main/java/org/hibernate/annotations/Check.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,10 @@
2626
* The check constraints string.
2727
*/
2828
String constraints();
29+
/**
30+
* A list of SQL constraints that should be used in the specified dialects.
31+
*
32+
* Overrides the default {@link #constraints()}.
33+
*/
34+
DialectOverride[] overrides() default {};
2935
}

hibernate-core/src/main/java/org/hibernate/annotations/ColumnDefault.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,10 @@
2525
* The DEFAULT definition to apply to the DDL.
2626
*/
2727
String value();
28+
/**
29+
* A list of SQL expressions that should be used in the specified dialects.
30+
*
31+
* Overrides the default {@link #value()}.
32+
*/
33+
DialectOverride[] overrides() default {};
2834
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.annotations;
8+
9+
import org.hibernate.dialect.Dialect;
10+
11+
import java.lang.annotation.Retention;
12+
13+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
14+
15+
/**
16+
* Defines a SQL expression or fragment of SQL that should be used in a given dialect.
17+
* Allows a portable program to embed unportable native SQL in annotations.
18+
*
19+
* @see Formula#overrides()
20+
* @see JoinFormula#overrides()
21+
* @see DiscriminatorFormula#overrides()
22+
* @see Check#overrides()
23+
* @see ColumnDefault#overrides()
24+
* @see Filter#overrides()
25+
* @see FilterDef#overrides()
26+
* @see Where#overrides()
27+
* @see OrderBy#overrides()
28+
*
29+
* @author Gavin King
30+
*/
31+
@Retention(RUNTIME)
32+
public @interface DialectOverride {
33+
/**
34+
* The {@link Dialect} to which this override applies.
35+
*
36+
* @return the concrete Java class of the {@code Dialect}
37+
*/
38+
Class<? extends Dialect> dialect();
39+
40+
/**
41+
* The SQL expression or SQL fragment that should be used in the specified dialect.
42+
*/
43+
String value();
44+
}

hibernate-core/src/main/java/org/hibernate/annotations/DiscriminatorFormula.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,10 @@
3030
* The formula string.
3131
*/
3232
String value();
33+
/**
34+
* A list of SQL formulas that should be used in the specified dialects.
35+
*
36+
* Overrides the default {@link #value()}.
37+
*/
38+
DialectOverride[] overrides() default {};
3339
}

hibernate-core/src/main/java/org/hibernate/annotations/Filter.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,11 @@
4848
* The alias descriptors for injection.
4949
*/
5050
SqlFragmentAlias[] aliases() default {};
51+
52+
/**
53+
* A list of SQL conditions that should be used in the specified dialects.
54+
*
55+
* Overrides the default {@link #condition()}.
56+
*/
57+
DialectOverride[] overrides() default {};
5158
}

hibernate-core/src/main/java/org/hibernate/annotations/FilterDef.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,11 @@
3737
* The filter parameter definitions.
3838
*/
3939
ParamDef[] parameters() default {};
40+
41+
/**
42+
* A list of SQL conditions that should be used in the specified dialects.
43+
*
44+
* Overrides the default {@link #defaultCondition()}.
45+
*/
46+
DialectOverride[] overrides() default {};
4047
}

hibernate-core/src/main/java/org/hibernate/annotations/Formula.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,10 @@
5050
* The formula string.
5151
*/
5252
String value();
53+
/**
54+
* A list of SQL formulas that should be used in the specified dialects.
55+
*
56+
* Overrides the default {@link #value()}.
57+
*/
58+
DialectOverride[] overrides() default {};
5359
}

hibernate-core/src/main/java/org/hibernate/annotations/JoinFormula.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,11 @@
3131
* The column this formula references.
3232
*/
3333
String referencedColumnName() default "";
34+
35+
/**
36+
* A list of SQL formulas that should be used in the specified dialects.
37+
*
38+
* Overrides the default {@link #value()}.
39+
*/
40+
DialectOverride[] overrides() default {};
3441
}

hibernate-core/src/main/java/org/hibernate/annotations/OrderBy.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,10 @@
3232
* SQL ordering clause.
3333
*/
3434
String clause();
35+
/**
36+
* A list of SQL ordering expressions that should be used in the specified dialects.
37+
*
38+
* Overrides the default {@link #clause()}.
39+
*/
40+
DialectOverride[] overrides() default {};
3541
}

0 commit comments

Comments
 (0)