Advertise with us

Our eBooks

HelloWorld JPA application

In this tutorial we will learn how to create a simple Hello World JPA application to be deployed on WildFly or any Java EE container.

Create the Maven project

We will need a Maven archetype to bootstrap a Web application project. You can use one simple like the following one:

mvn -DarchetypeGroupId=org.codehaus.mojo.archetypes \
-DarchetypeArtifactId=webapp-javaee7 \
-DgroupId=com.mastertheboss -DartifactId=jpa-basic \
-Dversion=1.1 -Dpackage=com.mastertheboss \
-Darchetype.interactive=false --batch-mode --update-snapshots \

Now Let's write our simple Entity class that we will persist in the Database:

package com.mastertheboss.model;

import javax.enterprise.event.Event;
import javax.inject.Inject;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.PostRemove;

public class SimpleProperty {

	private String key;

	private String value;

	public String getKey() {
		return key;

	public void setKey(String key) {
		this.key = key;

	public String toString() {
		return "SimpleProperty [key=" + key + ", value=" + value + "]";

	public String getValue() {
		return value;

	public void setValue(String value) {
		this.value = value;


Now, in order to store our Entity in a transactional way, we need an EJB:

package com.mastertheboss.ejb;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.mastertheboss.model.SimpleProperty;


public class  ServiceBean   {

	private EntityManager em;

	public void put(SimpleProperty p){


	public void delete(SimpleProperty p){

		Query query = em.createQuery("delete FROM SimpleProperty p where p.key='"+p.getKey()+"'");



	public List<SimpleProperty> findAll(){

		Query query = em.createQuery("FROM SimpleProperty");

		List <SimpleProperty> list = query.getResultList();
		return list;


	public SimpleProperty findById(String id){

		SimpleProperty p = em.find(SimpleProperty.class, id);
		return p;



Finally, we need a front-end for our application. To keep it pretty simple, a basic REST Endpoint will be added:


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;

import com.mastertheboss.model.SimpleProperty;
import com.mastertheboss.ejb.ServiceBean;

 * JAX-RS Example
 * <p/>
 * This class produces a RESTful service to read/write the contents of the members table.
public class RestService {

	private ServiceBean ejb;

	public List<SimpleProperty> listAllMembers() {
		return ejb.findAll();

	public SimpleProperty findById(@PathParam("id") String id) {

		SimpleProperty p = ejb.findById(id);
		if (p == null) {
			throw new WebApplicationException(Response.Status.NOT_FOUND);
		return p;

	public Response createMember(SimpleProperty member) {

		Response.ResponseBuilder builder = null;

		try {

			// Create an "ok" response
			builder = Response.ok("Saved!");

		} catch (Exception e) {
			// Handle generic exceptions
			Map<String, String> responseObj = new HashMap<>();
			responseObj.put("error", e.getMessage());
			builder = Response.status(Response.Status.BAD_REQUEST).entity(responseObj);



As you can see, the above REST endpoint has some basic methods for creating and querying our Entity objects, returning a JSON view of it.

In order to get it working, we need a REST Activator class:



public class RestActivator extends Application {

Done with the code, we will add a persistence.xml file to declare the mapping for our PersistenceContext objects:

<?xml version="1.0" encoding="UTF-8"?>

<persistence version="2.1"
             xmlns="" xmlns:xsi=""
    <persistence-unit name="primary">
            <!-- Properties for Hibernate -->
            <property name="" value="create-drop" />
            <property name="hibernate.show_sql" value="false" />

As you can see, we rely on the default Datasource which ships with WildFly. Here is the final project tree structure:

├── pom.xml
└── src
    └── main
        ├── java
        │   └── com
        │       └── mastertheboss
        │           ├── ejb
        │           │   └──
        │           ├── model
        │           │   └──
        │           └── rest
        │               ├──
        │               └──
        ├── resources
        │   └── META-INF
        │       └── persistence.xml
        └── webapp
            ├── index.html

Now compile the project:

$ mvn clean install

The deploy it on WildFly. We can start by adding some data using a POST with cURL:

$ curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"key":"xyz","value":"xyz"}' \

We can check that data was inserted with:

$ curl http://localhost:8080/jpa-basic/rest/action


Much the same, we can query by id. It will return the same id:

$ curl http://localhost:8080/jpa-basic/rest/action/id/xyz


That's all! Enjoy your first JPA application example on WildFly!

You can find the source code for this JPA application on:

© 2021 All Rights Reserved.

Please publish modules in offcanvas position.