使用JPA + Eclipselink操作PostgreSQL資料庫

來源:互聯網
上載者:User

標籤:SAP   SCP   SAP雲平台   JPA   EclipseLink   

首先確保您已經安裝了PostgreSQL。您可以參考我這篇文章PostgreSQL掃盲教程。

使用Eclipse建立一個新的JPA project:

Platform選擇EclipseLink,作為JPA的provider之一。

在Eclipse裡自動產生的project如所示:

用下列xml的內容覆蓋自動產生的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</provider>        <class>jpatest.Person</class>        <properties>            <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"/>         </properties>    </persistence-unit></persistence>

建立一個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;    }}

現在可以寫測試程式了:

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");    }}

成功執行後,在PostgreSQL的Admin UI上能看到測試Java程式裡用JPA插入資料庫的記錄:

本文完整的原始碼和所需的庫檔案可以在我的github上找到。

  • eclipselink-2.5.1.jar

  • javax.persistence-2.1.0.jar

  • postgresql-42.1.1.jar


要擷取更多Jerry的原創技術文章,請關注公眾號"汪子熙"或者掃描下面二維碼:

使用JPA + Eclipselink操作PostgreSQL資料庫

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.