- Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathExample.java
281 lines (238 loc) · 8.21 KB
/
Example.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
///usr/bin/env jbang "$0" "$@" ; exit $?
/* Hibernate, Relational Persistence for Idiomatic Java
*
* SPDX-License-Identifier: Apache-2.0
* Copyright: Red Hat Inc. and Hibernate Authors
*/
//DEPS com.ongres.scram:client:2.1
//DEPS io.vertx:vertx-pg-client:${vertx.version:4.5.14}
//DEPS io.vertx:vertx-mysql-client:${vertx.version:4.5.14}
//DEPS io.vertx:vertx-db2-client:${vertx.version:4.5.14}
//DEPS org.hibernate.reactive:hibernate-reactive-core:${hibernate-reactive.version:3.0.0.Beta1}
//DEPS org.slf4j:slf4j-simple:2.0.7
//DESCRIPTION Allow authentication to PostgreSQL using SCRAM:
importjava.time.LocalDate;
importjava.util.ArrayList;
importjava.util.List;
importjakarta.persistence.Basic;
importjakarta.persistence.Entity;
importjakarta.persistence.GeneratedValue;
importjakarta.persistence.Id;
importjakarta.persistence.ManyToOne;
importjakarta.persistence.OneToMany;
importjakarta.persistence.Table;
importorg.hibernate.boot.registry.StandardServiceRegistry;
importorg.hibernate.boot.registry.StandardServiceRegistryBuilder;
importorg.hibernate.cfg.Configuration;
importorg.hibernate.reactive.mutiny.Mutiny;
importorg.hibernate.reactive.provider.ReactiveServiceRegistryBuilder;
importorg.hibernate.reactive.provider.Settings;
importstaticjava.lang.System.out;
importstaticjava.time.Month.JANUARY;
importstaticjava.time.Month.JUNE;
importstaticjava.time.Month.MAY;
importstaticjakarta.persistence.CascadeType.PERSIST;
importstaticjakarta.persistence.FetchType.LAZY;
/**
* <a hreaf="https://www.jbang.dev/">JBang</a> compatible example for Hibernate Reactive.
* <p>
* The selected database must be available before running the example.
* </p>
* <p>
* Instructions on how to start the containers using Podman or Docker are available
* in the <a hreaf="https://github.com/hibernate/hibernate-reactive/blob/main/podman.md">podman.md</a>
* file on GitHub.
* </p>
* <p>
* Usage example:
* <dl>
* <dt>1. Download JBang</dt>
* <dd>See <a hreaf="https://www.jbang.dev/download">https://www.jbang.dev/download</a></dd>
* <dt>2. Start a database (default is PostgreSQL)</dt>
* <dd>
* <pre>
* podman run --rm --name HibernateTestingPGSQL \
* -e POSTGRES_USER=hreact -e POSTGRES_PASSWORD=hreact -e POSTGRES_DB=hreact \
* -p 5432:5432 postgres:17.4
* </pre>
* </dd>
* <dt>3. Run the example with JBang</dt>
* <dd>
* <pre>jbang Example.java</pre>
* </dd>
* <dt>4. (Optional) Edit the file (with IntelliJ IDEA for example):</dt>
* <dd>
* <pre>jbang edit --open=idea Example.java</pre>
* </dd>
* </dl>
* <p/>
*/
publicclassExample {
/**
* You can change this value to select a different database
*/
privatestaticfinalDatabaseDATABASE = Database.POSTGRESQL;
/**
* Create the {@link Configuration} for the {@link Mutiny.SessionFactory}.
*/
privatestaticConfigurationcreateConfiguration() {
Configurationconfiguration = newConfiguration();
// The URL to connect to
configuration.setProperty( Settings.URL, DATABASE.getUrl() );
// (Optional) Override default credentials
// configuration.setProperty( Settings.USER, "hreact" );
// configuration.setProperty( Settings.PASS, "hreact" );
// Schema generation. Supported values are create, drop, create-drop, drop-create, none
configuration.setProperty( Settings.HBM2DDL_AUTO, "create" );
// Register new entities here
configuration.addAnnotatedClass( Author.class );
configuration.addAnnotatedClass( Book.class );
// Query logging
configuration.setProperty( Settings.SHOW_SQL, "false" );
configuration.setProperty( Settings.HIGHLIGHT_SQL, "false" );
configuration.setProperty( Settings.FORMAT_SQL, "false" );
returnconfiguration;
}
publicstaticMutiny.SessionFactorycreateSessionFactory() {
Configurationconfiguration = createConfiguration();
StandardServiceRegistryBuilderbuilder = newReactiveServiceRegistryBuilder()
.applySettings( configuration.getProperties() );
StandardServiceRegistryregistry = builder.build();
returnconfiguration.buildSessionFactory( registry )
.unwrap( Mutiny.SessionFactory.class );
}
publicstaticvoidmain(String[] args) {
out.println( "== Mutiny API Example ==" );
out.println();
out.println( "Expected database: " + DATABASE );
out.println( "Connection URL: " + DATABASE.getUrl() );
out.println();
// define some test data
Authorauthor1 = newAuthor( "Iain M. Banks" );
Authorauthor2 = newAuthor( "Neal Stephenson" );
Bookbook1 = newBook( "1-85723-235-6", "Feersum Endjinn", author1, LocalDate.of( 1994, JANUARY, 1 ) );
Bookbook2 = newBook( "0-380-97346-4", "Cryptonomicon", author2, LocalDate.of( 1999, MAY, 1 ) );
Bookbook3 = newBook( "0-553-08853-X", "Snow Crash", author2, LocalDate.of( 1992, JUNE, 1 ) );
author1.getBooks().add( book1 );
author2.getBooks().add( book2 );
author2.getBooks().add( book3 );
try (Mutiny.SessionFactoryfactory = createSessionFactory()) {
// obtain a reactive session
factory.withTransaction(
// persist the Authors with their Books in a transaction
(session, tx) -> session.persistAll( author1, author2 )
)
// wait for it to finish
.await().indefinitely();
out.println();
out.println( "Looking for the book \"" + book1.getTitle() + "\"..." );
factory.withSession(
// retrieve a Book
session -> session.find( Book.class, book1.getId() )
// author is a lazy association, we need to fetch it first if we want to use it
.chain( book -> session.fetch( book.getAuthor() )
// print its title and author
.invoke( author -> out.println( "FOUND: \"" + book.getTitle() + "\"" + " by " + author.getName() + " is a great book!" ) )
)
)
// wait for it to finish
.await().indefinitely();
}
}
/**
* Example of classes representing entities.
* <p>
* If you create new entities, be sure to add them in {@link #createConfiguration()}.
* For example:
* <pre>
* configuration.addAnnotatedClass( MyOtherEntity.class );
* </pre>
*/
@Entity
@Table(name = "authors")
staticclassAuthor {
@Id
@GeneratedValue
privateIntegerid;
privateStringname;
@OneToMany(mappedBy = "author", cascade = PERSIST)
privateList<Book> books = newArrayList<>();
Author(Stringname) {
this.name = name;
}
Author() {
}
IntegergetId() {
returnid;
}
StringgetName() {
returnname;
}
List<Book> getBooks() {
returnbooks;
}
}
@Entity
@Table(name = "books")
staticclassBook {
@Id
@GeneratedValue
privateIntegerid;
privateStringisbn;
privateStringtitle;
@Basic(fetch = LAZY)
privateLocalDatepublished;
@ManyToOne(fetch = LAZY)
privateAuthorauthor;
Book(Stringisbn, Stringtitle, Authorauthor, LocalDatepublished) {
this.title = title;
this.isbn = isbn;
this.author = author;
this.published = published;
}
Book() {
}
IntegergetId() {
returnid;
}
StringgetIsbn() {
returnisbn;
}
StringgetTitle() {
returntitle;
}
AuthorgetAuthor() {
returnauthor;
}
LocalDategetPublished() {
returnpublished;
}
}
/**
* Show an example of different URL strings for each supported database.
* <p>
* One way to start the selected container is by following the instructions
* in the <a hreaf="https://github.com/hibernate/hibernate-reactive/blob/main/podman.md">podman.md</a>
* file on the Hibernate Reactive repository on GitHub.
* </p>
*/
enumDatabase {
POSTGRESQL( "postgresql://localhost:5432/hreact?user=hreact&password=hreact" ),
MYSQL( "mysql://localhost:3306/hreact?user=hreact&password=hreact&serverTimezone=UTC" ),
MARIADB( "mariadb://localhost:3306/hreact?user=hreact&password=hreact&serverTimezone=UTC" ),
DB2( "db2://localhost:50000/hreact:user=hreact;password=hreact;" ),
COCKROACHDB( "cockroachdb://localhost:26257/postgres?sslmode=disable&user=root" );
privateStringurl;
Database(Stringurl) {
this.url = url;
}
/**
* The URL string on the default port.
*
* @return a URL string value for the property {@link Settings#URL}
*/
publicStringgetUrl() {
returnurl;
}
}
}