First, make sure that you have PostgreSQL installed. You can refer to my this article PostgreSQL literacy tutorial.
Use Eclipse to create a new JPA project:
Platform chooses Eclipselink as one of the JPA provider.
The automatically generated project in eclipse looks like this:
Overwrite the autogenerated XML with the contents of the following XML:
<?xml version= "1.0" encoding= "UTF-8"? ><persistence version= "2.0" 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 "> <persistence-unit name=" JERRYJPA "transaction-type=" resource_local "> <provider>org.eclipse.persistence.jpa.persistenceprovider& Lt;/provider> <class>jpatest. person</class> <properties> <property name= "eclipselink.ddl-generation" value= "Create-tab Les "/> <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"/> </properties> </persistence-unit></persistence>
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;@Entity@Table(name = "T_PERSON")@NamedQuery(name = "AllPersons", query = "select p from Person p")public class Person { @Id @GeneratedValue private long id; @Basic private String firstName; @Basic private String lastName; public long getId() { return id; } public void setId(long newId) { this.id = 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(); transaction.begin(); Person user = new Person(); user.setFirstName("Jerry_SAP"); user.setLastName("Wang"); entityManager.persist(user); transaction.commit(); entityManager.close(); factory.close(); System.out.println("done"); }}
After successful execution, you can see the records in the test Java program that are inserted into the database with JPA in the admin UI of PostgreSQL:
The complete source code for this article and the required library files can be found on my github.
To get more original Jerry's technical articles, please follow the public number "Wang Zixi" or scan the QR code below:
Using JPA + Eclipselink to manipulate the PostgreSQL database