Skip to content

Commit b19b3e6

Browse files
committed
Updated Jakarta EE example
1 parent 21f71a4 commit b19b3e6

File tree

6 files changed

+76
-42
lines changed

6 files changed

+76
-42
lines changed

Diff for: jakarta-ee/README.md

+57-24
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Add the Jakarta EE API:
1212
<dependency>
1313
<groupId>jakarta.platform</groupId>
1414
<artifactId>jakarta.jakartaee-api</artifactId>
15-
<version>9.1.0</version>
15+
<version>10.0.0</version>
1616
<scope>provided</scope>
1717
</dependency>
1818
```
@@ -32,7 +32,7 @@ Implement a JPA Entity and define a named query ([JPQL](https://jakarta.ee/speci
3232
```Java
3333
@Entity
3434
@Table(name = "programming_language")
35-
@NamedQuery(name = "topProgrammingLanguages", query = "from ProgrammingLanguage pl where pl.rating > :rating")
35+
@NamedQuery(name = "popularProgrammingLanguages", query = "from ProgrammingLanguage pl where pl.rating > :rating order by pl.rating desc")
3636
public class ProgrammingLanguage {
3737

3838
@Id
@@ -62,28 +62,43 @@ Execute queries:
6262

6363
```java
6464
TypedQuery<ProgrammingLanguage> query = entityManager.createNamedQuery(
65-
"topProgrammingLanguages", ProgrammingLanguage.class);
65+
"popularProgrammingLanguages", ProgrammingLanguage.class);
6666
query.setParameter("rating", 5);
6767
List<ProgrammingLanguage> programmingLanguages = query.getResultList();
6868
```
6969

7070
## Requirements
71-
- Java 17 or later. Previous versions should work (update the version in the pom.xml file).
72-
Apache Maven.
73-
- Eclipse GlassFish 6.2.5 or later.
74-
- MariaDB server. If you don't want to install anything extra, try creating a free [SkySQL account](https://cloud.mariadb.com).
75-
- MariaDB Connector/J 3.0.4 or later.
76-
- An SQL client tool like mariadb, DBeaver, or an SQL integration for your IDE.
71+
- [Java 17](https://whichjdk.com/) or later. Previous versions should work (update the version in the **pom.xml** file)
72+
- [Apache Maven](https://maven.apache.org/download.cgi)
73+
- [Eclipse GlassFish](https://glassfish.org/download.html) 7.0.9 or later
74+
- MariaDB server ([Enterprise](https://mariadb.com/products/enterprise/) or [Community](https://mariadb.com/products/community-server/))
75+
- [MariaDB Connector/J](https://mariadb.com/downloads/connectors/connectors-data-access/java8-connector) 3.2.0 or later
76+
- An SQL client tool like [Mariadb Shell](https://mariadb.com/downloads/tools/), DBeaver, or an [SQL integration](https://www.youtube.com/watch?v=rJYUTxD-2-M) for your IDE
7777

78-
## Running the app
78+
## Preparing the database
7979

80-
Prepare the database:
80+
If you don't have a MariaDB server running, you can easily run using [Docker](https://hub.docker.com/u/mariadb):
81+
82+
```shell
83+
docker run --name mariadb -d -p 3306:3306 -e MARIADB_ROOT_PASSWORD='RootPassword123!' mariadb
84+
```
85+
86+
Alternatively, you can [Download](https://mariadb.com/downloads/) and install the server directly on your OS.
87+
88+
Connect to the database:
89+
90+
```shell
91+
mariadb-shell --dsn mariadb://root:'RootPassword123!'@127.0.0.1
92+
```
93+
94+
Alternatively, you can use any database client compatible with MariaDB.
95+
96+
Execute the following SQL statements to create a user for the JakartaEE application, a database (or schema) and a table:
8197

8298
```sql
8399
CREATE DATABASE jakartaee_demo;
84-
CREATE USER 'user'@'%';
85-
GRANT ALL ON jakartaee_demo.* TO 'user'@'%' IDENTIFIED BY 'password';
86-
FLUSH PRIVILEGES;
100+
CREATE USER 'user'@'%' IDENTIFIED BY 'Password123!';
101+
GRANT SELECT, INSERT, UPDATE, DELETE ON jakartaee_demo.* TO 'user'@'%';
87102

88103
USE jakartaee_demo;
89104
CREATE TABLE programming_language(
@@ -93,10 +108,12 @@ CREATE TABLE programming_language(
93108
);
94109
```
95110

96-
[Download MariaDB Connector/J](https://mariadb.com/downloads/connectors/connectors-data-access/java8-connector) (select Java8+ connector) and place the JAR in the **lib** directory of the fault GlassFish domain:
111+
## Building and deploying the application
112+
113+
[Download MariaDB Connector/J](https://mariadb.com/downloads/connectors/connectors-data-access/java8-connector) (select Java8+ connector) and place the JAR in the **lib** directory of the fault GlassFish domain (replace `[GLASSFISH_HOME]` with the directory in which your GlassFish installation resides):
97114

98115
```
99-
cp ~/Downloads/mariadb-java-client-3.0.4.jar [GLASSFISH_HOME]/glassfish/domains/domain1/lib
116+
cp ~/Downloads/mariadb-java-client-3.2.0.jar [GLASSFISH_HOME]/glassfish/domains/domain1/lib/
100117
```
101118

102119
Start the GlassFish application server:
@@ -105,7 +122,7 @@ Start the GlassFish application server:
105122
[GLASSFISH_HOME]/bin/asadmin start-domain
106123
```
107124

108-
Configure the database connection pool using the [Administration Console](https://glassfish.org/docs/latest/administration-guide/overview.html#GSADG00698) by going to **Resources > JDBC > JDBC Connection Pools**. Click **New** and fill in the following details:
125+
Configure the database connection pool using the [Administration Console](https://glassfish.org/docs/latest/administration-guide.html#administration-console) (http://localhost:4848) by going to **Resources > JDBC > JDBC Connection Pools**. Click **New** and fill in the following details:
109126

110127
* **Pool Name**: `MariaDB`
111128
* **Resource Type**: `java.sql.Driver`
@@ -116,29 +133,45 @@ Click **Next** and fill in the following details:
116133

117134
Click **Finish**.
118135

119-
In the JDBC Connection Pool list, click the newly created pool, select the **Additional Properties** tab, and add the following properties using the **Add Property** button:
136+
In the JDBC Connection Pool list, click on **MariaDB**, select the **Additional Properties** tab, and add the following properties using the **Add Property** button:
120137

121-
* **url**: `jdbc:mariadb://localhost:3306/jakartaee_demo`
138+
* **url**: `jdbc:mariadb://127.0.0.1:3306/jakartaee_demo`
122139
* **user**: `user`
123-
* **password**: `password`
140+
* **password**: `Password123!`
124141

125142
> If you are using [MariaDB SkySQL](https://mariadb.com/products/skysql/), enable SSL and specify the path to the CA chain file that you can download from the [SkySQL Portal](https://cloud.mariadb.com):
126143
>
127-
> `jdbc:mariadb://demo-db0000xxxx.mdb000xxxx.db.skysql.net:5047/jakartaee_demo?useSsl=true&serverSslCert=/path/to/your/skysql_chain.pem`
144+
> `jdbc:mariadb://demo-db0000xxxx.mdb000xxxx.db.skysql.net:3306/jakartaee_demo?sslMode=verify-ca&serverSslCert=/path/to/your/skysql_chain.pem`
128145
129146
Go to **Resources > JDBC > JDBC Resources**. Click **New** and fill in the following details:
130147

131148
* **JNDI Name**: `mariadb-database`
132149
* **Pool Name**: `MariaDB`
133150

151+
Click **OK**.
152+
134153
Build the WAR file:
135154

136-
```
155+
```Bash
137156
git clone [email protected]:mariadb-developers/java-quickstart.git
138157
cd java-quickstart/jakarta-ee/
139158
mvn package
140159
```
141160

142-
Deploy the WAR file to GlassFish using the Administration Console. Go to **Applications** and click te button next to the **Location** field, select the WAR file (**java-quickstart/jakarta-ee/target/jakarta-ee-1.0-SNAPSHOT.war**), and click **Ok**.
161+
To deploy the WAR file to GlassFish using the Administration Console, go to **Applications** and click the **Deploy** button. Click on the button next to **Location** and select the WAR file (**java-quickstart/jakarta-ee/target/jakarta-ee-1.0-SNAPSHOT.war**). Click **Ok**.
162+
163+
## Check the output
164+
165+
Go to **Monitoring Data > server** and click either the **View Log Files** or **View Raw Log** button. You should be able to see log messages confirming that data was deleted, created, and read. You can also connect to the database and see the data in the `programming_language` table.
166+
167+
Connect to the database:
143168

144-
You should be able to see new rows in the `programming_language` table in the database as well as log messages confirming that data was deleted, created, and read. Go to **Monitoring Data > server** and click the **View Log Files** button.
169+
```shell
170+
mariadb-shell --dsn mariadb://user:'Password123!'@127.0.0.1
171+
```
172+
173+
Run the following query:
174+
175+
```SQL
176+
SELECT * FROM jakartaee_demo.programming_languages;
177+
```

Diff for: jakarta-ee/pom.xml

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010

1111
<properties>
1212
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13-
<maven.compiler.source>17</maven.compiler.source>
14-
<maven.compiler.target>17</maven.compiler.target>
13+
<maven.compiler.source>21</maven.compiler.source>
14+
<maven.compiler.target>21</maven.compiler.target>
1515
</properties>
1616

1717
<dependencies>
1818
<dependency>
1919
<groupId>jakarta.platform</groupId>
2020
<artifactId>jakarta.jakartaee-api</artifactId>
21-
<version>9.1.0</version>
21+
<version>10.0.0</version>
2222
<scope>provided</scope>
2323
</dependency>
2424
</dependencies>
@@ -28,12 +28,12 @@
2828
<plugin>
2929
<groupId>org.apache.maven.plugins</groupId>
3030
<artifactId>maven-war-plugin</artifactId>
31-
<version>3.3.2</version>
31+
<version>3.4.0</version>
3232
</plugin>
3333
<plugin>
3434
<groupId>org.apache.maven.plugins</groupId>
3535
<artifactId>maven-compiler-plugin</artifactId>
36-
<version>3.10.1</version>
36+
<version>3.11.0</version>
3737
</plugin>
3838
</plugins>
3939
</build>

Diff for: jakarta-ee/src/main/java/com/example/Application.java

+12-8
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class Application {
2525
public void run() {
2626
deleteProgrammingLanguages();
2727
createProgrammingLanguages();
28-
printTopProgrammingLanguages();
28+
printPupularProgrammingLanguages();
2929
}
3030

3131
@Transactional
@@ -37,22 +37,26 @@ public void deleteProgrammingLanguages() {
3737
@Transactional
3838
public void createProgrammingLanguages() {
3939
log.info("Creating programming languages...");
40-
Arrays.stream("Java,C++,C#,JavaScript,Rust,Go,Python,PHP".split(","))
40+
var java = new ProgrammingLanguage("Java", 10);
41+
entityManager.persist(java);
42+
Arrays.stream("C++,C#,JavaScript,Rust,Go,Python,PHP".split(","))
4143
.map(name -> new ProgrammingLanguage(name, (int) (Math.random() * 10)))
4244
.forEach(entityManager::persist);
4345
}
4446

45-
public void printTopProgrammingLanguages() {
46-
log.info("Top programming languages:");
47-
47+
public void printPupularProgrammingLanguages() {
48+
log.info("Printing popular programming languages...");
4849
TypedQuery<ProgrammingLanguage> query = entityManager.createNamedQuery(
49-
"topProgrammingLanguages", ProgrammingLanguage.class);
50+
"popularProgrammingLanguages", ProgrammingLanguage.class);
5051
query.setParameter("rating", 5);
5152
List<ProgrammingLanguage> programmingLanguages = query.getResultList();
53+
var output = new StringBuilder("Popular programming languages:");
5254

5355
programmingLanguages.stream()
54-
.map(pl -> pl.getName() + ": " + pl.getRating())
55-
.forEach(System.out::println);
56+
.map(pl -> "\n" + pl.getName() + ": " + pl.getRating())
57+
.forEach(output::append);
58+
59+
log.info(output.toString());
5660
}
5761

5862
}

Diff for: jakarta-ee/src/main/java/com/example/ProgrammingLanguage.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
@Entity
88
@Table(name = "programming_language")
9-
@NamedQuery(name = "topProgrammingLanguages", query = "from ProgrammingLanguage pl where pl.rating > :rating")
9+
@NamedQuery(name = "popularProgrammingLanguages", query = "from ProgrammingLanguage pl where pl.rating > :rating order by pl.rating desc")
1010
public class ProgrammingLanguage {
1111

1212
@Id
-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0">
22
<persistence-unit name="jakarta-ee-demo" transaction-type="JTA">
33
<jta-data-source>mariadb-database</jta-data-source> <!-- Use the same name in the application server -->
4-
<properties>
5-
<property name="eclipselink.target-database" value="MySQL" /> <!-- EclipseLink JPA (default JPA implementation in Glassfish) requires this -->
6-
</properties>
74
</persistence-unit>
85
</persistence>

Diff for: microprofile/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<plugins>
3232
<plugin>
3333
<artifactId>maven-war-plugin</artifactId>
34-
<version>3.3.2</version>
34+
<version>3.4.0</version>
3535
<configuration>
3636
<failOnMissingWebXml>false</failOnMissingWebXml>
3737
</configuration>

0 commit comments

Comments
 (0)