allow @UuidGenerator to be applied to a non-@Id field

This commit is contained in:
Gavin 2023-04-06 01:33:13 +02:00 committed by Gavin King
parent 783da2a906
commit 2927c006b2
4 changed files with 85 additions and 7 deletions

View File

@ -13,6 +13,7 @@ import java.util.UUID;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* Specifies that an entity identifier is generated as an RFC 4122 UUID.
@ -23,8 +24,9 @@ import static java.lang.annotation.ElementType.METHOD;
*
* @since 6.0
*/
@IdGeneratorType( org.hibernate.id.uuid.UuidGenerator.class )
@Retention(RetentionPolicy.RUNTIME)
@IdGeneratorType(org.hibernate.id.uuid.UuidGenerator.class)
@ValueGenerationType(generatedBy = org.hibernate.id.uuid.UuidGenerator.class)
@Retention(RUNTIME)
@Target({ FIELD, METHOD })
public @interface UuidGenerator {

View File

@ -14,6 +14,7 @@ import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.generator.EventType;
import org.hibernate.generator.EventTypeSets;
import org.hibernate.generator.GeneratorCreationContext;
import org.hibernate.id.factory.spi.CustomIdGeneratorCreationContext;
import org.hibernate.generator.BeforeExecutionGenerator;
import org.hibernate.type.descriptor.java.UUIDJavaType;
@ -36,10 +37,9 @@ public class UuidGenerator implements BeforeExecutionGenerator {
private final ValueGenerator generator;
private final ValueTransformer valueTransformer;
public UuidGenerator(
private UuidGenerator(
org.hibernate.annotations.UuidGenerator config,
Member idMember,
CustomIdGeneratorCreationContext creationContext) {
Member idMember) {
if ( config.style() == TIME ) {
generator = new CustomVersionOneStrategy();
}
@ -63,6 +63,20 @@ public class UuidGenerator implements BeforeExecutionGenerator {
}
}
public UuidGenerator(
org.hibernate.annotations.UuidGenerator config,
Member idMember,
CustomIdGeneratorCreationContext creationContext) {
this(config, idMember);
}
public UuidGenerator(
org.hibernate.annotations.UuidGenerator config,
Member idMember,
GeneratorCreationContext creationContext) {
this(config, idMember);
}
/**
* @return {@link EventTypeSets#INSERT_ONLY}
*/

View File

@ -0,0 +1,49 @@
/*
* 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.id.uuid.annotation;
import jakarta.persistence.Basic;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import org.hibernate.annotations.UuidGenerator;
import java.util.UUID;
@Entity(name = "TheOtherEntity")
@Table(name = "TheOtherEntity")
public class TheOtherEntity {
@Id @GeneratedValue
public Long pk;
@UuidGenerator
public UUID id;
@Basic
public String name;
private TheOtherEntity() {
// for Hibernate use
}
public TheOtherEntity(String name) {
this.name = name;
}
public UUID getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -17,7 +17,7 @@ import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
@DomainModel( annotatedClasses = TheEntity.class )
@DomainModel( annotatedClasses = {TheEntity.class, TheOtherEntity.class} )
@SessionFactory
public class UuidGeneratorAnnotationTests {
@Test
@ -36,7 +36,20 @@ public class UuidGeneratorAnnotationTests {
@Test
public void basicUseTest(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
session.persist( new TheEntity( "steve" ) );
TheEntity steve = new TheEntity("steve");
session.persist( steve );
session.flush();
assertThat( steve.id ).isNotNull();
} );
}
@Test
public void nonPkUseTest(SessionFactoryScope scope) {
scope.inTransaction( (session) -> {
TheOtherEntity gavin = new TheOtherEntity("gavin");
session.persist( gavin );
session.flush();
assertThat( gavin.id ).isNotNull();
} );
}
}