Using JPA + Eclipselink to manipulate the PostgreSQL database

Source: Internet
Author: User
Tags postgresql

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.

    • Eclipselink-2.5.1.jar

    • Javax.persistence-2.1.0.jar

    • Postgresql-42.1.1.jar


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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.