Skip to content

Commit 2273365

Browse files
committed
HHH-17448 - Add newly standard column annotation attributes to Hibernate column annotations
1 parent c1124d4 commit 2273365

File tree

5 files changed

+119
-1
lines changed

5 files changed

+119
-1
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,24 @@
6868
*/
6969
String columnName() default "";
7070

71+
/**
72+
* (Optional) The SQL fragment that is used when
73+
* generating the DDL for the column.
74+
* <p>
75+
* The DDL must be written in the native SQL dialect
76+
* of the target database (it is not portable across databases).
77+
*
78+
* @since 7.0
79+
*/
80+
String options() default "";
81+
82+
/**
83+
* (Optional) A comment to be applied to the column.
84+
*
85+
* @since 7.0
86+
*/
87+
String comment() default "";
88+
7189
/**
7290
* The strategy to use for storing/reading values to/from the database.
7391
* <p/>

hibernate-core/src/main/java/org/hibernate/boot/model/internal/SoftDeleteHelper.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.hibernate.boot.model.relational.Database;
1414
import org.hibernate.boot.spi.MetadataBuildingContext;
1515
import org.hibernate.dialect.Dialect;
16+
import org.hibernate.internal.util.StringHelper;
1617
import org.hibernate.mapping.BasicValue;
1718
import org.hibernate.mapping.Column;
1819
import org.hibernate.mapping.SoftDeletable;
@@ -93,7 +94,13 @@ private static Column createSoftDeleteIndicatorColumn(
9394
softDeleteColumn.setLength( 1 );
9495
softDeleteColumn.setNullable( false );
9596
softDeleteColumn.setUnique( false );
96-
softDeleteColumn.setComment( "Soft-delete indicator" );
97+
softDeleteColumn.setOptions( softDeleteConfig.options() );
98+
if ( StringHelper.isEmpty( softDeleteConfig.comment() ) ) {
99+
softDeleteColumn.setComment( "Soft-delete indicator" );
100+
}
101+
else {
102+
softDeleteColumn.setComment( softDeleteConfig.comment() );
103+
}
97104

98105
softDeleteColumn.setValue( softDeleteIndicatorValue );
99106
softDeleteIndicatorValue.addColumn( softDeleteColumn );

hibernate-core/src/main/java/org/hibernate/boot/models/annotations/internal/SoftDeleteAnnotation.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
@jakarta.annotation.Generated("org.hibernate.orm.build.annotations.ClassGeneratorProcessor")
2121
public class SoftDeleteAnnotation implements SoftDelete {
2222
private String columnName;
23+
private String options;
24+
private String comment;
2325
private org.hibernate.annotations.SoftDeleteType strategy;
2426
private java.lang.Class<? extends jakarta.persistence.AttributeConverter<java.lang.Boolean, ?>> converter;
2527

@@ -38,6 +40,8 @@ public SoftDeleteAnnotation(SourceModelBuildingContext modelContext) {
3840
public SoftDeleteAnnotation(SoftDelete annotation, SourceModelBuildingContext modelContext) {
3941
this.columnName = annotation.columnName();
4042
this.strategy = annotation.strategy();
43+
this.options = annotation.options();
44+
this.comment = annotation.comment();
4145
this.converter = annotation.converter();
4246
}
4347

@@ -52,6 +56,8 @@ public SoftDeleteAnnotation(AnnotationInstance annotation, SourceModelBuildingCo
5256
modelContext
5357
);
5458
this.strategy = extractJandexValue( annotation, HibernateAnnotations.SOFT_DELETE, "strategy", modelContext );
59+
this.options = extractJandexValue( annotation, HibernateAnnotations.SOFT_DELETE, "options", modelContext );
60+
this.comment = extractJandexValue( annotation, HibernateAnnotations.SOFT_DELETE, "comment", modelContext );
5561
this.converter = extractJandexValue( annotation, HibernateAnnotations.SOFT_DELETE, "converter", modelContext );
5662
}
5763

@@ -69,6 +75,23 @@ public void columnName(String value) {
6975
this.columnName = value;
7076
}
7177

78+
@Override
79+
public String options() {
80+
return options;
81+
}
82+
83+
public void options(String options) {
84+
this.options = options;
85+
}
86+
87+
@Override
88+
public String comment() {
89+
return comment;
90+
}
91+
92+
public void comment(String comment) {
93+
this.comment = comment;
94+
}
7295

7396
@Override
7497
public org.hibernate.annotations.SoftDeleteType strategy() {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.orm.test.softdelete;
8+
9+
import org.hibernate.annotations.SoftDelete;
10+
import org.hibernate.mapping.Column;
11+
import org.hibernate.mapping.PersistentClass;
12+
import org.hibernate.mapping.RootClass;
13+
14+
import org.hibernate.testing.orm.junit.DomainModel;
15+
import org.hibernate.testing.orm.junit.DomainModelScope;
16+
import org.hibernate.testing.schema.SchemaCreateHelper;
17+
import org.junit.jupiter.api.Test;
18+
19+
import jakarta.persistence.Entity;
20+
import jakarta.persistence.Id;
21+
import jakarta.persistence.Table;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
/**
26+
* @author Steve Ebersole
27+
*/
28+
@SuppressWarnings("JUnitMalformedDeclaration")
29+
public class SoftDeleteColumnConfigTests {
30+
@Test
31+
@DomainModel(annotatedClasses = Thing.class)
32+
void verifyModel(DomainModelScope modelScope) {
33+
final RootClass entityBinding = (RootClass) modelScope.getEntityBinding( Thing.class );
34+
final Column softDeleteColumn = entityBinding.getSoftDeleteColumn();
35+
assertThat( softDeleteColumn.getOptions() ).isEqualTo( "do_it=true" );
36+
assertThat( softDeleteColumn.getComment() ).isEqualTo( "Explicit soft-delete comment" );
37+
38+
final String ddl = SchemaCreateHelper.toCreateDdl( modelScope.getDomainModel() );
39+
assertThat( ddl ).contains( "do_it=true" );
40+
assertThat( ddl ).contains( "Explicit soft-delete comment" );
41+
}
42+
43+
@Entity(name="Thing")
44+
@Table(name="Thing")
45+
@SoftDelete( comment = "Explicit soft-delete comment", options = "do_it=true" )
46+
public static class Thing {
47+
@Id
48+
private Integer id;
49+
private String name;
50+
}
51+
}

hibernate-testing/src/main/java/org/hibernate/testing/schema/SchemaCreateHelper.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package org.hibernate.testing.schema;
88

99
import java.io.OutputStreamWriter;
10+
import java.io.StringWriter;
1011
import java.io.Writer;
1112
import java.sql.Connection;
1213
import java.util.HashMap;
@@ -80,6 +81,24 @@ public static void toWriter(Metadata metadata, Writer writer) {
8081
);
8182
}
8283

84+
public static String toCreateDdl(Metadata metadata) {
85+
final StringWriter writer = new StringWriter();
86+
87+
final ServiceRegistry serviceRegistry = ( (MetadataImplementor) metadata ).getMetadataBuildingOptions().getServiceRegistry();
88+
final Map<String,Object> settings = serviceRegistry.requireService( ConfigurationService.class ).getSettings();
89+
final Map<String,Object> copy = new HashMap<>( settings );
90+
copy.put( SchemaToolingSettings.JAKARTA_HBM2DDL_SCRIPTS_ACTION, Action.CREATE_ONLY );
91+
copy.put( SchemaToolingSettings.JAKARTA_HBM2DDL_SCRIPTS_CREATE_TARGET, writer );
92+
SchemaManagementToolCoordinator.process(
93+
metadata,
94+
serviceRegistry,
95+
copy,
96+
DelayedDropRegistryNotAvailableImpl.INSTANCE
97+
);
98+
99+
return writer.toString();
100+
}
101+
83102
@AllowSysOut
84103
public static void createOnlyToStdOut(Metadata metadata, ServiceRegistry serviceRegistry) {
85104
createOnlyToWriter( metadata, serviceRegistry, new OutputStreamWriter( System.out ) );

0 commit comments

Comments
 (0)