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:
<?xmlVersion= "1.0" encoding= "UTF-8"?><persistenceversion="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-unitname="JERRYJPA"transaction-type="Resource_local"> <provider>Org.eclipse.persistence.jpa.PersistenceProvider</provider> <class>Jpatest. Person</class> <properties> <propertyname="Eclipselink.ddl-generation"value="Create-tables" /> <propertyname="Javax.persistence.jdbc.url"value="Jdbc:postgresql://localhost:5432/postgres"/> <propertyname="Javax.persistence.jdbc.driver"value="Org.postgresql.Driver"/> <propertyname="Javax.persistence.jdbc.user"value="Postgres"/> <propertyname="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 the person P") Public classperson {@Id @GeneratedValue Private LongId@Basic PrivateString FirstName;@Basic PrivateString LastName; Public Long getId() {returnId } Public void setId(LongNEWID) { This.ID= NewId; } PublicStringGetfirstname() {return This.FirstName; } Public void Setfirstname(String newfirstname) { This.FirstName= Newfirstname; } PublicStringGetlastname() {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 classTest { 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