|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.idgen.userdefined; |
| 6 | + |
| 7 | +import jakarta.persistence.Entity; |
| 8 | +import jakarta.persistence.GeneratedValue; |
| 9 | +import jakarta.persistence.Id; |
| 10 | +import org.hibernate.annotations.IdGeneratorType; |
| 11 | +import org.hibernate.annotations.ValueGenerationType; |
| 12 | +import org.hibernate.engine.spi.SharedSessionContractImplementor; |
| 13 | +import org.hibernate.generator.BeforeExecutionGenerator; |
| 14 | +import org.hibernate.generator.EventType; |
| 15 | +import org.hibernate.generator.EventTypeSets; |
| 16 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 17 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 18 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 19 | +import org.junit.jupiter.api.AfterAll; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import java.lang.annotation.Retention; |
| 23 | +import java.lang.annotation.Target; |
| 24 | +import java.util.EnumSet; |
| 25 | +import java.util.UUID; |
| 26 | + |
| 27 | +import static java.lang.annotation.ElementType.FIELD; |
| 28 | +import static java.lang.annotation.ElementType.METHOD; |
| 29 | +import static java.lang.annotation.RetentionPolicy.RUNTIME; |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; |
| 31 | + |
| 32 | +@SessionFactory |
| 33 | +@DomainModel(annotatedClasses = { |
| 34 | + BeforeExecutionAssignedValuesTest.EntityWithGeneratedId.class, |
| 35 | + BeforeExecutionAssignedValuesTest.EntityWithGeneratedProperty.class, |
| 36 | +}) |
| 37 | +class BeforeExecutionAssignedValuesTest { |
| 38 | + @Test |
| 39 | + void testAssignedId(SessionFactoryScope scope) { |
| 40 | + final String assigned = "assigned-id"; |
| 41 | + final EntityWithGeneratedId entity = new EntityWithGeneratedId( assigned, "assigned-entity" ); |
| 42 | + scope.inTransaction( session -> session.persist( entity ) ); |
| 43 | + assertThat( entity.getGeneratedId() ).isEqualTo( assigned ); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + void testGeneratedId(SessionFactoryScope scope) { |
| 48 | + final EntityWithGeneratedId entity = new EntityWithGeneratedId( null, "assigned-entity" ); |
| 49 | + scope.inTransaction( session -> session.persist( entity ) ); |
| 50 | + assertThat( entity.getGeneratedId() ).isNotNull(); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void testInsertAssignedProperty(SessionFactoryScope scope) { |
| 55 | + final String assigned = "assigned-property"; |
| 56 | + final EntityWithGeneratedProperty entity = new EntityWithGeneratedProperty( 1L, assigned ); |
| 57 | + scope.inTransaction( session -> session.persist( entity ) ); |
| 58 | + assertThat( entity.getGeneratedProperty() ).isEqualTo( assigned ); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void testGeneratedPropertyAndUpdate(SessionFactoryScope scope) { |
| 63 | + final EntityWithGeneratedProperty entity = new EntityWithGeneratedProperty( 2L, null ); |
| 64 | + scope.inTransaction( session -> { |
| 65 | + session.persist( entity ); |
| 66 | + session.flush(); |
| 67 | + |
| 68 | + assertThat( entity.getGeneratedProperty() ).isNotNull(); |
| 69 | + |
| 70 | + // test update |
| 71 | + entity.setGeneratedProperty( "new-assigned-property" ); |
| 72 | + } ); |
| 73 | + |
| 74 | + assertThat( entity.getGeneratedProperty() ).isEqualTo( "new-assigned-property" ); |
| 75 | + } |
| 76 | + |
| 77 | + @AfterAll |
| 78 | + public void tearDown(SessionFactoryScope scope) { |
| 79 | + scope.getSessionFactory().getSchemaManager().truncateMappedObjects(); |
| 80 | + } |
| 81 | + |
| 82 | + @Entity(name = "EntityWithGeneratedId") |
| 83 | + static class EntityWithGeneratedId { |
| 84 | + @Id |
| 85 | + @GeneratedValue |
| 86 | + @AssignableGenerator |
| 87 | + private String generatedId; |
| 88 | + |
| 89 | + private String name; |
| 90 | + |
| 91 | + public EntityWithGeneratedId() { |
| 92 | + } |
| 93 | + |
| 94 | + public EntityWithGeneratedId(String generatedId, String name) { |
| 95 | + this.generatedId = generatedId; |
| 96 | + this.name = name; |
| 97 | + } |
| 98 | + |
| 99 | + public String getGeneratedId() { |
| 100 | + return generatedId; |
| 101 | + } |
| 102 | + |
| 103 | + public String getName() { |
| 104 | + return name; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @Entity(name = "EntityWithGeneratedProperty") |
| 109 | + static class EntityWithGeneratedProperty { |
| 110 | + @Id |
| 111 | + private Long id; |
| 112 | + |
| 113 | + @AssignableGenerator |
| 114 | + private String generatedProperty; |
| 115 | + |
| 116 | + public EntityWithGeneratedProperty() { |
| 117 | + } |
| 118 | + |
| 119 | + public EntityWithGeneratedProperty(Long id, String generatedProperty) { |
| 120 | + this.id = id; |
| 121 | + this.generatedProperty = generatedProperty; |
| 122 | + } |
| 123 | + |
| 124 | + public Long getId() { |
| 125 | + return id; |
| 126 | + } |
| 127 | + |
| 128 | + public String getGeneratedProperty() { |
| 129 | + return generatedProperty; |
| 130 | + } |
| 131 | + |
| 132 | + public void setGeneratedProperty(String generatedProperty) { |
| 133 | + this.generatedProperty = generatedProperty; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + @IdGeneratorType(AssignedIdGenerator.class) |
| 138 | + @ValueGenerationType(generatedBy = AssignedGenerator.class) |
| 139 | + @Retention(RUNTIME) |
| 140 | + @Target({FIELD, METHOD}) |
| 141 | + @interface AssignableGenerator { |
| 142 | + } |
| 143 | + |
| 144 | + public static class AssignedGenerator implements BeforeExecutionGenerator { |
| 145 | + @Override |
| 146 | + public Object generate( |
| 147 | + SharedSessionContractImplementor session, |
| 148 | + Object owner, |
| 149 | + Object currentValue, |
| 150 | + EventType eventType) { |
| 151 | + if ( currentValue != null ) { |
| 152 | + return currentValue; |
| 153 | + } |
| 154 | + return UUID.randomUUID().toString(); |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public EnumSet<EventType> getEventTypes() { |
| 159 | + return EventTypeSets.INSERT_AND_UPDATE; |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + public static class AssignedIdGenerator extends AssignedGenerator { |
| 164 | + @Override |
| 165 | + public EnumSet<EventType> getEventTypes() { |
| 166 | + return EventTypeSets.INSERT_ONLY; |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public boolean allowAssignedIdentifiers() { |
| 171 | + return true; |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments