diff --git a/hibernate-core/src/main/java/org/hibernate/boot/models/xml/internal/XmlAnnotationHelper.java b/hibernate-core/src/main/java/org/hibernate/boot/models/xml/internal/XmlAnnotationHelper.java index 36608dcaed68..54bb3cb56d32 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/models/xml/internal/XmlAnnotationHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/models/xml/internal/XmlAnnotationHelper.java @@ -20,6 +20,7 @@ import java.time.OffsetTime; import java.time.ZonedDateTime; import java.util.ArrayList; +import java.util.EnumSet; import java.util.List; import java.util.UUID; import java.util.function.Consumer; @@ -32,6 +33,7 @@ import org.hibernate.annotations.ResultCheckStyle; import org.hibernate.annotations.SecondaryRow; import org.hibernate.boot.internal.LimitedCollectionClassification; +import org.hibernate.boot.jaxb.mapping.GenerationTiming; import org.hibernate.boot.jaxb.mapping.spi.JaxbAssociationOverrideImpl; import org.hibernate.boot.jaxb.mapping.spi.JaxbAttributeOverrideImpl; import org.hibernate.boot.jaxb.mapping.spi.JaxbBasicMapping; @@ -90,6 +92,7 @@ import org.hibernate.boot.models.xml.spi.XmlDocument; import org.hibernate.boot.models.xml.spi.XmlDocumentContext; import org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle; +import org.hibernate.generator.EventType; import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.collections.CollectionHelper; import org.hibernate.models.ModelsException; @@ -479,6 +482,27 @@ public static void applyNationalized( memberDetails.applyAnnotationUsage( HibernateAnnotations.NATIONALIZED, xmlDocumentContext.getModelBuildingContext() ); } + public static void applyGenerated( + GenerationTiming timing, + MutableMemberDetails memberDetails, + XmlDocumentContext xmlDocumentContext) { + if ( timing == null ) { + return; + } + + EnumSet eventTypes = timing.getEventTypes(); + if ( eventTypes == null ) { + return; + } + + final GeneratedAnnotation generatedAnn = (GeneratedAnnotation) memberDetails.applyAnnotationUsage( + HibernateAnnotations.GENERATED, + xmlDocumentContext.getModelBuildingContext() + ); + + generatedAnn.event( eventTypes.toArray( new EventType[0] ) ); + } + public static void applyGeneratedValue( JaxbGeneratedValueImpl jaxbGeneratedValue, MutableMemberDetails memberDetails, diff --git a/hibernate-core/src/main/java/org/hibernate/boot/models/xml/internal/attr/BasicAttributeProcessing.java b/hibernate-core/src/main/java/org/hibernate/boot/models/xml/internal/attr/BasicAttributeProcessing.java index 60628ed0b286..8849ddd0df24 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/models/xml/internal/attr/BasicAttributeProcessing.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/models/xml/internal/attr/BasicAttributeProcessing.java @@ -78,8 +78,8 @@ else if ( jaxbBasic.getColumn() != null ) { XmlAnnotationHelper.applyLob( jaxbBasic.getLob(), memberDetails, xmlDocumentContext ); XmlAnnotationHelper.applyEnumerated( jaxbBasic.getEnumerated(), memberDetails, xmlDocumentContext ); XmlAnnotationHelper.applyNationalized( jaxbBasic.getNationalized(), memberDetails, xmlDocumentContext ); + XmlAnnotationHelper.applyGenerated( jaxbBasic.getGenerated(), memberDetails, xmlDocumentContext ); - // todo : value generation // todo : ... return memberDetails; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/various/OrmXmlGeneratedTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/various/OrmXmlGeneratedTest.java new file mode 100644 index 000000000000..e5f134418498 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/various/OrmXmlGeneratedTest.java @@ -0,0 +1,52 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.orm.test.annotations.various; + +import org.hibernate.boot.Metadata; +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.StandardServiceRegistry; +import org.hibernate.boot.spi.MetadataImplementor; +import org.hibernate.generator.EventType; +import org.hibernate.mapping.GeneratorCreator; +import org.hibernate.mapping.PersistentClass; +import org.hibernate.testing.ServiceRegistryBuilder; +import org.hibernate.testing.orm.junit.BaseUnitTest; +import org.hibernate.testing.util.uuid.IdGeneratorCreationContext; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +@BaseUnitTest +class OrmXmlGeneratedTest { + + @Test + void testOrmXmlDefinedGenerated() { + StandardServiceRegistry ssr = ServiceRegistryBuilder.buildServiceRegistry(); + + try { + MetadataSources ms = new MetadataSources( ssr ); + ms.addResource( "org/hibernate/orm/test/annotations/generated/ormXml/orm.xml" ); + + Metadata metadata = ms.buildMetadata(); + + PersistentClass entityBinding = metadata.getEntityBinding( Tractor.class.getName() ); + GeneratorCreator generator = entityBinding + .getProperty( "serialNumber" ) + .getValueGeneratorCreator(); + + assertThat( generator ) + .extracting( creator -> creator.createGenerator( + new IdGeneratorCreationContext( (MetadataImplementor) metadata, entityBinding.getRootClass() ) + ) ) + .satisfies( gen -> + assertThat( gen.getEventTypes() ) + .containsExactly( EventType.INSERT, EventType.UPDATE, EventType.FORCE_INCREMENT ) + ); + } + finally { + ServiceRegistryBuilder.destroy( ssr ); + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/various/Tractor.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/various/Tractor.java new file mode 100644 index 000000000000..4c2b50d71251 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/various/Tractor.java @@ -0,0 +1,13 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.orm.test.annotations.various; + +public class Tractor { + + public Long id; + public String model; + public int serialNumber; + +} diff --git a/hibernate-core/src/test/resources/org/hibernate/orm/test/annotations/generated/ormXml/orm.xml b/hibernate-core/src/test/resources/org/hibernate/orm/test/annotations/generated/ormXml/orm.xml new file mode 100644 index 000000000000..6b91fa2599d9 --- /dev/null +++ b/hibernate-core/src/test/resources/org/hibernate/orm/test/annotations/generated/ormXml/orm.xml @@ -0,0 +1,17 @@ + + + + + + + + + ALWAYS + + + +