Skip to content

Latest commit

 

History

History

jakarta-ee

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Jakarta EE / Eclipse GlassFish quickstart

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.

This Maven project shows the minimal configuration needed to connect to MariaDB databases using JPA in a Jakarta EE application deployed to Eclipse GlassFish.

TL;DR (kinda)

Add the Jakarta EE API:

<dependency>
	<groupId>jakarta.platform</groupId>
	<artifactId>jakarta.jakartaee-api</artifactId>
	<version>10.0.0</version>
	<scope>provided</scope>
</dependency>

Define a Persistence Unit:

<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">
	<persistence-unit name="jakarta-ee-demo" transaction-type="JTA">
		<jta-data-source>mariadb-database</jta-data-source>
	</persistence-unit>
</persistence>

Implement a JPA Entity and define a named query (JPQL):

@Entity
@Table(name = "programming_language")
@NamedQuery(name = "popularProgrammingLanguages", query = "from ProgrammingLanguage pl where pl.rating > :rating order by pl.rating desc")
public class ProgrammingLanguage {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "pl_id")
	private Integer id;

	@Column(name = "pl_name")
	private String name;

	@Column(name = "pl_rating")
	private Integer rating;

	... equals, hashCode, getters and setters ...

}

Inject an EntityManager:

@PersistenceContext(unitName = "jakarta-ee-demo")
private EntityManager entityManager;

Execute queries:

TypedQuery<ProgrammingLanguage> query = entityManager.createNamedQuery(
		"popularProgrammingLanguages", ProgrammingLanguage.class);
query.setParameter("rating", 5);
List<ProgrammingLanguage> programmingLanguages = query.getResultList();

Requirements

Preparing the database

See the instructions here.

Configuring Glassfish

Download MariaDB Connector/J (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):

cp ~/Downloads/mariadb-java-client-3.2.0.jar [GLASSFISH_HOME]/glassfish/domains/domain1/lib/

Start the GlassFish application server:

[GLASSFISH_HOME]/bin/asadmin start-domain

Configure the database connection pool using the Administration Console (http://localhost:4848) by going to Resources > JDBC > JDBC Connection Pools. Click New and fill in the following details:

  • Pool Name: MariaDB
  • Resource Type: java.sql.Driver

Click Next and fill in the following details:

  • Driver Classname: org.mariadb.jdbc.Driver

Click Finish.

In the JDBC Connection Pool list, click on MariaDB, select the Additional Properties tab, and add the following properties using the Add Property button:

  • url: jdbc:mariadb://127.0.0.1:3306/demo
  • user: user
  • password: Password123!

Go to Resources > JDBC > JDBC Resources. Click New and fill in the following details:

  • JNDI Name: mariadb-database
  • Pool Name: MariaDB

Click OK.

Building and deploying the application

Build the WAR file:

git clone [email protected]:mariadb-developers/java-quickstart.git
cd java-quickstart/jakarta-ee/
mvn package

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.

Check the output

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:

mariadb-shell --dsn mariadb://user:'Password123!'@127.0.0.1

Run the following query:

SELECT * FROM demo.programming_languages;