Skip to content

Commit 1e6c2d4

Browse files
committed
[#1906] Add test for Support @IdGeneratorType
1 parent 02b81ab commit 1e6c2d4

File tree

2 files changed

+226
-0
lines changed

2 files changed

+226
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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.generator.EventType;
10+
import org.hibernate.reactive.id.ReactiveIdentifierGenerator;
11+
import org.hibernate.reactive.session.ReactiveConnectionSupplier;
12+
import org.hibernate.reactive.util.impl.CompletionStages;
13+
14+
import org.junit.jupiter.api.Test;
15+
16+
import io.vertx.junit5.Timeout;
17+
import io.vertx.junit5.VertxTestContext;
18+
import jakarta.persistence.Entity;
19+
import jakarta.persistence.Id;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
import java.util.Collection;
25+
import java.util.EnumSet;
26+
import java.util.List;
27+
import java.util.concurrent.CompletionStage;
28+
import java.util.concurrent.atomic.AtomicLong;
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 BeforeExecutionIdGeneratorTypeTest 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();
44+
test(
45+
context, openSession()
46+
.thenCompose( session -> session.persist( person ) )
47+
.thenAccept( v -> {
48+
assertThat( person.getId() ).isNotNull();
49+
} )
50+
);
51+
}
52+
53+
@Entity(name = "Person")
54+
public static class Person {
55+
@Id
56+
@SimpleId
57+
long id;
58+
59+
String name;
60+
61+
public Person() {
62+
}
63+
64+
public Person(String name) {
65+
this.name = name;
66+
}
67+
68+
public long getId() {
69+
return id;
70+
}
71+
72+
public String getName() {
73+
return name;
74+
}
75+
}
76+
77+
@Target(ElementType.FIELD)
78+
@Retention(RetentionPolicy.RUNTIME)
79+
@IdGeneratorType(SimpleGenerator.class)
80+
public @interface SimpleId {
81+
}
82+
83+
public static class SimpleGenerator implements ReactiveIdentifierGenerator<Long> {
84+
85+
private AtomicLong sequence = new AtomicLong( 1 );
86+
87+
public SimpleGenerator() {
88+
}
89+
90+
@Override
91+
public boolean generatedOnExecution() {
92+
return false;
93+
}
94+
95+
@Override
96+
public EnumSet<EventType> getEventTypes() {
97+
return EnumSet.of( EventType.INSERT );
98+
}
99+
100+
101+
@Override
102+
public CompletionStage<Long> generate(ReactiveConnectionSupplier session, Object entity) {
103+
return CompletionStages.completedFuture( sequence.getAndIncrement() );
104+
}
105+
}
106+
107+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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

Comments
 (0)