diff --git a/hibernate-core/src/main/java/org/hibernate/annotations/ColumnDefault.java b/hibernate-core/src/main/java/org/hibernate/annotations/ColumnDefault.java
index 8853aac171..2c02eb3d49 100644
--- a/hibernate-core/src/main/java/org/hibernate/annotations/ColumnDefault.java
+++ b/hibernate-core/src/main/java/org/hibernate/annotations/ColumnDefault.java
@@ -14,8 +14,7 @@ import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
- * Specifies that a column has a {@code DEFAULT} value specified in DDL,
- * and whether Hibernate should fetch the defaulted value from the database.
+ * Specifies that a column has a {@code default} value specified in DDL.
*
* {@code @ColumnDefault} may be used in combination with:
*
@@ -38,18 +37,4 @@ public @interface ColumnDefault {
* @return a SQL expression that evaluates to the default column value
*/
String value();
-
- /**
- * The name of the generated column. Optional for a field or property
- * mapped to a single column.
- *
- * - If the column name is explicitly specified using the
- * {@link jakarta.persistence.Column @Column} annotation, the name given
- * here must match the name specified by
- * {@link jakarta.persistence.Column#name}.
- *
- Or, if the column name is inferred implicitly by Hibernate, the
- * name given here must match the inferred name.
- *
- */
- String name() default "";
}
diff --git a/hibernate-core/src/main/java/org/hibernate/annotations/GeneratedColumn.java b/hibernate-core/src/main/java/org/hibernate/annotations/GeneratedColumn.java
index 35b0ebbfd9..5428f33bba 100644
--- a/hibernate-core/src/main/java/org/hibernate/annotations/GeneratedColumn.java
+++ b/hibernate-core/src/main/java/org/hibernate/annotations/GeneratedColumn.java
@@ -17,8 +17,8 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* Specifies that a column is defined using a DDL {@code generated always as} clause
- * or equivalent, and whether Hibernate should fetch the generated value from the
- * database.
+ * or equivalent, and that Hibernate should fetch the generated value from the
+ * database after each SQL {@code INSERT} or {@code UPDATE}.
*
* @author Gavin King
*
@@ -34,18 +34,4 @@ public @interface GeneratedColumn {
* @return the SQL expression that is evaluated to generate the column value.
*/
String value();
-
- /**
- * The name of the generated column. Optional for a field or property mapped
- * to a single column.
- *
- * - If the column name is explicitly specified using the
- * {@link jakarta.persistence.Column @Column} annotation, the name given
- * here must match the name specified by
- * {@link jakarta.persistence.Column#name}.
- *
- Or, if the column name is inferred implicitly by Hibernate, the
- * name given here must match the inferred name.
- *
- */
- String name() default "";
}
diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3Column.java b/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3Column.java
index d413666732..1a4d5a89d1 100644
--- a/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3Column.java
+++ b/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3Column.java
@@ -10,6 +10,7 @@ import java.util.Map;
import org.hibernate.AnnotationException;
import org.hibernate.AssertionFailure;
+import org.hibernate.MappingException;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.GeneratedColumn;
import org.hibernate.annotations.ColumnTransformer;
@@ -642,8 +643,8 @@ public class Ejb3Column {
column.setPropertyHolder( propertyHolder );
column.setJoins( secondaryTables );
column.setBuildingContext( context );
- column.applyColumnDefault( inferredData, length==1 );
- column.applyGeneratedAs( inferredData, length==1 );
+ column.applyColumnDefault( inferredData, length );
+ column.applyGeneratedAs( inferredData, length );
column.extractDataFromPropertyData(inferredData);
column.bind();
columns[index] = column;
@@ -653,11 +654,14 @@ public class Ejb3Column {
return columns;
}
- private void applyColumnDefault(PropertyData inferredData, boolean implicit) {
+ private void applyColumnDefault(PropertyData inferredData, int length) {
final XProperty xProperty = inferredData.getProperty();
if ( xProperty != null ) {
ColumnDefault columnDefaultAnn = xProperty.getAnnotation( ColumnDefault.class );
- if ( columnDefaultAnn != null && isReferencedColumn( columnDefaultAnn.name(), implicit ) ) {
+ if ( columnDefaultAnn != null ) {
+ if (length!=1) {
+ throw new MappingException("@ColumnDefault may only be applied to single-column mappings");
+ }
setDefaultValue( columnDefaultAnn.value() );
}
}
@@ -668,11 +672,14 @@ public class Ejb3Column {
}
}
- private void applyGeneratedAs(PropertyData inferredData, boolean implicit) {
+ private void applyGeneratedAs(PropertyData inferredData, int length) {
final XProperty xProperty = inferredData.getProperty();
if ( xProperty != null ) {
GeneratedColumn generatedAnn = xProperty.getAnnotation( GeneratedColumn.class );
- if ( generatedAnn != null && isReferencedColumn( generatedAnn.name(), implicit ) ) {
+ if ( generatedAnn != null ) {
+ if (length!=1) {
+ throw new MappingException("@GeneratedColumn may only be applied to single-column mappings");
+ }
setGeneratedAs( generatedAnn.value() );
}
}
@@ -683,25 +690,6 @@ public class Ejb3Column {
}
}
- private boolean isReferencedColumn(String name, boolean implicit) {
- if ( name.isEmpty() && implicit ) {
- return true;
- }
- else {
- String columnName;
- if ( StringHelper.isNotEmpty( logicalColumnName ) ) {
- columnName = processColumnName( logicalColumnName, true );
- }
- else if ( propertyName != null ) {
- columnName = inferColumnName( propertyName );
- }
- else {
- return false;
- }
- return name.equals(columnName);
- }
- }
-
//must only be called after all setters are defined and before binding
private void extractDataFromPropertyData(PropertyData inferredData) {
if ( inferredData != null ) {
@@ -771,8 +759,8 @@ public class Ejb3Column {
column.setLogicalColumnName( propertyName + suffixForDefaultColumnName );
}
column.setImplicit( implicit );
- column.applyColumnDefault( inferredData, implicit );
- column.applyGeneratedAs( inferredData, implicit );
+ column.applyColumnDefault( inferredData, 1 );
+ column.applyGeneratedAs( inferredData, 1 );
column.extractDataFromPropertyData( inferredData );
column.bind();
diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/generated/always/GeneratedAlwaysTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/generated/always/GeneratedAlwaysTest.java
index a9edda2d8c..02862a6932 100644
--- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/generated/always/GeneratedAlwaysTest.java
+++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/generated/always/GeneratedAlwaysTest.java
@@ -61,7 +61,7 @@ public class GeneratedAlwaysTest {
@GeneratedColumn(value = "unitPrice*quantity")
private BigDecimal total;
@Column(name = "discountedTotal")
- @GeneratedColumn(name = "discountedTotal", value = "unitPrice*quantity*(1.0-discount/100.0)")
+ @GeneratedColumn(value = "unitPrice*quantity*(1.0-discount/100.0)")
private BigDecimal discounted;
public OrderLine() {}