Skip to content

Commit 78abaab

Browse files
dreab8DavideD
authored andcommitted
[#1906] Test @IdGeneratorType support
1 parent 02b81ab commit 78abaab

File tree

2 files changed

+274
-0
lines changed

2 files changed

+274
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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 java.lang.annotation.ElementType;
9+
import java.lang.annotation.Retention;
10+
import java.lang.annotation.RetentionPolicy;
11+
import java.lang.annotation.Target;
12+
import java.util.Collection;
13+
import java.util.EnumSet;
14+
import java.util.List;
15+
import java.util.Objects;
16+
import java.util.concurrent.CompletionStage;
17+
import java.util.concurrent.atomic.AtomicLong;
18+
19+
import org.hibernate.annotations.IdGeneratorType;
20+
import org.hibernate.generator.EventType;
21+
import org.hibernate.reactive.id.ReactiveIdentifierGenerator;
22+
import org.hibernate.reactive.session.ReactiveConnectionSupplier;
23+
import org.hibernate.reactive.util.impl.CompletionStages;
24+
25+
import org.junit.jupiter.api.Test;
26+
27+
import io.vertx.junit5.Timeout;
28+
import io.vertx.junit5.VertxTestContext;
29+
import jakarta.persistence.Entity;
30+
import jakarta.persistence.Id;
31+
import jakarta.persistence.Table;
32+
import jakarta.persistence.Tuple;
33+
34+
import static java.util.concurrent.TimeUnit.MINUTES;
35+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
36+
37+
@Timeout(value = 10, timeUnit = MINUTES)
38+
public class BeforeExecutionIdGeneratorTypeTest extends BaseReactiveTest {
39+
40+
@Override
41+
protected Collection<Class<?>> annotatedEntities() {
42+
return List.of( Person.class );
43+
}
44+
45+
@Test
46+
public void testPersistWithoutTransaction(VertxTestContext context) {
47+
final Person person = new Person( "Janet" );
48+
// The id should be set by the persist
49+
assertThat( person.getId() ).isNull();
50+
test( context, getMutinySessionFactory()
51+
// The value won't be persisted on the database, but the id should have been assigned anyway
52+
.withSession( session -> session.persist( person ) )
53+
.invoke( () -> assertThat( person.getId() ).isGreaterThan( 0 ) )
54+
// Check that the value has not been saved
55+
.chain( () -> getMutinySessionFactory().withTransaction( s -> s
56+
.createNativeQuery( "select * from Person", Tuple.class ).getSingleResultOrNull() )
57+
)
58+
.invoke( result -> assertThat( result ).isNull() )
59+
);
60+
}
61+
62+
@Test
63+
public void testPersistWithTransaction(VertxTestContext context) {
64+
final Person person = new Person( "Baldrick" );
65+
// The id should be set by the persist
66+
assertThat( person.getId() ).isNull();
67+
test( context, getMutinySessionFactory()
68+
.withTransaction( session -> session.persist( person ) )
69+
.invoke( () -> assertThat( person.getId() ).isGreaterThan( 0 ) )
70+
// Check that the value has been saved
71+
.chain( () -> getMutinySessionFactory().withTransaction( s -> s
72+
.createNativeQuery( "select id,name from Person", Object[].class ).getSingleResult() )
73+
)
74+
.invoke( row -> assertThat( row ).containsExactly( person.id, person.name ) )
75+
76+
);
77+
}
78+
79+
@Entity(name = "Person")
80+
@Table(name = "Person")
81+
public static class Person {
82+
@Id
83+
@SimpleId
84+
Long id;
85+
86+
String name;
87+
88+
public Person() {
89+
}
90+
91+
public Person(String name) {
92+
this.name = name;
93+
}
94+
95+
public Long getId() {
96+
return id;
97+
}
98+
99+
public String getName() {
100+
return name;
101+
}
102+
103+
@Override
104+
public boolean equals(Object o) {
105+
if ( o == null || getClass() != o.getClass() ) {
106+
return false;
107+
}
108+
Person person = (Person) o;
109+
return Objects.equals( name, person.name );
110+
}
111+
112+
@Override
113+
public int hashCode() {
114+
return Objects.hashCode( name );
115+
}
116+
117+
@Override
118+
public String toString() {
119+
return id + ":" + name;
120+
}
121+
}
122+
123+
@Target(ElementType.FIELD)
124+
@Retention(RetentionPolicy.RUNTIME)
125+
@IdGeneratorType(SimpleGenerator.class)
126+
public @interface SimpleId {
127+
}
128+
129+
public static class SimpleGenerator implements ReactiveIdentifierGenerator<Long> {
130+
131+
private AtomicLong sequence = new AtomicLong( 1 );
132+
133+
public SimpleGenerator() {
134+
}
135+
136+
@Override
137+
public boolean generatedOnExecution() {
138+
return false;
139+
}
140+
141+
@Override
142+
public EnumSet<EventType> getEventTypes() {
143+
return EnumSet.of( EventType.INSERT );
144+
}
145+
146+
147+
@Override
148+
public CompletionStage<Long> generate(ReactiveConnectionSupplier session, Object entity) {
149+
return CompletionStages.completedFuture( sequence.getAndIncrement() );
150+
}
151+
}
152+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
22+
import java.lang.annotation.ElementType;
23+
import java.lang.annotation.Retention;
24+
import java.lang.annotation.RetentionPolicy;
25+
import java.lang.annotation.Target;
26+
import java.util.Collection;
27+
import java.util.Date;
28+
import java.util.EnumSet;
29+
import java.util.List;
30+
31+
import static java.util.concurrent.TimeUnit.MINUTES;
32+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
33+
34+
@Timeout(value = 10, timeUnit = MINUTES)
35+
public class OnExecutionGeneratorTypeTest extends BaseReactiveTest {
36+
37+
@Override
38+
protected Collection<Class<?>> annotatedEntities() {
39+
return List.of( Person.class );
40+
}
41+
42+
@Test
43+
public void testPersist(VertxTestContext context) {
44+
Person person = new Person( "Davide" );
45+
test(
46+
context, getSessionFactory()
47+
.withTransaction( session -> session.persist( person ) )
48+
.thenAccept( v -> {
49+
assertThat( person.getId() ).isNotNull();
50+
assertThat( person.getCreated() ).isNotNull();
51+
} )
52+
);
53+
}
54+
55+
@Entity(name = "Person")
56+
public static class Person {
57+
@Id
58+
@FunctionCreatedValueId
59+
Date id;
60+
61+
String name;
62+
63+
@FunctionCreatedValue
64+
Date created;
65+
66+
public Person() {
67+
}
68+
69+
public Person(String name) {
70+
this.name = name;
71+
}
72+
73+
public Date getId() {
74+
return id;
75+
}
76+
77+
public String getName() {
78+
return name;
79+
}
80+
81+
public Date getCreated() {
82+
return created;
83+
}
84+
}
85+
86+
@Target(ElementType.FIELD)
87+
@Retention(RetentionPolicy.RUNTIME)
88+
@IdGeneratorType(FunctionCreationValueGeneration.class)
89+
public @interface FunctionCreatedValueId {
90+
}
91+
92+
@Target(ElementType.FIELD)
93+
@Retention(RetentionPolicy.RUNTIME)
94+
@ValueGenerationType(generatedBy = FunctionCreationValueGeneration.class)
95+
public @interface FunctionCreatedValue {
96+
}
97+
98+
public static class FunctionCreationValueGeneration
99+
implements ReactiveOnExecutionGenerator {
100+
101+
@Override
102+
public boolean referenceColumnsInSql(Dialect dialect) {
103+
return true;
104+
}
105+
106+
@Override
107+
public boolean writePropertyValue() {
108+
return false;
109+
}
110+
111+
@Override
112+
public String[] getReferencedColumnValues(Dialect dialect) {
113+
return new String[] { dialect.currentTimestamp() };
114+
}
115+
116+
@Override
117+
public EnumSet<EventType> getEventTypes() {
118+
return EventTypeSets.INSERT_ONLY;
119+
}
120+
}
121+
122+
}

0 commit comments

Comments
 (0)