Using JPA + eclipse link to operate PostgreSQL database

Posted by rubenc on Thu, 19 Mar 2020 16:55:47 +0100

First, make sure you have PostgreSQL installed. You can refer to my article PostgreSQL literacy tutorial.

Create a new JPA project using Eclipse:

The Platform selects eclipse link as one of the provider s of JPA.

The automatically generated project in Eclipse is shown below:

Overwrite the automatically generated xml with the contents of the following xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="" xmlns:xsi=""
    <persistence-unit name="jerryjpa" transaction-type="RESOURCE_LOCAL">
            <property name="eclipselink.ddl-generation" value="create-tables" />
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/postgres"/>         
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>  
            <property name="javax.persistence.jdbc.user" value="postgres"/>  
            <property name="javax.persistence.jdbc.password" value="test_password"/> 

Create a new Java class:

package jpatest;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

@Table(name = "T_PERSON")
@NamedQuery(name = "AllPersons", query = "select p from Person p")
public class Person {
    private long id;
    private String firstName;
    private String lastName;

    public long getId() {
        return id;

    public void setId(long newId) { = newId;

    public String getFirstName() {
        return this.firstName;

    public void setFirstName(String newFirstName) {
        this.firstName = newFirstName;

    public String getLastName() {
        return this.lastName;

    public void setLastName(String newLastName) {
        this.lastName = newLastName;

Now you can write the test program:

package jpatest;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

public class Test {
public static void main(String[] args) {
        String persistenceUnitName = "jerryjpa";  
        EntityManagerFactory factory = Persistence.createEntityManagerFactory(persistenceUnitName);  
        EntityManager entityManager = factory.createEntityManager();  
        EntityTransaction transaction = entityManager.getTransaction();  
        Person user = new Person();  

After successful execution, you can see the record of inserting JPA into the database in the test Java program on the Admin UI of PostgreSQL:

The complete source code and required library files of this article can be found in my github Find it.

  • eclipselink-2.5.1.jar

  • javax.persistence-2.1.0.jar

  • postgresql-42.1.1.jar

For more original technical articles of Jerry, please pay attention to the official account "Wang Zixi" or scan the following two-dimensional code:

Topics: PostgreSQL xml Java JDBC