openjpa postgresql入门

 

 

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

 

 

 

 

你可能感兴趣的:(PostgreSQL,openjpa)