Skip to content

Commit 26edf0b

Browse files
authored
Merge pull request #1520 from lucamolteni/45715-development
Hibernate ORM guide examples / quickstart are more complex than they should
2 parents 03316db + 81a4b21 commit 26edf0b

File tree

40 files changed

+370
-214
lines changed

40 files changed

+370
-214
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,15 @@ See [CONTRIBUTING](CONTRIBUTING.md) for how to build these examples.
5050
* [Hibernate Reactive Panache and RESTEasy Reactive](./hibernate-reactive-quickstart): Exposing a CRUD service over REST using Hibernate Reactive and Panache to connect to a PostgreSQL database
5151
* [Hibernate Reactive and RESTEasy](./hibernate-reactive-quickstart): Exposing a CRUD service over REST using Hibernate Reactive to connect to a PostgreSQL database
5252
* [Hibernate Reactive and Vert.x Web](./hibernate-reactive-routes-quickstart): Exposing a CRUD service with Reactive Routes using Hibernate Reactive to connect to a PostgreSQL database
53+
* [Hibernate Reactive with StatelessSession](./hibernate-reactive-stateless-quickstart): Exposing a CRUD service with Reactive Routes using Hibernate Reactive using `StatelessSession` to connect to a PostgreSQL database
5354
* [Hibernate ORM and RESTEasy](./hibernate-orm-quickstart): Exposing a CRUD service over REST using Hibernate ORM to connect to a PostgreSQL database
5455
* [Hibernate ORM with Panache and RESTEasy](./hibernate-orm-panache-quickstart): Exposing a CRUD service over REST using Panache to connect to a PostgreSQL database
5556
* [Hibernate ORM with Panache and RESTEasy in Kotlin](./hibernate-orm-panache-kotlin-quickstart): Exposing a CRUD service over REST using Panache and kotlin to connect to a PostgreSQL database
5657
* [Hibernate ORM REST Data with Panache](./hibernate-orm-rest-data-panache-quickstart): Automatically generate the CRUD endpoints for your entities and repositories using Hibernate ORM with Panache.
5758
* [Hibernate ORM Multitenancy Database](./hibernate-orm-multi-tenancy-database-quickstart): Multitenant CRUD service over REST using Hibernate ORM to connect to multiple PostgreSQL databases (database approach)
5859
* [Hibernate ORM Multitenancy Schema](./hibernate-orm-multi-tenancy-schema-quickstart): Multitenant CRUD service over REST using Hibernate ORM to connect to a PostgreSQL database (schema approach)
5960
* [Hibernate Search + Elasticsearch](./hibernate-search-orm-elasticsearch-quickstart): Index your Hibernate entities in Elasticsearch to get full text search
61+
* [Hibernate Search Standalone + Elasticsearch](./hibernate-search-orm-elasticsearch-quickstart): Index your Hibernate entities in Elasticsearch without Hibernate ORM to get full text search
6062
* [Infinispan Client](./infinispan-client-quickstart): How to use Infinispan Client. Covers creating caches and simple get/put
6163
* [Artemis JMS](./jms-quickstart): How to use the Artemis JMS extension
6264
* [Kafka](./kafka-quickstart): Use MicroProfile Reactive Messaging to interact with Apache Kafka

hibernate-orm-multi-tenancy-database-quickstart/README.md

Lines changed: 79 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,98 @@ Launch the Maven build on the checked out sources of this demo:
3131
3232
## Running the demo
3333

34-
### Start Quarkus in development mode
34+
### Live coding with Quarkus
3535

36-
The Maven Quarkus plugin provides a development mode that supports live coding. To try this out:
36+
The Maven Quarkus plugin provides a development mode that supports
37+
live coding. To try this out:
3738

3839
> ./mvnw quarkus:dev
3940
4041
In this mode you can make changes to the code and have the changes immediately applied, by just refreshing your browser.
4142

42-
### Run Quarkus as native executable
43+
Dev Mode automatically starts two Docker containers running Postgres databases. This feature is called ["Dev Services."](https://quarkus.io/guides/dev-services)
4344

44-
You can also create a native executable from this application without making any source code changes. A native executable removes the dependency on the JVM:
45-
everything needed to run the application on the target platform is included in the executable, allowing the application to run with minimal resource overhead.
45+
To access the database from the terminal, run:
4646

47-
Compiling a native executable takes a bit longer, as GraalVM performs additional steps to remove unnecessary codepaths. Use the `native` profile to compile a native executable:
47+
```sh
48+
docker container list
49+
```
4850

49-
> ./mvnw package -Pnative
51+
To get the id or the name of the created containers and then to access the database machine use
5052

51-
After getting a cup of coffee, you'll be able to run this executable directly:
53+
```sh
54+
docker exec -it <container-name> psql -U quarkus -d base
55+
docker exec -it <container-name> psql -U quarkus -d mycompany
56+
```
57+
58+
As the ids and names are auto generated, you should try the command with both ids or names to see what works.
59+
If the wrong one is provided, you'll get a `FATAL: role "base" does not exist`. Try the other container instance instead.
60+
61+
Hot reload works even when modifying your JPA entities.
62+
Try it! Even the database schema will be updated on the fly.
63+
64+
### Run Quarkus in JVM mode
65+
66+
When you're done iterating in developer mode, you can run the application as a
67+
conventional jar file.
68+
69+
First compile it:
70+
71+
> ./mvnw package
72+
73+
Next, make sure you have the two PostgreSQL databases running. In production, Quarkus does not start any container for you like it does in Dev Mode.
74+
To set up two PostgreSQL databases with Docker:
75+
76+
```sh
77+
78+
docker run -it --rm=true --name base-container -e POSTGRES_USER=base -e POSTGRES_PASSWORD=base -e POSTGRES_DB=base -p 5432:5432 postgres:13.3
79+
docker run -it --rm=true --name mycompany-container -e POSTGRES_USER=mycompany -e POSTGRES_PASSWORD=mycompany -e POSTGRES_DB=mycompany -p 5433:5432 postgres:13.3
80+
```
81+
82+
Connection properties for the Agroal datasource are defined in the standard Quarkus configuration file,
83+
`src/main/resources/application.properties`.
84+
85+
To access these databases, since the usernames and passwords are set inside the `%prod` profile, and the usernames are the same as the Postgres database names, we can use to access those:
86+
87+
```sh
88+
docker exec -it base-container psql -U base
89+
docker exec -it mycompany-container psql -U mycompany
90+
```
91+
92+
93+
Then run it:
94+
95+
> java -jar ./target/quarkus-app/quarkus-run.jar
96+
97+
Have a look at how fast it boots.
98+
Or measure total native memory consumption...
99+
100+
### Run Quarkus as a native application
101+
102+
You can also create a native executable from this application without making any
103+
source code changes. A native executable removes the dependency on the JVM:
104+
everything needed to run the application on the target platform is included in
105+
the executable, allowing the application to run with minimal resource overhead.
106+
107+
Compiling a native executable takes a bit longer, as GraalVM performs additional
108+
steps to remove unnecessary codepaths. Use the `native` profile to compile a
109+
native executable:
110+
111+
> ./mvnw package -Dnative
112+
113+
After getting a cup of coffee, you'll be able to run this binary directly:
52114

53115
> ./target/hibernate-orm-schema-multi-tenancy-quickstart-1.0.0-SNAPSHOT-runner
54116
117+
Please brace yourself: don't choke on that fresh cup of coffee you just got.
118+
119+
Now observe the time it took to boot, and remember: that time was mostly spent to generate the tables in your database and import the initial data.
120+
121+
Next, maybe you're ready to measure how much memory this service is consuming.
122+
123+
N.B. This implies all dependencies have been compiled to native;
124+
that's a whole lot of stuff: from the bytecode enhancements that Hibernate ORM
125+
applies to your entities, to the lower level essential components such as the PostgreSQL JDBC driver, the Undertow webserver.
55126

56127
## See the demo in your browser
57128

hibernate-orm-multi-tenancy-database-quickstart/src/main/java/org/acme/hibernate/orm/Fruit.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,14 @@
55
import jakarta.persistence.GeneratedValue;
66
import jakarta.persistence.Id;
77
import jakarta.persistence.NamedQuery;
8-
import jakarta.persistence.SequenceGenerator;
9-
import jakarta.persistence.Table;
108

119
@Entity
12-
@Table(name = "known_fruits")
1310
@NamedQuery(name = "Fruits.findAll", query = "SELECT f FROM Fruit f ORDER BY f.name")
1411
@NamedQuery(name = "Fruits.findByName", query = "SELECT f FROM Fruit f WHERE f.name=:name")
1512
public class Fruit {
1613

1714
@Id
18-
@SequenceGenerator(name = "fruitsSequence", sequenceName = "known_fruits_id_seq", allocationSize = 1, initialValue = 10)
19-
@GeneratedValue(generator = "fruitsSequence")
15+
@GeneratedValue
2016
private Integer id;
2117

2218
@Column(length = 40, unique = true)

hibernate-orm-multi-tenancy-database-quickstart/src/main/resources/META-INF/resources/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ <h1>Multitenancy Hibernate ORM - Quarkus</h1>
141141
<li>ArC, a CDI based dependency injection framework</li>
142142
<li>the Narayana Transaction Manager coordinating all transactions</li>
143143
<li>Agroal, the high performance Datasource implementation</li>
144-
<li>Infinispan used as Hibernate 2nd level caching: enabled on both entities and queries</li>
145144
<li>The Undertow webserver</li>
146145
<li>Some magic bytecode generation plugged in the compiler...</li>
147146
</ul>

hibernate-orm-multi-tenancy-database-quickstart/src/main/resources/application.properties

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ quarkus.hibernate-orm.datasource=base
55

66
# Default tenant 'base'
77
quarkus.datasource.base.db-kind=postgresql
8-
quarkus.datasource.base.username=quarkus_test
9-
quarkus.datasource.base.password=quarkus_test
10-
%prod.quarkus.datasource.base.jdbc.url=jdbc:postgresql://localhost:5432/quarkus_test
118
quarkus.flyway.base.locations=classpath:database/base
129
quarkus.flyway.base.migrate-at-start=true
10+
%prod.quarkus.datasource.base.username=base
11+
%prod.quarkus.datasource.base.password=base
12+
%prod.quarkus.datasource.base.jdbc.url=jdbc:postgresql://localhost:5432/base
1313

1414
# Tenant 'mycompany'
1515
quarkus.datasource.mycompany.db-kind=postgresql
16-
quarkus.datasource.mycompany.username=mycompany
17-
quarkus.datasource.mycompany.password=mycompany
18-
%prod.quarkus.datasource.mycompany.jdbc.url=jdbc:postgresql://localhost:5433/mycompany
1916
quarkus.flyway.mycompany.locations=classpath:database/mycompany
20-
quarkus.flyway.mycompany.migrate-at-start=true
17+
quarkus.flyway.mycompany.migrate-at-start=true
18+
%prod.quarkus.datasource.mycompany.username=mycompany
19+
%prod.quarkus.datasource.mycompany.password=mycompany
20+
%prod.quarkus.datasource.mycompany.jdbc.url=jdbc:postgresql://localhost:5433/mycompany
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
CREATE SEQUENCE known_fruits_id_seq;
2-
SELECT setval('known_fruits_id_seq', 3);
3-
CREATE TABLE known_fruits
1+
CREATE SEQUENCE fruit_seq INCREMENT BY 50; -- 50 is quarkus default
2+
CREATE TABLE fruit
43
(
5-
id INT,
6-
name VARCHAR(40)
4+
id INT,
5+
name VARCHAR(40)
76
);
8-
INSERT INTO known_fruits(id, name) VALUES (1, 'Cherry');
9-
INSERT INTO known_fruits(id, name) VALUES (2, 'Apple');
10-
INSERT INTO known_fruits(id, name) VALUES (3, 'Banana');
7+
INSERT INTO fruit(id, name) VALUES (1, 'Cherry');
8+
INSERT INTO fruit(id, name) VALUES (2, 'Apple');
9+
INSERT INTO fruit(id, name) VALUES (3, 'Banana');
10+
ALTER SEQUENCE fruit_seq RESTART WITH 4;
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
CREATE SEQUENCE known_fruits_id_seq;
2-
SELECT setval('known_fruits_id_seq', 3);
3-
CREATE TABLE known_fruits
1+
CREATE SEQUENCE fruit_seq INCREMENT BY 50; -- 50 is quarkus default
2+
CREATE TABLE fruit
43
(
54
id INT,
65
name VARCHAR(40)
76
);
8-
INSERT INTO known_fruits(id, name) VALUES (1, 'Avocado');
9-
INSERT INTO known_fruits(id, name) VALUES (2, 'Apricots');
10-
INSERT INTO known_fruits(id, name) VALUES (3, 'Blackberries');
7+
INSERT INTO fruit(id, name) VALUES (1, 'Avocado');
8+
INSERT INTO fruit(id, name) VALUES (2, 'Apricots');
9+
INSERT INTO fruit(id, name) VALUES (3, 'Blackberries');
10+
ALTER SEQUENCE fruit_seq RESTART WITH 4;

hibernate-orm-multi-tenancy-schema-quickstart/README.md

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,80 @@ for help setting up your environment.
2828
Launch the Maven build on the checked out sources of this demo:
2929

3030
> ./mvnw package
31+
3132
## Running the demo
3233

33-
### Start Quarkus in development mode (Profile 'development')
34+
### Live coding with Quarkus
3435

35-
The Maven Quarkus plugin provides a development mode that supports live coding. To try this out:
36+
The Maven Quarkus plugin provides a development mode that supports
37+
live coding. To try this out:
3638

3739
> ./mvnw quarkus:dev
40+
3841
In this mode you can make changes to the code and have the changes immediately applied, by just refreshing your browser.
3942

40-
### Run Quarkus as native executable
43+
Dev Mode automatically starts a Docker container with a Postgres database. This feature is called ["Dev Services."](https://quarkus.io/guides/dev-services)
44+
45+
To access the database from the terminal, run:
46+
47+
```sh
48+
docker exec -it <container-name> psql -U quarkus
49+
```
50+
51+
Hot reload works even when modifying your JPA entities.
52+
Try it! Even the database schema will be updated on the fly.
53+
54+
### Run Quarkus in JVM mode
55+
56+
When you're done iterating in developer mode, you can run the application as a
57+
conventional jar file.
58+
59+
First compile it:
60+
61+
> ./mvnw package
4162
42-
You can also create a native executable from this application without making any source code changes. A native executable removes the dependency on the JVM:
43-
everything needed to run the application on the target platform is included in the executable, allowing the application to run with minimal resource overhead.
63+
Next, make sure you have a PostgreSQL database running. In production, Quarkus does not start a container for you like it does in Dev Mode.
64+
To set up a PostgreSQL database with Docker:
4465

45-
Compiling a native executable takes a bit longer, as GraalVM performs additional steps to remove unnecessary codepaths. Use the `native` profile to compile a
66+
> docker run -it --rm=true --name quarkus_test -e POSTGRES_USER=quarkus_test -e POSTGRES_PASSWORD=quarkus_test -e POSTGRES_DB=quarkus_test -p 5432:5432 postgres:13.3
67+
68+
Connection properties for the Agroal datasource are defined in the standard Quarkus configuration file,
69+
`src/main/resources/application.properties`.
70+
71+
Then run it:
72+
73+
> java -jar ./target/quarkus-app/quarkus-run.jar
74+
75+
Have a look at how fast it boots.
76+
Or measure total native memory consumption...
77+
78+
### Run Quarkus as a native application
79+
80+
You can also create a native executable from this application without making any
81+
source code changes. A native executable removes the dependency on the JVM:
82+
everything needed to run the application on the target platform is included in
83+
the executable, allowing the application to run with minimal resource overhead.
84+
85+
Compiling a native executable takes a bit longer, as GraalVM performs additional
86+
steps to remove unnecessary codepaths. Use the `native` profile to compile a
4687
native executable:
4788

48-
> ./mvnw package -Pnative
89+
> ./mvnw package -Dnative
4990
50-
After getting a cup of coffee, you'll be able to run this executable directly:
91+
After getting a cup of coffee, you'll be able to run this binary directly:
5192

5293
> ./target/hibernate-orm-schema-multi-tenancy-quickstart-1.0.0-SNAPSHOT-runner
5394
95+
Please brace yourself: don't choke on that fresh cup of coffee you just got.
96+
97+
Now observe the time it took to boot, and remember: that time was mostly spent to generate the tables in your database and import the initial data.
98+
99+
Next, maybe you're ready to measure how much memory this service is consuming.
100+
101+
N.B. This implies all dependencies have been compiled to native;
102+
that's a whole lot of stuff: from the bytecode enhancements that Hibernate ORM
103+
applies to your entities, to the lower level essential components such as the PostgreSQL JDBC driver, the Undertow webserver.
104+
54105
## See the demo in your browser
55106

56107
Navigate to:

hibernate-orm-multi-tenancy-schema-quickstart/src/main/java/org/acme/hibernate/orm/Fruit.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,14 @@
55
import jakarta.persistence.GeneratedValue;
66
import jakarta.persistence.Id;
77
import jakarta.persistence.NamedQuery;
8-
import jakarta.persistence.SequenceGenerator;
9-
import jakarta.persistence.Table;
108

119
@Entity
12-
@Table(name = "known_fruits")
1310
@NamedQuery(name = "Fruits.findAll", query = "SELECT f FROM Fruit f ORDER BY f.name")
1411
@NamedQuery(name = "Fruits.findByName", query = "SELECT f FROM Fruit f WHERE f.name=:name")
1512
public class Fruit {
1613

1714
@Id
18-
@SequenceGenerator(name = "fruitsSequence", sequenceName = "known_fruits_id_seq", allocationSize = 1, initialValue = 10)
19-
@GeneratedValue(generator = "fruitsSequence")
15+
@GeneratedValue
2016
private Integer id;
2117

2218
@Column(length = 40, unique = true)

hibernate-orm-multi-tenancy-schema-quickstart/src/main/resources/META-INF/resources/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ <h1>Multitenancy Hibernate ORM - Quarkus</h1>
141141
<li>ArC, a CDI based dependency injection framework</li>
142142
<li>the Narayana Transaction Manager coordinating all transactions</li>
143143
<li>Agroal, the high performance Datasource implementation</li>
144-
<li>Infinispan used as Hibernate 2nd level caching: enabled on both entities and queries</li>
145144
<li>The Undertow webserver</li>
146145
<li>Some magic bytecode generation plugged in the compiler...</li>
147146
</ul>

0 commit comments

Comments
 (0)