|
| 1 | +# Jakarta EE / Eclipse GlassFish quickstart |
| 2 | + |
| 3 | +[Jakarta EE](https://jakarta.ee) is a set of vendor-neutral APIs for developing Java cloud-native applications that require enterprise features like distributed computing and web services. |
| 4 | + |
| 5 | +This Maven project shows the minimal configuration needed to connect to MariaDB databases using JPA in a Jakarta EE application deployed to [Eclipse GlassFish](https://glassfish.org). |
| 6 | + |
| 7 | +## LT;DR (kinda) |
| 8 | + |
| 9 | +Add the Jakarta EE API: |
| 10 | + |
| 11 | +```xml |
| 12 | +<dependency> |
| 13 | + <groupId>jakarta.platform</groupId> |
| 14 | + <artifactId>jakarta.jakartaee-api</artifactId> |
| 15 | + <version>9.1.0</version> |
| 16 | + <scope>provided</scope> |
| 17 | +</dependency> |
| 18 | +``` |
| 19 | + |
| 20 | +Define a Persistence Unit: |
| 21 | + |
| 22 | +```xml |
| 23 | +<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"> |
| 24 | + <persistence-unit name="jakarta-ee-demo" transaction-type="JTA"> |
| 25 | + <jta-data-source>mariadb-database</jta-data-source> |
| 26 | + </persistence-unit> |
| 27 | +</persistence> |
| 28 | +``` |
| 29 | + |
| 30 | +Implement a JPA Entity and define a named query ([JPQL](https://jakarta.ee/specifications/persistence/3.1/jakarta-persistence-spec-3.1.html#a4665)): |
| 31 | + |
| 32 | +```Java |
| 33 | +@Entity |
| 34 | +@Table(name = "programming_language") |
| 35 | +@NamedQuery(name = "topProgrammingLanguages", query = "from ProgrammingLanguage pl where pl.rating > :rating") |
| 36 | +public class ProgrammingLanguage { |
| 37 | + |
| 38 | + @Id |
| 39 | + @GeneratedValue(strategy = GenerationType.IDENTITY) |
| 40 | + @Column(name = "pl_id") |
| 41 | + private Integer id; |
| 42 | + |
| 43 | + @Column(name = "pl_name") |
| 44 | + private String name; |
| 45 | + |
| 46 | + @Column(name = "pl_rating") |
| 47 | + private Integer rating; |
| 48 | + |
| 49 | + ... equals, hashCode, getters and setters ... |
| 50 | + |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +Inject an `EntityManager`: |
| 55 | + |
| 56 | +```java |
| 57 | +@PersistenceContext(unitName = "jakarta-ee-demo") |
| 58 | +private EntityManager entityManager; |
| 59 | +``` |
| 60 | + |
| 61 | +Execute queries: |
| 62 | + |
| 63 | +```java |
| 64 | +TypedQuery<ProgrammingLanguage> query = entityManager.createNamedQuery( |
| 65 | + "topProgrammingLanguages", ProgrammingLanguage.class); |
| 66 | +query.setParameter("rating", 5); |
| 67 | +List<ProgrammingLanguage> programmingLanguages = query.getResultList(); |
| 68 | +``` |
| 69 | + |
| 70 | +## 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. |
| 77 | + |
| 78 | +## Running the app |
| 79 | + |
| 80 | +Prepare the database: |
| 81 | + |
| 82 | +```sql |
| 83 | +CREATE DATABASE jakartaee_demo; |
| 84 | +CREATE USER 'user'@'%'; |
| 85 | +GRANT ALL ON jakartaee_demo.* TO 'user'@'%' IDENTIFIED BY 'password'; |
| 86 | +FLUSH PRIVILEGES; |
| 87 | + |
| 88 | +USE jakartaee_demo; |
| 89 | +CREATE TABLE programming_language( |
| 90 | + pl_id INT PRIMARY KEY AUTOINCREMENT, |
| 91 | + pl_name VARCHAR(50) NOT NULL UNIQUE, |
| 92 | + pl_rating INT |
| 93 | +); |
| 94 | +``` |
| 95 | + |
| 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: |
| 97 | + |
| 98 | +``` |
| 99 | +cp ~/Downloads/mariadb-java-client-3.0.4.jar [GLASSFISH_HOME]/glassfish/domains/domain1/lib |
| 100 | +``` |
| 101 | + |
| 102 | +Start the GlassFish application server: |
| 103 | + |
| 104 | +``` |
| 105 | +[GLASSFISH_HOME]/bin/asadmin start-domain |
| 106 | +``` |
| 107 | + |
| 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: |
| 109 | + |
| 110 | + * **Pool Name**: `MariaDB` |
| 111 | + * **Resource Type**: `java.sql.Driver` |
| 112 | + |
| 113 | +Click **Next** and fill in the following details: |
| 114 | + |
| 115 | + * **Driver Classname**: `org.mariadb.jdbc.Driver` |
| 116 | + |
| 117 | +Click **Finish**. |
| 118 | + |
| 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: |
| 120 | + |
| 121 | + * **url**: `jdbc:mariadb://localhost:3306/jakartaee_demo` |
| 122 | + * **user**: `user` |
| 123 | + * **password**: `password` |
| 124 | + |
| 125 | +Go to **Resources > JDBC > JDBC Resources**. Click **New** and fill in the following details: |
| 126 | + |
| 127 | +* **JNDI Name**: `mariadb-database` |
| 128 | +* **Pool Name**: `MariaDB` |
| 129 | + |
| 130 | +Build the WAR file: |
| 131 | + |
| 132 | +``` |
| 133 | +git clone [email protected]:mariadb-developers/java-quickstart.git |
| 134 | +cd java-quickstart/jakarta-ee/ |
| 135 | +mvn package |
| 136 | +``` |
| 137 | + |
| 138 | +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**. |
| 139 | + |
| 140 | +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. |
0 commit comments