Skip to content

Commit

Permalink
HHH-17448 - Add newly standard column annotation attributes to Hibern…
Browse files Browse the repository at this point in the history
…ate column annotations
  • Loading branch information
sebersole committed Aug 1, 2024
1 parent 365e9c4 commit fac934f
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@
*/
String columnName() default "";

/**
* (Optional) The SQL fragment that is used when
* generating the DDL for the column.
* <p>
* The DDL must be written in the native SQL dialect
* of the target database (it is not portable across databases).
*
* @since 7.0
*/
String options() default "";

/**
* (Optional) A comment to be applied to the column.
*
* @since 7.0
*/
String comment() default "";

/**
* The strategy to use for storing/reading values to/from the database.
* <p/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.hibernate.boot.model.relational.Database;
import org.hibernate.boot.spi.MetadataBuildingContext;
import org.hibernate.dialect.Dialect;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.mapping.BasicValue;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.SoftDeletable;
Expand Down Expand Up @@ -93,7 +94,13 @@ private static Column createSoftDeleteIndicatorColumn(
softDeleteColumn.setLength( 1 );
softDeleteColumn.setNullable( false );
softDeleteColumn.setUnique( false );
softDeleteColumn.setComment( "Soft-delete indicator" );
softDeleteColumn.setOptions( softDeleteConfig.options() );
if ( StringHelper.isEmpty( softDeleteConfig.comment() ) ) {
softDeleteColumn.setComment( "Soft-delete indicator" );
}
else {
softDeleteColumn.setComment( softDeleteConfig.comment() );
}

softDeleteColumn.setValue( softDeleteIndicatorValue );
softDeleteIndicatorValue.addColumn( softDeleteColumn );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
@jakarta.annotation.Generated("org.hibernate.orm.build.annotations.ClassGeneratorProcessor")
public class SoftDeleteAnnotation implements SoftDelete {
private String columnName;
private String options;
private String comment;
private org.hibernate.annotations.SoftDeleteType strategy;
private java.lang.Class<? extends jakarta.persistence.AttributeConverter<java.lang.Boolean, ?>> converter;

Expand All @@ -38,6 +40,8 @@ public SoftDeleteAnnotation(SourceModelBuildingContext modelContext) {
public SoftDeleteAnnotation(SoftDelete annotation, SourceModelBuildingContext modelContext) {
this.columnName = annotation.columnName();
this.strategy = annotation.strategy();
this.options = annotation.options();
this.comment = annotation.comment();
this.converter = annotation.converter();
}

Expand All @@ -52,6 +56,8 @@ public SoftDeleteAnnotation(AnnotationInstance annotation, SourceModelBuildingCo
modelContext
);
this.strategy = extractJandexValue( annotation, HibernateAnnotations.SOFT_DELETE, "strategy", modelContext );
this.options = extractJandexValue( annotation, HibernateAnnotations.SOFT_DELETE, "options", modelContext );
this.comment = extractJandexValue( annotation, HibernateAnnotations.SOFT_DELETE, "comment", modelContext );
this.converter = extractJandexValue( annotation, HibernateAnnotations.SOFT_DELETE, "converter", modelContext );
}

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

@Override
public String options() {
return options;
}

public void options(String options) {
this.options = options;
}

@Override
public String comment() {
return comment;
}

public void comment(String comment) {
this.comment = comment;
}

@Override
public org.hibernate.annotations.SoftDeleteType strategy() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.orm.test.softdelete;

import org.hibernate.annotations.SoftDelete;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.RootClass;

import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.DomainModelScope;
import org.hibernate.testing.schema.SchemaCreateHelper;
import org.junit.jupiter.api.Test;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author Steve Ebersole
*/
@SuppressWarnings("JUnitMalformedDeclaration")
public class SoftDeleteColumnConfigTests {
@Test
@DomainModel(annotatedClasses = Thing.class)
void verifyModel(DomainModelScope modelScope) {
final RootClass entityBinding = (RootClass) modelScope.getEntityBinding( Thing.class );
final Column softDeleteColumn = entityBinding.getSoftDeleteColumn();
assertThat( softDeleteColumn.getOptions() ).isEqualTo( "do_it=true" );
assertThat( softDeleteColumn.getComment() ).isEqualTo( "Explicit soft-delete comment" );

final String ddl = SchemaCreateHelper.toCreateDdl( modelScope.getDomainModel() );
assertThat( ddl ).contains( "do_it=true" );
assertThat( ddl ).contains( "Explicit soft-delete comment" );
}

@Entity(name="Thing")
@Table(name="Thing")
@SoftDelete( comment = "Explicit soft-delete comment", options = "do_it=true" )
public static class Thing {
@Id
private Integer id;
private String name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package org.hibernate.testing.schema;

import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.sql.Connection;
import java.util.HashMap;
Expand Down Expand Up @@ -80,6 +81,24 @@ public static void toWriter(Metadata metadata, Writer writer) {
);
}

public static String toCreateDdl(Metadata metadata) {
final StringWriter writer = new StringWriter();

final ServiceRegistry serviceRegistry = ( (MetadataImplementor) metadata ).getMetadataBuildingOptions().getServiceRegistry();
final Map<String,Object> settings = serviceRegistry.requireService( ConfigurationService.class ).getSettings();
final Map<String,Object> copy = new HashMap<>( settings );
copy.put( SchemaToolingSettings.JAKARTA_HBM2DDL_SCRIPTS_ACTION, Action.CREATE_ONLY );
copy.put( SchemaToolingSettings.JAKARTA_HBM2DDL_SCRIPTS_CREATE_TARGET, writer );
SchemaManagementToolCoordinator.process(
metadata,
serviceRegistry,
copy,
DelayedDropRegistryNotAvailableImpl.INSTANCE
);

return writer.toString();
}

@AllowSysOut
public static void createOnlyToStdOut(Metadata metadata, ServiceRegistry serviceRegistry) {
createOnlyToWriter( metadata, serviceRegistry, new OutputStreamWriter( System.out ) );
Expand Down

0 comments on commit fac934f

Please sign in to comment.