|
| 1 | +/* Hibernate, Relational Persistence for Idiomatic Java |
| 2 | + * |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * Copyright: Red Hat Inc. and Hibernate Authors |
| 5 | + */ |
| 6 | +package org.hibernate.reactive; |
| 7 | + |
| 8 | +import org.hibernate.annotations.IdGeneratorType; |
| 9 | +import org.hibernate.annotations.ValueGenerationType; |
| 10 | +import org.hibernate.dialect.Dialect; |
| 11 | +import org.hibernate.generator.EventType; |
| 12 | +import org.hibernate.generator.EventTypeSets; |
| 13 | +import org.hibernate.reactive.id.ReactiveOnExecutionGenerator; |
| 14 | + |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +import io.vertx.junit5.Timeout; |
| 18 | +import io.vertx.junit5.VertxTestContext; |
| 19 | +import jakarta.persistence.Entity; |
| 20 | +import jakarta.persistence.Id; |
| 21 | +import java.lang.annotation.ElementType; |
| 22 | +import java.lang.annotation.Retention; |
| 23 | +import java.lang.annotation.RetentionPolicy; |
| 24 | +import java.lang.annotation.Target; |
| 25 | +import java.util.Collection; |
| 26 | +import java.util.Date; |
| 27 | +import java.util.EnumSet; |
| 28 | +import java.util.List; |
| 29 | + |
| 30 | +import static java.util.concurrent.TimeUnit.MINUTES; |
| 31 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 32 | + |
| 33 | +@Timeout(value = 10, timeUnit = MINUTES) |
| 34 | +public class OnExecutionGeneratorTypeTest extends BaseReactiveTest { |
| 35 | + |
| 36 | + @Override |
| 37 | + protected Collection<Class<?>> annotatedEntities() { |
| 38 | + return List.of( Person.class ); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void testPersist(VertxTestContext context) { |
| 43 | + Person person = new Person( "Davide" ); |
| 44 | + test( context, getSessionFactory() |
| 45 | + .withTransaction( session -> session.persist( person ) ) |
| 46 | + .thenAccept( v -> { |
| 47 | + assertThat( person.getId() ).isNotNull(); |
| 48 | + assertThat( person.getCreated() ).isNotNull(); |
| 49 | + } ) |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + @Entity(name = "Person") |
| 54 | + public static class Person { |
| 55 | + @Id |
| 56 | + @FunctionCreatedValueId |
| 57 | + Date id; |
| 58 | + |
| 59 | + String name; |
| 60 | + |
| 61 | + @FunctionCreatedValue |
| 62 | + Date created; |
| 63 | + |
| 64 | + public Person() { |
| 65 | + } |
| 66 | + |
| 67 | + public Person(String name) { |
| 68 | + this.name = name; |
| 69 | + } |
| 70 | + |
| 71 | + public Date getId() { |
| 72 | + return id; |
| 73 | + } |
| 74 | + |
| 75 | + public String getName() { |
| 76 | + return name; |
| 77 | + } |
| 78 | + |
| 79 | + public Date getCreated() { |
| 80 | + return created; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Target(ElementType.FIELD) |
| 85 | + @Retention(RetentionPolicy.RUNTIME) |
| 86 | + @IdGeneratorType(FunctionCreationValueGeneration.class) |
| 87 | + public @interface FunctionCreatedValueId { |
| 88 | + } |
| 89 | + |
| 90 | + @Target(ElementType.FIELD) |
| 91 | + @Retention(RetentionPolicy.RUNTIME) |
| 92 | + @ValueGenerationType(generatedBy = FunctionCreationValueGeneration.class) |
| 93 | + public @interface FunctionCreatedValue {} |
| 94 | + |
| 95 | + public static class FunctionCreationValueGeneration |
| 96 | + implements ReactiveOnExecutionGenerator { |
| 97 | + |
| 98 | + @Override |
| 99 | + public boolean referenceColumnsInSql(Dialect dialect) { |
| 100 | + return true; |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public boolean writePropertyValue() { |
| 105 | + return false; |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public String[] getReferencedColumnValues(Dialect dialect) { |
| 110 | + return new String[] { dialect.currentTimestamp() }; |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public EnumSet<EventType> getEventTypes() { |
| 115 | + return EventTypeSets.INSERT_ONLY; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments