maven pom.xml :
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.tch.test</groupId> <artifactId>openjpa</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>openjpa</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.apache.openjpa</groupId> <artifactId>openjpa</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.3-1102-jdbc41</version> </dependency> </dependencies> </project>
src/main/resources 创建META-INF文件夹,创建persistence.xml :
<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0"> <persistence-unit name="openjpa" transaction-type="RESOURCE_LOCAL"> <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> <class>com.tch.test.openjpa.Point</class> <exclude-unlisted-classes>true</exclude-unlisted-classes> <properties> <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" /> <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/postgres" /> <property name="javax.persistence.jdbc.user" value="postgres" /> <property name="javax.persistence.jdbc.password" value="postgres" /> <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema" /> <property name="openjpa.jdbc.DBDictionary" value="postgres(SearchStringEscape=#)" /> <property name="openjpa.Log" value="DefaultLevel=WARN, Runtime=WARN, Tool=INFO, SQL=info" /> </properties> </persistence-unit> </persistence>
然后创建实体类:
package com.tch.test.openjpa; import java.io.Serializable; import javax.persistence.*; @Entity public class Point implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue private long id; private int x; private int y; public Point() { } Point(int x, int y) { this.x = x; this.y = y; } public Long getId() { return id; } public int getX() { return x; } public int getY() { return y; } @Override public String toString() { return String.format("(%d, %d)", this.x, this.y); } }
测试:
package com.tch.test.openjpa; import javax.persistence.*; import java.util.*; public class Main { private static EntityManager em; private static EntityManagerFactory emf; static { // Open a database connection emf = Persistence.createEntityManagerFactory("openjpa"); em = emf.createEntityManager(); } public static void main(String[] args) { try{ batchSave(); getCount(); typedQuery(); clearTable(); }catch(Exception e){ e.printStackTrace(); }finally{ // Close the database connection: em.close(); emf.close(); } } private static void clearTable() { em.getTransaction().begin(); em.createQuery("delete from Point").executeUpdate(); em.getTransaction().commit(); } private static void typedQuery(){ // Retrieve all the Point objects from the database: TypedQuery<Point> query = em.createQuery("SELECT p FROM Point p", Point.class); List<Point> results = query.setMaxResults(10).getResultList(); System.out.println(results.size()); } private static void batchSave(){ long startTime = System.currentTimeMillis(); // Store 1000 Point objects in the database: em.getTransaction().begin(); for (int i = 0; i < 1000; i++) { Point p = new Point(i, i); em.persist(p); } em.getTransaction().commit(); System.out.println((System.currentTimeMillis()-startTime)+"ms"); } private static void getCount(){ // Find the number of Point objects in the database: Query q1 = em.createQuery("SELECT COUNT(p) FROM Point p"); System.out.println("Total Points: " + q1.getSingleResult()); } }
OK
相关推荐
OpenJPA is Apache's implementation of Sun's Java Persistence 2.0 API (JSR-317 JPA 2.0) specification for the transparent persistence of Java objects. This document provides an overview of the JPA ...
默认情况下,当应用程序第一次获取实体标识时,OpenJPA 框架从数据库中一次性获取 50 个连续的实体标识缓存起来,当下一次应用程序需要获取实体标识时,OpenJPA 将首先检测缓存中是否存在实体标识,如果存在,Open...
OpenJPA OpenJPA 是 Apache 组织提供的开源项目,它实现了 EJB 3.0 中的 JPA 标准,为开发者提供功能强大、使用简单的持久化数据管理框架。OpenJPA 封装了和关系型数据库交互的操作,让开发者把注意力集中在编写...
openJPA官方手册,openJPA官方手册openJPA官方手册
jpa从入门到放弃,你值得拥有!!!!!
'SpringDataJPA从入门到精通'以SpringBoot为技术基础 从入门到精通 由浅入深地介绍SpringDataJPA的使用。有语法 有实践 有原理剖析。'SpringDataJPA从入门到精通'分为12章 内容包括整体认识JPA、JPA基础查询方法、...
OpenJPA API 文档 chm格式
Open JPA2 employee 简单例子
jsf 入门知识 ,jsf标签,jsf与openJpa整合,jsf与Spring整合
自己用openjpa些的一个基于web 的例子
Spring中使用OpenJPA源代码 博文链接:https://lxy19791111.iteye.com/blog/184997
JPA Overview and OpenJPA Reference Guide
Spring MVC+OpenJPA框架,只是个框架哦
Apache OpenJPA-自述文件 前言 感谢您下载此版本的Apache OpenJPA。 Apache OpenJPA是Java Persistence API规范的实现。 执照 此存储库的内容已根据Apache License 2.0 许可 更多信息 可以在openjpa-project子目录...
OpenJPA OpenJPA 是 Apache 组织提供的开源项目,它实现了 EJB 3.0 中的 JPA 标准,为开发者提供功能强大、使用简单的持久化数据管理框架。
Openjpa2.2+Mysql+Maven+Servlet+JSP 博客源码: http://blog.csdn.net/shenhonglei1234/article/details/10394379
http://blog.csdn.net/shenhonglei1234/article/details/10352367 openjpa框架入门_项目框架搭建(二) Openjpa2.2+Mysql+Maven+Servlet+JSP
JPA快速入门初步(myeclipse)