Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1424780
  • 博文数量: 931
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 10198
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-08 12:28
个人简介

测试

文章分类

全部博文(931)

文章存档

2020年(134)

2019年(792)

2018年(5)

我的朋友

分类: 架构设计与优化

2019-04-17 17:05:02

首先确保您已经安装了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="" xmlns:xsi="" xsi:schemaLocation=" /persistence_2_0.xsd"> <persistence-unit name="jerryjpa" transaction-type="RESOURCE_LOCAL"> <provider>org.eclipse.persistence.jpa.PersistenceProviderprovider> <class>jpatest.Personclass> <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的原创技术文章,请关注公众号"汪子熙"或者扫描下面二维码:

阅读(527) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~