remove name() from @ColumnDefault + @GeneratedColumn

after all that, we decided it wasn't necessary :-D
This commit is contained in:
Gavin King 2021-12-31 16:53:10 +01:00
parent 9beab38716
commit 4a88399bb5
4 changed files with 19 additions and 60 deletions

View File

@ -14,8 +14,7 @@ import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME; import static java.lang.annotation.RetentionPolicy.RUNTIME;
/** /**
* Specifies that a column has a {@code DEFAULT} value specified in DDL, * Specifies that a column has a {@code default} value specified in DDL.
* and whether Hibernate should fetch the defaulted value from the database.
* <p> * <p>
* {@code @ColumnDefault} may be used in combination with: * {@code @ColumnDefault} may be used in combination with:
* <ul> * <ul>
@ -38,18 +37,4 @@ public @interface ColumnDefault {
* @return a SQL expression that evaluates to the default column value * @return a SQL expression that evaluates to the default column value
*/ */
String value(); String value();
/**
* The name of the generated column. Optional for a field or property
* mapped to a single column.
* <ul>
* <li>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}.
* <li>Or, if the column name is inferred implicitly by Hibernate, the
* name given here must match the inferred name.
* </ul>
*/
String name() default "";
} }

View File

@ -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 * 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 * or equivalent, and that Hibernate should fetch the generated value from the
* database. * database after each SQL {@code INSERT} or {@code UPDATE}.
* *
* @author Gavin King * @author Gavin King
* *
@ -34,18 +34,4 @@ public @interface GeneratedColumn {
* @return the SQL expression that is evaluated to generate the column value. * @return the SQL expression that is evaluated to generate the column value.
*/ */
String value(); String value();
/**
* The name of the generated column. Optional for a field or property mapped
* to a single column.
* <ul>
* <li>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}.
* <li>Or, if the column name is inferred implicitly by Hibernate, the
* name given here must match the inferred name.
* </ul>
*/
String name() default "";
} }

View File

@ -10,6 +10,7 @@ import java.util.Map;
import org.hibernate.AnnotationException; import org.hibernate.AnnotationException;
import org.hibernate.AssertionFailure; import org.hibernate.AssertionFailure;
import org.hibernate.MappingException;
import org.hibernate.annotations.ColumnDefault; import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.GeneratedColumn; import org.hibernate.annotations.GeneratedColumn;
import org.hibernate.annotations.ColumnTransformer; import org.hibernate.annotations.ColumnTransformer;
@ -642,8 +643,8 @@ public class Ejb3Column {
column.setPropertyHolder( propertyHolder ); column.setPropertyHolder( propertyHolder );
column.setJoins( secondaryTables ); column.setJoins( secondaryTables );
column.setBuildingContext( context ); column.setBuildingContext( context );
column.applyColumnDefault( inferredData, length==1 ); column.applyColumnDefault( inferredData, length );
column.applyGeneratedAs( inferredData, length==1 ); column.applyGeneratedAs( inferredData, length );
column.extractDataFromPropertyData(inferredData); column.extractDataFromPropertyData(inferredData);
column.bind(); column.bind();
columns[index] = column; columns[index] = column;
@ -653,11 +654,14 @@ public class Ejb3Column {
return columns; return columns;
} }
private void applyColumnDefault(PropertyData inferredData, boolean implicit) { private void applyColumnDefault(PropertyData inferredData, int length) {
final XProperty xProperty = inferredData.getProperty(); final XProperty xProperty = inferredData.getProperty();
if ( xProperty != null ) { if ( xProperty != null ) {
ColumnDefault columnDefaultAnn = xProperty.getAnnotation( ColumnDefault.class ); 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() ); 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(); final XProperty xProperty = inferredData.getProperty();
if ( xProperty != null ) { if ( xProperty != null ) {
GeneratedColumn generatedAnn = xProperty.getAnnotation( GeneratedColumn.class ); 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() ); 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 //must only be called after all setters are defined and before binding
private void extractDataFromPropertyData(PropertyData inferredData) { private void extractDataFromPropertyData(PropertyData inferredData) {
if ( inferredData != null ) { if ( inferredData != null ) {
@ -771,8 +759,8 @@ public class Ejb3Column {
column.setLogicalColumnName( propertyName + suffixForDefaultColumnName ); column.setLogicalColumnName( propertyName + suffixForDefaultColumnName );
} }
column.setImplicit( implicit ); column.setImplicit( implicit );
column.applyColumnDefault( inferredData, implicit ); column.applyColumnDefault( inferredData, 1 );
column.applyGeneratedAs( inferredData, implicit ); column.applyGeneratedAs( inferredData, 1 );
column.extractDataFromPropertyData( inferredData ); column.extractDataFromPropertyData( inferredData );
column.bind(); column.bind();

View File

@ -61,7 +61,7 @@ public class GeneratedAlwaysTest {
@GeneratedColumn(value = "unitPrice*quantity") @GeneratedColumn(value = "unitPrice*quantity")
private BigDecimal total; private BigDecimal total;
@Column(name = "discountedTotal") @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; private BigDecimal discounted;
public OrderLine() {} public OrderLine() {}