Creating a Java EE application with JPA and PostgreSQL in NetBeans

A Java EE application becomes much easier to maintain when its business logic, persistence layer and web interface have clear responsibilities. NetBeans provides project templates, server integration, code completion and database tools that help connect these parts without excessive configuration.

This walkthrough uses PostgreSQL as the relational database and JPA as the object-relational mapping layer. The example stores customer records, but the same structure suits an inventory system, booking platform or internal application used by a business in Sydney, Melbourne or Brisbane.

The naming has changed over time. Older Java EE projects use javax.persistence, while Jakarta EE projects use jakarta.persistence. The procedure is similar, but your imports must match the application server. The examples below target Jakarta EE 10 with a compatible server such as Payara 6; use Payara 5 or an older GlassFish release when maintaining a Java EE 8 project.

Prepare NetBeans and the application server

Install a supported JDK, NetBeans IDE and PostgreSQL. JDK 17 is a practical choice for current Jakarta EE servers. Add the server through Tools > Servers, then register Payara or GlassFish by selecting its installation directory. NetBeans can deploy the application and display server logs directly in the IDE.

Create a new Web Application project and select the registered server. Maven is a sensible default for a conventional Java EE build, although teams using a Gradle pipeline can follow this Gradle project setup before adding the Jakarta EE dependencies.

Give the project a clear name such as CustomerPortal. Select the correct Java platform and server profile. A mismatch between Jakarta EE 10 libraries and a Java EE 8 server commonly produces deployment errors, especially when the application uses jakarta.* classes on a server expecting javax.*.

Create the PostgreSQL database

Open psql, pgAdmin or a preferred database client and create a dedicated database and login rather than using the PostgreSQL administrator account:

CREATE USER portal_user WITH PASSWORD 'change_this_password';
CREATE DATABASE customer_portal OWNER portal_user;

Connect to customer_portal and allow the application user to create tables during development. In production, use controlled migrations and limited permissions. PostgreSQL’s default port is 5432, so check that the server accepts connections from the machine running Payara.

Australian teams should consider where the database is hosted. A Sydney or Melbourne cloud region can reduce latency for local users, while a Brisbane deployment may be preferable for a Queensland operation. If customer information is stored offshore, review contractual obligations and the Australian Privacy Principles before choosing a provider.

Configure the JPA persistence unit

Create src/main/resources/META-INF/persistence.xml. The persistence unit connects JPA to a server-managed datasource:

<persistence xmlns="https://jakarta.ee/xml/ns/persistence"
             version="3.0">
  <persistence-unit name="CustomerPU" transaction-type="JTA">
    <jta-data-source>jdbc/CustomerDS</jta-data-source>
    <properties>
      <property name="jakarta.persistence.schema-generation.database.action"
                value="create"/>
    </properties>
  </persistence-unit>
</persistence>

In Payara’s administration console, create a PostgreSQL JDBC connection pool using the PostgreSQL driver. Set the database URL to jdbc:postgresql://localhost:5432/customer_portal, then create a JNDI resource named jdbc/CustomerDS that points to the pool. Test the pool before deploying the application.

The create schema action is convenient while experimenting, but it can erase or recreate structures depending on the provider. For a real service, use Flyway or Liquibase migrations and set schema generation to none. This is particularly important for a small Australian business where an accidental redeployment should not remove customer orders or contact details.

Define the entity and repository logic

Create a Customer entity with an automatically generated PostgreSQL identity value:

package com.example.portal;

import jakarta.persistence.*;

@Entity
@Table(name = "customers")
public class Customer {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false, length = 120)
    private String name;

    @Column(nullable = false, unique = true, length = 180)
    private String email;

    public Long getId() { return id; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public String getEmail() { return email; }
    public void setEmail(String email) { this.email = email; }
}

Use an EJB session bean to keep database operations inside container-managed transactions:

import jakarta.ejb.Stateless;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import java.util.List;

@Stateless
public class CustomerService {
    @PersistenceContext(unitName = "CustomerPU")
    private EntityManager entityManager;

    public void save(Customer customer) {
        entityManager.persist(customer);
    }

    public List<Customer> findAll() {
        return entityManager
            .createQuery("select c from Customer c order by c.name",
                         Customer.class)
            .getResultList();
    }
}

A stateless bean is suitable for short operations that do not need conversational state. Validate email addresses and required fields before calling persist, and use parameterised JPQL rather than assembling query strings from form input.

Expose the application through a web layer

For a simple server-rendered interface, create a servlet that injects CustomerService. A REST endpoint is often a better fit for a JavaScript frontend or a mobile client:

import jakarta.inject.Inject;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import java.util.List;

@Path("/customers")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class CustomerResource {
    @Inject
    private CustomerService service;

    @GET
    public List<Customer> all() {
        return service.findAll();
    }

    @POST
    public void create(Customer customer) {
        service.save(customer);
    }
}

Register JAX-RS with an Application subclass, then deploy from NetBeans. The endpoint will usually be available under a path such as /CustomerPortal/api/customers, depending on the application name and configuration. For guidance about NetBeans workflows and related Java development topics, the NetBeans development blog provides useful background.

Test, deploy and operate safely

Right-click the project and choose Run. Check the Payara log for datasource, persistence and class-loading errors. Test the REST endpoint with curl, Postman or an integration test, then inspect PostgreSQL to confirm that rows appear in customers.

  • Keep Jakarta EE API versions aligned with the selected server.
  • Store database passwords in server configuration or environment secrets.
  • Use database migrations instead of automatic schema creation in production.
  • Add validation, unique constraints and useful transaction error handling.
  • Configure connection-pool limits and PostgreSQL backups before launch.
  • Store timestamps in UTC and convert them for Australian users at presentation time.
  • Review Australian Privacy Principles when handling names, emails or other personal data.

For a team working across Perth, Adelaide and the east coast, define logging and support times clearly because Australian time zones and daylight-saving changes can affect scheduled jobs. A friendly “no worries” user experience still depends on reliable monitoring, tested backups and predictable deployment procedures. Further Java examples and IDE configuration notes can be found at Java development resources.