Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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.annotations;

import java.lang.annotation.Target;
import java.lang.annotation.Retention;

import org.hibernate.Incubating;
import org.hibernate.cfg.AvailableSettings;

import jakarta.persistence.SequenceGenerator;
import jakarta.persistence.TableGenerator;

import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* For use with the standard {@linkplain SequenceGenerator @SequenceGenerator}
* providing additional Hibernate configuration of the generator
*
* @author Steve Ebersole
*/
@Target({ANNOTATION_TYPE, TYPE, METHOD, FIELD})
@Retention(RUNTIME)
@Incubating
public @interface SequenceGeneratorExtension {
/**
* The name of the associated {@linkplain SequenceGenerator @SequenceGenerator}.
*
* @see SequenceGenerator#name()
*/
String name();

/**
* Specific type of {@linkplain org.hibernate.id.enhanced.Optimizer optimizer} to use,
* if any, to optimize access to the underlying sequence.
*
* @see org.hibernate.id.enhanced.OptimizerDescriptor
* @see org.hibernate.id.enhanced.StandardOptimizerDescriptor
*/
String optimizerName() default "";

/**
* Specifies the suffix to use for an implicit sequence name - appended to the
* {@value org.hibernate.id.enhanced.SequenceStyleGenerator#IMPLICIT_NAME_BASE}
* parameter (which is the entity name or collection role).
*/
String perEntitySuffix() default "_SEQ";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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.annotations;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import org.hibernate.Incubating;
import org.hibernate.cfg.AvailableSettings;

import jakarta.persistence.TableGenerator;

import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* For use with the standard {@linkplain TableGenerator @TableGenerator}
* providing additional Hibernate configuration of the generator
*
* @see TableGenerator
* @see org.hibernate.id.enhanced.TableGenerator
*
* @author Steve Ebersole
*/
@Target({ANNOTATION_TYPE, TYPE, METHOD, FIELD})
@Retention(RUNTIME)
@Incubating
public @interface TableGeneratorExtension {
/**
* The name of the associated {@linkplain TableGenerator @TableGenerator}.
*
* @see TableGenerator#name()
*/
String name();

/**
* Specific type of {@linkplain org.hibernate.id.enhanced.Optimizer optimizer} to use,
* if any, to optimize access to the underlying table.
*
* @see org.hibernate.id.enhanced.OptimizerDescriptor
* @see org.hibernate.id.enhanced.StandardOptimizerDescriptor
*/
String optimizerName() default "";

/**
* Hibernate's default is to use a single row for all generators mapped to it,
* using {@link org.hibernate.id.enhanced.TableGenerator#DEF_SEGMENT_VALUE}
* as the {@link org.hibernate.id.enhanced.TableGenerator#DEF_SEGMENT_COLUMN}.
* <p/>
* This tells Hibernate to instead use a row for each mapped generator. The
* entity name mapped to the generator table
*
* @see org.hibernate.id.enhanced.TableGenerator#CONFIG_PREFER_SEGMENT_PER_ENTITY
* @see AvailableSettings#ID_DB_STRUCTURE_NAMING_STRATEGY
*/
boolean perEntity() default false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;

import org.hibernate.AnnotationException;
Expand All @@ -29,7 +30,9 @@
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.Columns;
import org.hibernate.annotations.ManyToAny;
import org.hibernate.annotations.SequenceGeneratorExtension;
import org.hibernate.annotations.Subselect;
import org.hibernate.annotations.TableGeneratorExtension;
import org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor;
import org.hibernate.annotations.common.annotationfactory.AnnotationFactory;
import org.hibernate.annotations.common.reflection.AnnotationReader;
Expand Down Expand Up @@ -102,6 +105,7 @@
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.jpa.AvailableHints;

import jakarta.persistence.Access;
Expand Down Expand Up @@ -442,7 +446,9 @@ private void initAnnotations() {
addIfNotNull( annotationList, getDiscriminatorValue( managedTypeOverride, defaults ) );
addIfNotNull( annotationList, getDiscriminatorColumn( managedTypeOverride, defaults ) );
addIfNotNull( annotationList, getSequenceGenerator( managedTypeOverride, defaults ) );
addIfNotNull( annotationList, getSequenceGeneratorExtension( managedTypeOverride, defaults ) );
addIfNotNull( annotationList, getTableGenerator( managedTypeOverride, defaults ) );
addIfNotNull( annotationList, getTableGeneratorExtension( managedTypeOverride, defaults ) );
addIfNotNull( annotationList, getNamedQueries( managedTypeOverride, defaults ) );
addIfNotNull( annotationList, getNamedNativeQueries( managedTypeOverride, defaults ) );
addIfNotNull( annotationList, getNamedStoredProcedureQueries( managedTypeOverride, defaults ) );
Expand Down Expand Up @@ -497,7 +503,9 @@ else if ( className != null ) { //&& propertyName != null ) { //always true but
getManyToAny( annotationList, defaults );
getElementCollection( annotationList, defaults );
addIfNotNull( annotationList, getSequenceGenerator( elementsForProperty, defaults ) );
addIfNotNull( annotationList, getSequenceGeneratorExtension( elementsForProperty, defaults ) );
addIfNotNull( annotationList, getTableGenerator( elementsForProperty, defaults ) );
addIfNotNull( annotationList, getTableGeneratorExtension( elementsForProperty, defaults ) );
addIfNotNull( annotationList, getConvertsForAttribute( elementsForProperty, defaults ) );
}
processEventAnnotations( annotationList, defaults );
Expand Down Expand Up @@ -734,6 +742,22 @@ private void addIfNotNull(List<Annotation> annotationList, Annotation annotation
}
}

private Annotation getTableGeneratorExtension(PropertyMappingElementCollector elementsForProperty, XMLContext.Default defaults) {
for ( JaxbId element : elementsForProperty.getId() ) {
JaxbTableGenerator subelement = element.getTableGenerator();
if ( subelement != null ) {
return buildTableGeneratorExtensionAnnotation( subelement, defaults );
}
}

if ( defaults.canUseJavaAnnotations() ) {
return getPhysicalAnnotation( TableGeneratorExtension.class );
}

return null;

}

private Annotation getTableGenerator(PropertyMappingElementCollector elementsForProperty, XMLContext.Default defaults) {
for ( JaxbId element : elementsForProperty.getId() ) {
JaxbTableGenerator subelement = element.getTableGenerator();
Expand All @@ -749,6 +773,21 @@ private Annotation getTableGenerator(PropertyMappingElementCollector elementsFor
}
}

private Annotation getSequenceGeneratorExtension(PropertyMappingElementCollector elementsForProperty, XMLContext.Default defaults) {
for ( JaxbId element : elementsForProperty.getId() ) {
JaxbSequenceGenerator subelement = element.getSequenceGenerator();
if ( subelement != null ) {
return buildSequenceGeneratorExtensionAnnotation( subelement );
}
}

if ( elementsForProperty.isEmpty() && defaults.canUseJavaAnnotations() ) {
return getPhysicalAnnotation( SequenceGeneratorExtension.class );
}

return null;
}

private Annotation getSequenceGenerator(PropertyMappingElementCollector elementsForProperty, XMLContext.Default defaults) {
for ( JaxbId element : elementsForProperty.getId() ) {
JaxbSequenceGenerator subelement = element.getSequenceGenerator();
Expand Down Expand Up @@ -1705,8 +1744,10 @@ private void getId(List<Annotation> annotationList, XMLContext.Default defaults)
//FIXME: fix the priority of xml over java for generator names
annotation = getTableGenerator( element.getTableGenerator(), defaults );
addIfNotNull( annotationList, annotation );
addIfNotNull( annotationList, getTableGeneratorExtension( element.getTableGenerator(), defaults ) );
annotation = getSequenceGenerator( element.getSequenceGenerator(), defaults );
addIfNotNull( annotationList, annotation );
addIfNotNull( annotationList, getSequenceGeneratorExtension( element.getSequenceGenerator(), defaults ) );
AnnotationDescriptor id = new AnnotationDescriptor( Id.class );
annotationList.add( AnnotationFactory.create( id ) );
getAccessType( annotationList, element.getAccess() );
Expand Down Expand Up @@ -2649,13 +2690,32 @@ private static void addSynchronizationsHint(
hints.put( AvailableHints.HINT_NATIVE_SPACES, sb.toString() );
}

private TableGeneratorExtension getTableGeneratorExtension(ManagedType root, XMLContext.Default defaults) {
return getTableGeneratorExtension(
root instanceof JaxbEntity ? ( (JaxbEntity) root ).getTableGenerator() : null,
defaults
);
}

private TableGenerator getTableGenerator(ManagedType root, XMLContext.Default defaults) {
return getTableGenerator(
root instanceof JaxbEntity ? ( (JaxbEntity) root ).getTableGenerator() : null,
defaults
);
}

private TableGeneratorExtension getTableGeneratorExtension(JaxbTableGenerator element, XMLContext.Default defaults) {
if ( element != null ) {
return buildTableGeneratorExtensionAnnotation( element, defaults );
}

if ( defaults.canUseJavaAnnotations() ) {
return getPhysicalAnnotation( TableGeneratorExtension.class );
}

return null;
}

private TableGenerator getTableGenerator(JaxbTableGenerator element, XMLContext.Default defaults) {
if ( element != null ) {
return buildTableGeneratorAnnotation( element, defaults );
Expand Down Expand Up @@ -2694,6 +2754,30 @@ && isNotEmpty( defaults.getSchema() ) ) {
}
}

public static TableGeneratorExtension buildTableGeneratorExtensionAnnotation(
JaxbTableGenerator element,
XMLContext.Default defaults) {
final Boolean perEntity = element.isPerEntity();
final String optimizerName = element.getOptimizer();
if ( perEntity != Boolean.TRUE && StringHelper.isEmpty( optimizerName ) ) {
return null;
}

final AnnotationDescriptor ad = new AnnotationDescriptor( TableGeneratorExtension.class );
copyAttribute( ad, "name", element.getName(), false );


if ( StringHelper.isNotEmpty( optimizerName ) ) {
copyAttribute( ad, "optimizer", optimizerName, false );
}

if ( perEntity ) {
copyAttribute( ad, "perEntity", true, false );
}

return AnnotationFactory.create( ad );
}

public static TableGenerator buildTableGeneratorAnnotation(JaxbTableGenerator element, XMLContext.Default defaults) {
AnnotationDescriptor ad = new AnnotationDescriptor( TableGenerator.class );
copyAttribute( ad, "name", element.getName(), false );
Expand All @@ -2714,26 +2798,59 @@ && isNotEmpty( defaults.getSchema() ) ) {
&& isNotEmpty( defaults.getCatalog() ) ) {
ad.setValue( "catalog", defaults.getCatalog() );
}

return AnnotationFactory.create( ad );
}

private SequenceGeneratorExtension getSequenceGeneratorExtension(ManagedType root, XMLContext.Default defaults) {
return getSequenceGeneratorExtension(
root instanceof JaxbEntity ? ( (JaxbEntity) root ).getSequenceGenerator() : null,
defaults
);
}

private SequenceGenerator getSequenceGenerator(ManagedType root, XMLContext.Default defaults) {
return getSequenceGenerator(
root instanceof JaxbEntity ? ( (JaxbEntity) root ).getSequenceGenerator() : null,
defaults
);
}

private SequenceGeneratorExtension getSequenceGeneratorExtension(JaxbSequenceGenerator element, XMLContext.Default defaults) {
if ( element != null ) {
return buildSequenceGeneratorExtensionAnnotation( element );
}

if ( defaults.canUseJavaAnnotations() ) {
return getPhysicalAnnotation( SequenceGeneratorExtension.class );
}

return null;
}

private SequenceGenerator getSequenceGenerator(JaxbSequenceGenerator element, XMLContext.Default defaults) {
if ( element != null ) {
return buildSequenceGeneratorAnnotation( element );
}
else if ( defaults.canUseJavaAnnotations() ) {

if ( defaults.canUseJavaAnnotations() ) {
return getPhysicalAnnotation( SequenceGenerator.class );
}
else {

return null;
}

public static SequenceGeneratorExtension buildSequenceGeneratorExtensionAnnotation(JaxbSequenceGenerator element) {
if ( element == null ) {
return null;
}

final AnnotationDescriptor ad = new AnnotationDescriptor( SequenceGeneratorExtension.class );
copyAttribute( ad, "name", element.getName(), false );
copyAttribute( ad, "optimizerName", element.getOptimizer(), false );
copyAttribute( ad, "perEntitySuffix", element.getPerEntitySuffix(), false );

return AnnotationFactory.create( ad );
}

public static SequenceGenerator buildSequenceGeneratorAnnotation(JaxbSequenceGenerator element) {
Expand Down
Loading