Skip to content

Commit c505102

Browse files
committed
ORM test
1 parent 614c780 commit c505102

File tree

1 file changed

+88
-71
lines changed

1 file changed

+88
-71
lines changed

hibernate-reactive-core/src/test/java/org/hibernate/reactive/ORMReactivePersistenceTest.java

Lines changed: 88 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,33 @@
1111

1212
import org.hibernate.Session;
1313
import org.hibernate.SessionFactory;
14+
import org.hibernate.annotations.JdbcType;
15+
import org.hibernate.annotations.JdbcTypeCode;
1416
import org.hibernate.boot.registry.StandardServiceRegistry;
1517
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
1618
import org.hibernate.cfg.Configuration;
19+
import org.hibernate.dialect.PostgreSQLJsonPGObjectJsonbType;
1720
import org.hibernate.reactive.annotations.DisabledFor;
21+
import org.hibernate.type.SqlTypes;
1822

1923
import org.junit.jupiter.api.AfterEach;
2024
import org.junit.jupiter.api.BeforeEach;
2125
import org.junit.jupiter.api.Test;
2226

2327
import io.vertx.junit5.Timeout;
2428
import io.vertx.junit5.VertxTestContext;
29+
import jakarta.persistence.Embeddable;
2530
import jakarta.persistence.Entity;
2631
import jakarta.persistence.Id;
2732
import jakarta.persistence.Table;
2833

2934
import static java.util.concurrent.TimeUnit.MINUTES;
35+
import static org.assertj.core.api.Assertions.assertThat;
3036
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.COCKROACHDB;
3137
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.DB2;
3238
import static org.hibernate.reactive.containers.DatabaseConfiguration.dbType;
3339
import static org.hibernate.reactive.provider.Settings.DIALECT;
3440
import static org.hibernate.reactive.provider.Settings.DRIVER;
35-
import static org.junit.jupiter.api.Assertions.assertEquals;
3641

3742
@Timeout(value = 10, timeUnit = MINUTES)
3843

@@ -48,9 +53,12 @@ public class ORMReactivePersistenceTest extends BaseReactiveTest {
4853

4954
@Override
5055
protected Collection<Class<?>> annotatedEntities() {
51-
return List.of( Flour.class );
56+
return List.of( Book.class );
5257
}
5358

59+
final Book fakeHistory = new Book( 3, "Fake History", new Book.Author( "Jo", "Hedwig Teeuwisse") );
60+
final Book theBookOfM = new Book( 5, "The Book of M", new Book.Author( "Peng", "Shepherd") );
61+
5462
@BeforeEach
5563
public void prepareOrmFactory() {
5664
Configuration configuration = constructConfiguration();
@@ -69,94 +77,97 @@ public void closeOrmFactory() {
6977
ormFactory.close();
7078
}
7179

72-
@Test
73-
public void testORMWithStageSession(VertxTestContext context) {
74-
final Flour almond = new Flour( 1, "Almond", "made from ground almonds.", "Gluten free" );
75-
76-
try (Session session = ormFactory.openSession()) {
77-
session.beginTransaction();
78-
session.persist( almond );
79-
session.getTransaction().commit();
80-
}
81-
82-
// Check database with Stage session and verify 'almond' flour exists
83-
test( context, openSession()
84-
.thenCompose( stageSession -> stageSession.find( Flour.class, almond.id ) )
85-
.thenAccept( entityFound -> assertEquals( almond, entityFound ) )
86-
);
87-
}
88-
8980
@Test
9081
public void testORMWitMutinySession(VertxTestContext context) {
91-
final Flour rose = new Flour( 2, "Rose", "made from ground rose pedals.", "Full fragrance" );
82+
try (Session ormSession = ormFactory.openSession()) {
83+
ormSession.beginTransaction();
84+
ormSession.persist( theBookOfM );
85+
ormSession.persist( fakeHistory );
86+
ormSession.getTransaction().commit();
87+
}
9288

9389
try (Session ormSession = ormFactory.openSession()) {
9490
ormSession.beginTransaction();
95-
ormSession.persist( rose );
91+
Book result = ormSession.createNativeQuery( "select * from BookWithJson where id = 3", Book.class )
92+
.getSingleResult();
93+
assertThat( result ).isEqualTo( theBookOfM );
9694
ormSession.getTransaction().commit();
9795
}
9896

99-
// Check database with Mutiny session and verify 'rose' flour exists
100-
test( context, openMutinySession()
101-
.chain( session -> session.find( Flour.class, rose.id ) )
102-
.invoke( foundRose -> assertEquals( rose, foundRose ) )
103-
);
10497
}
10598

106-
@Entity(name = "Flour")
107-
@Table(name = "Flour")
108-
public static class Flour {
99+
@Entity(name = "Book")
100+
@Table(name = "BookWithJson")
101+
public static class Book {
102+
109103
@Id
110-
private Integer id;
111-
private String name;
112-
private String description;
113-
private String type;
104+
Integer id;
114105

115-
public Flour() {
116-
}
106+
String title;
117107

118-
public Flour(Integer id, String name, String description, String type) {
119-
this.id = id;
120-
this.name = name;
121-
this.description = description;
122-
this.type = type;
123-
}
108+
@JdbcTypeCode(SqlTypes.JSON)
109+
@JdbcType(PostgreSQLJsonPGObjectJsonbType.class)
110+
Author author;
124111

125-
public Integer getId() {
126-
return id;
127-
}
112+
@Embeddable
113+
public static class Author {
114+
private String name;
115+
private String surname;
128116

129-
public void setId(Integer id) {
130-
this.id = id;
131-
}
117+
public Author() {
118+
}
132119

133-
public String getName() {
134-
return name;
135-
}
120+
public Author(String name, String surname) {
121+
this.name = name;
122+
this.surname = surname;
123+
}
136124

137-
public void setName(String name) {
138-
this.name = name;
139-
}
125+
public String getName() {
126+
return name;
127+
}
140128

141-
public String getDescription() {
142-
return description;
143-
}
129+
public void setName(String name) {
130+
this.name = name;
131+
}
144132

145-
public void setDescription(String description) {
146-
this.description = description;
147-
}
133+
public String getSurname() {
134+
return surname;
135+
}
136+
137+
public void setSurname(String surname) {
138+
this.surname = surname;
139+
}
140+
141+
@Override
142+
public boolean equals(Object o) {
143+
if ( this == o ) {
144+
return true;
145+
}
146+
if ( o == null || getClass() != o.getClass() ) {
147+
return false;
148+
}
149+
Book.Author author = (Book.Author) o;
150+
return Objects.equals( name, author.name ) && Objects.equals( surname, author.surname );
151+
}
148152

149-
public String getType() {
150-
return type;
153+
@Override
154+
public int hashCode() {
155+
return Objects.hash( name, surname );
156+
}
157+
158+
@Override
159+
public String toString() {
160+
return name + ' ' + surname;
161+
}
151162
}
152163

153-
public void setType(String type) {
154-
this.type = type;
164+
public Book() {
155165
}
156166

157-
@Override
158-
public String toString() {
159-
return name;
167+
public Book(Integer id, String title, Author author) {
168+
this.id = id;
169+
this.title = title;
170+
this.author = author;
160171
}
161172

162173
@Override
@@ -167,15 +178,21 @@ public boolean equals(Object o) {
167178
if ( o == null || getClass() != o.getClass() ) {
168179
return false;
169180
}
170-
Flour flour = (Flour) o;
171-
return Objects.equals( name, flour.name ) &&
172-
Objects.equals( description, flour.description ) &&
173-
Objects.equals( type, flour.type );
181+
Book book = (Book) o;
182+
return Objects.equals( id, book.id ) && Objects.equals(
183+
title,
184+
book.title
185+
) && Objects.equals( author, book.author );
174186
}
175187

176188
@Override
177189
public int hashCode() {
178-
return Objects.hash( name, description, type );
190+
return Objects.hash( id, title, author );
191+
}
192+
193+
@Override
194+
public String toString() {
195+
return id + ":" + title + ":" + author;
179196
}
180197
}
181198
}

0 commit comments

Comments
 (0)