|
| 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.Embeddable; |
| 8 | +import jakarta.persistence.EmbeddedId; |
| 9 | +import jakarta.persistence.Entity; |
| 10 | +import jakarta.persistence.GeneratedValue; |
| 11 | +import jakarta.persistence.Id; |
| 12 | +import org.hibernate.annotations.IdGeneratorType; |
| 13 | +import org.hibernate.annotations.ValueGenerationType; |
| 14 | +import org.hibernate.engine.spi.SharedSessionContractImplementor; |
| 15 | +import org.hibernate.generator.BeforeExecutionGenerator; |
| 16 | +import org.hibernate.generator.EventType; |
| 17 | +import org.hibernate.generator.EventTypeSets; |
| 18 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 19 | +import org.hibernate.testing.orm.junit.Jira; |
| 20 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 21 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 22 | +import org.junit.jupiter.api.AfterAll; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import java.lang.annotation.Retention; |
| 26 | +import java.lang.annotation.Target; |
| 27 | +import java.util.EnumSet; |
| 28 | +import java.util.UUID; |
| 29 | + |
| 30 | +import static java.lang.annotation.ElementType.FIELD; |
| 31 | +import static java.lang.annotation.ElementType.METHOD; |
| 32 | +import static java.lang.annotation.RetentionPolicy.RUNTIME; |
| 33 | +import static org.assertj.core.api.Assertions.assertThat; |
| 34 | + |
| 35 | +@SessionFactory |
| 36 | +@DomainModel(annotatedClasses = { |
| 37 | + BeforeExecutionAssignedValuesTest.EntityWithGeneratedId.class, |
| 38 | + BeforeExecutionAssignedValuesTest.GeneratedCompositeId.class, |
| 39 | + BeforeExecutionAssignedValuesTest.EntityWithGeneratedEmbeddedId.class, |
| 40 | + BeforeExecutionAssignedValuesTest.EntityWithGeneratedProperty.class, |
| 41 | +}) |
| 42 | +@Jira("https://hibernate.atlassian.net/browse/HHH-19320") |
| 43 | +class BeforeExecutionAssignedValuesTest { |
| 44 | + @Test |
| 45 | + void testAssignedId(SessionFactoryScope scope) { |
| 46 | + final EntityWithGeneratedId entity1 = new EntityWithGeneratedId( "assigned-id", "assigned-entity" ); |
| 47 | + scope.inTransaction( session -> session.persist( entity1 ) ); |
| 48 | + assertThat( entity1.getGeneratedId() ).isEqualTo( "assigned-id" ); |
| 49 | + |
| 50 | + final EntityWithGeneratedId entity2 = new EntityWithGeneratedId( "stateless-id", "stateless-entity" ); |
| 51 | + scope.inStatelessTransaction( session -> session.insert( entity2 ) ); |
| 52 | + assertThat( entity2.getGeneratedId() ).isEqualTo( "stateless-id" ); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void testGeneratedId(SessionFactoryScope scope) { |
| 57 | + final EntityWithGeneratedId entity = new EntityWithGeneratedId( null, "assigned-entity" ); |
| 58 | + scope.inTransaction( session -> session.persist( entity ) ); |
| 59 | + assertThat( entity.getGeneratedId() ).isNotNull(); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void testAssignedEmbeddedId(SessionFactoryScope scope) { |
| 64 | + final EntityWithGeneratedEmbeddedId entity1 = new EntityWithGeneratedEmbeddedId( |
| 65 | + new GeneratedCompositeId( "assigned-1", null), |
| 66 | + "generated-entity" |
| 67 | + ); |
| 68 | + scope.inTransaction( session -> session.persist( entity1 ) ); |
| 69 | + assertThat( entity1.getId().getId1() ).isEqualTo( "assigned-1" ); |
| 70 | + assertThat( entity1.getId().getId2() ).isNotNull(); |
| 71 | + |
| 72 | + final EntityWithGeneratedEmbeddedId entity2 = new EntityWithGeneratedEmbeddedId( |
| 73 | + new GeneratedCompositeId( "new-assigned-1", "assigned-2"), |
| 74 | + "generated-entity" |
| 75 | + ); |
| 76 | + scope.inTransaction( session -> session.persist( entity2 ) ); |
| 77 | + assertThat( entity2.getId().getId1() ).isEqualTo( "new-assigned-1" ); |
| 78 | + assertThat( entity2.getId().getId2() ).isEqualTo( "assigned-2" ); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void testGeneratedEmbeddedId(SessionFactoryScope scope) { |
| 83 | + final EntityWithGeneratedEmbeddedId entity = new EntityWithGeneratedEmbeddedId( |
| 84 | + new GeneratedCompositeId(), |
| 85 | + "generated-entity" |
| 86 | + ); |
| 87 | + scope.inTransaction( session -> session.persist( entity ) ); |
| 88 | + assertThat( entity.getId().getId1() ).isNotNull(); |
| 89 | + assertThat( entity.getId().getId2() ).isNotNull(); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void testInsertAssignedProperty(SessionFactoryScope scope) { |
| 94 | + final String assigned = "assigned-property"; |
| 95 | + final EntityWithGeneratedProperty entity = new EntityWithGeneratedProperty( 1L, assigned ); |
| 96 | + scope.inTransaction( session -> session.persist( entity ) ); |
| 97 | + assertThat( entity.getGeneratedProperty() ).isEqualTo( assigned ); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + void testGeneratedPropertyAndUpdate(SessionFactoryScope scope) { |
| 102 | + final EntityWithGeneratedProperty entity = new EntityWithGeneratedProperty( 2L, null ); |
| 103 | + scope.inTransaction( session -> { |
| 104 | + session.persist( entity ); |
| 105 | + session.flush(); |
| 106 | + |
| 107 | + assertThat( entity.getGeneratedProperty() ).isNotNull(); |
| 108 | + |
| 109 | + // test update |
| 110 | + entity.setGeneratedProperty( "new-assigned-property" ); |
| 111 | + } ); |
| 112 | + |
| 113 | + assertThat( entity.getGeneratedProperty() ).isEqualTo( "new-assigned-property" ); |
| 114 | + } |
| 115 | + |
| 116 | + @AfterAll |
| 117 | + public void tearDown(SessionFactoryScope scope) { |
| 118 | + scope.getSessionFactory().getSchemaManager().truncateMappedObjects(); |
| 119 | + } |
| 120 | + |
| 121 | + @Entity(name = "EntityWithGeneratedId") |
| 122 | + static class EntityWithGeneratedId { |
| 123 | + @Id |
| 124 | + @GeneratedValue |
| 125 | + @AssignableGenerator |
| 126 | + private String generatedId; |
| 127 | + |
| 128 | + private String name; |
| 129 | + |
| 130 | + public EntityWithGeneratedId() { |
| 131 | + } |
| 132 | + |
| 133 | + public EntityWithGeneratedId(String generatedId, String name) { |
| 134 | + this.generatedId = generatedId; |
| 135 | + this.name = name; |
| 136 | + } |
| 137 | + |
| 138 | + public String getGeneratedId() { |
| 139 | + return generatedId; |
| 140 | + } |
| 141 | + |
| 142 | + public String getName() { |
| 143 | + return name; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + @Embeddable |
| 148 | + static class GeneratedCompositeId { |
| 149 | + @AssignableGenerator |
| 150 | + private String id1; |
| 151 | + |
| 152 | + @AssignableGenerator |
| 153 | + private String id2; |
| 154 | + |
| 155 | + public GeneratedCompositeId() { |
| 156 | + } |
| 157 | + |
| 158 | + public GeneratedCompositeId(String id1, String id2) { |
| 159 | + this.id1 = id1; |
| 160 | + this.id2 = id2; |
| 161 | + } |
| 162 | + |
| 163 | + public String getId1() { |
| 164 | + return id1; |
| 165 | + } |
| 166 | + |
| 167 | + public String getId2() { |
| 168 | + return id2; |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + @Entity(name = "EntityWithGeneratedEmbeddedId") |
| 173 | + static class EntityWithGeneratedEmbeddedId { |
| 174 | + @EmbeddedId |
| 175 | + private GeneratedCompositeId id; |
| 176 | + |
| 177 | + private String name; |
| 178 | + |
| 179 | + public EntityWithGeneratedEmbeddedId() { |
| 180 | + } |
| 181 | + |
| 182 | + public EntityWithGeneratedEmbeddedId(GeneratedCompositeId id, String name) { |
| 183 | + this.id = id; |
| 184 | + this.name = name; |
| 185 | + } |
| 186 | + |
| 187 | + public GeneratedCompositeId getId() { |
| 188 | + return id; |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + @Entity(name = "EntityWithGeneratedProperty") |
| 193 | + static class EntityWithGeneratedProperty { |
| 194 | + @Id |
| 195 | + private Long id; |
| 196 | + |
| 197 | + @AssignableGenerator |
| 198 | + private String generatedProperty; |
| 199 | + |
| 200 | + public EntityWithGeneratedProperty() { |
| 201 | + } |
| 202 | + |
| 203 | + public EntityWithGeneratedProperty(Long id, String generatedProperty) { |
| 204 | + this.id = id; |
| 205 | + this.generatedProperty = generatedProperty; |
| 206 | + } |
| 207 | + |
| 208 | + public Long getId() { |
| 209 | + return id; |
| 210 | + } |
| 211 | + |
| 212 | + public String getGeneratedProperty() { |
| 213 | + return generatedProperty; |
| 214 | + } |
| 215 | + |
| 216 | + public void setGeneratedProperty(String generatedProperty) { |
| 217 | + this.generatedProperty = generatedProperty; |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + @IdGeneratorType(AssignedIdGenerator.class) |
| 222 | + @ValueGenerationType(generatedBy = AssignedGenerator.class) |
| 223 | + @Retention(RUNTIME) |
| 224 | + @Target({FIELD, METHOD}) |
| 225 | + @interface AssignableGenerator { |
| 226 | + } |
| 227 | + |
| 228 | + public static class AssignedGenerator implements BeforeExecutionGenerator { |
| 229 | + @Override |
| 230 | + public Object generate( |
| 231 | + SharedSessionContractImplementor session, |
| 232 | + Object owner, |
| 233 | + Object currentValue, |
| 234 | + EventType eventType) { |
| 235 | + if ( currentValue != null ) { |
| 236 | + return currentValue; |
| 237 | + } |
| 238 | + return UUID.randomUUID().toString(); |
| 239 | + } |
| 240 | + |
| 241 | + @Override |
| 242 | + public EnumSet<EventType> getEventTypes() { |
| 243 | + return EventTypeSets.INSERT_AND_UPDATE; |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + public static class AssignedIdGenerator extends AssignedGenerator { |
| 248 | + @Override |
| 249 | + public EnumSet<EventType> getEventTypes() { |
| 250 | + return EventTypeSets.INSERT_ONLY; |
| 251 | + } |
| 252 | + |
| 253 | + @Override |
| 254 | + public boolean allowAssignedIdentifiers() { |
| 255 | + return true; |
| 256 | + } |
| 257 | + } |
| 258 | +} |
0 commit comments