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

Filter by extension

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
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<EventType> 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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 );
}
}
}
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!--
~ SPDX-License-Identifier: Apache-2.0
~ Copyright Red Hat Inc. and Hibernate Authors
-->
<entity-mappings xmlns="http://www.hibernate.org/xsd/orm/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="7.0">

<entity name="tractor" class="org.hibernate.orm.test.annotations.various.Tractor" access="FIELD" metadata-complete="true">
<attributes>
<id name="id" />
<basic name="model"/>
<basic name="serialNumber">
<generated>ALWAYS</generated>
</basic>
</attributes>
</entity>
</entity-mappings>