关闭

应用程序5步应用jpa--openjpa

446人阅读 评论(0) 收藏 举报

5步展示openjpa用于应用程序(j2se)

 

环境j2se1.6 , eclipse(要求支持maven项目环境).

 

注: 类文件代码来源于Apache OpenJPA示例代码

 

1.用eclipse建立maven项目。如果搞不清maven那就是另外一回事了!

2.在增加依赖包,这里用的是oracle数据库

     <dependency>
        <groupId>org.apache.openjpa</groupId>
        <artifactId>openjpa-persistence-jdbc</artifactId>
        <version>2.0.1</version>
    </dependency>
     <dependency>   
        <groupId>ojdbc</groupId>
        <artifactId>ojdbc</artifactId>
        <version>6</version>
        <scope>runtime</scope>
    </dependency>

3.写代码

a.持久化类

 

文件message.java

 

import java.util.*;
import javax.persistence.*;

/**
 * A very simple persistent entity that holds a "message", has a
 * "created" field that is initialized to the time at which the
 * object was created, and an id field that is initialized to the
 * current time.
 */
@Entity
public class Message {
    @Id
    private long id = System.currentTimeMillis();

    @Basic
    private String message;

    @Basic
    private Date created = new Date();

    public Message() {
    }

    public Message(String msg) {
        message = msg;
    }

    public void setId(long val) {
        id = val;
    }

    public long getId() {
        return id;
    }

    public void setMessage(String msg) {
        message = msg;
    }

    public String getMessage() {
        return message;
    }

    public void setCreated(Date date) {
        created = date;
    }

    public Date getCreated() {
        return created;
    }
}

 

 

b.业务操作类

Main.java

import java.util.*;
import javax.persistence.*;

public class Main {
       @SuppressWarnings("unchecked")
        public static void main(String[] args) {
            // Create a new EntityManagerFactory using the System properties.
            // The "hellojpa" name will be used to configure based on the
            // corresponding name in the META-INF/persistence.xml file
            EntityManagerFactory factory = Persistence.
                createEntityManagerFactory("hellojpa", System.getProperties());

            // Create a new EntityManager from the EntityManagerFactory. The
            // EntityManager is the main object in the persistence API, and is
            // used to create, delete, and query objects, as well as access
            // the current transaction
            EntityManager em = factory.createEntityManager();

            // Begin a new local transaction so that we can persist a new entity
            em.getTransaction().begin();

            // Create and persist a new Message entity
            em.persist(new Message("Hello Persistence!"));

            // Commit the transaction, which will cause the entity to
            // be stored in the database
            em.getTransaction().commit();

            // It is always good practice to close the EntityManager so that
            // resources are conserved.
            em.close();

            // Create a fresh, new EntityManager
            EntityManager em2 = factory.createEntityManager();

            // Perform a simple query for all the Message entities
            Query q = em2.createQuery("select m from Message m");

            // Go through each of the entities and print out each of their
            // messages, as well as the date on which it was created
            for (Message m : (List<Message>) q.getResultList()) {
                System.out.println(m.getMessage()
                    + " (created on: " + m.getCreated() + ")");
            }

            // Again, it is always good to clean up after ourselves
            em2.close();
            factory.close();
        }
}

 

4.在META-INF下建配置文件 persistence.xml (META-INF在包的顶级目录)

 

<?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"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
  version="1.0" >
  <persistence-unit name="hellojpa">
    <description>JPA Application Client</description>
    <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
    <class>as.jpa.Message</class>
    <properties>
      <property name="openjpa.ConnectionURL" value="jdbc:oracle:thin:@localhost:1521:test" />
      <property name="openjpa.ConnectionDriverName" value="oracle.jdbc.driver.OracleDriver" />
      <property name="ConnectionUserName" value="test" />
      <property name="ConnectionPassword" value="test" />
      <property name="openjpa.jdbc.SynchronizeMappings" value="false" />
    </properties>
  </persistence-unit>
</persistence>

 

这里用oracle数据库. class参数指定为Message类(注意包与实际对应)

 

5.在数据库里建Message的表

create table message (id number,message varchar(32),created date)

 

6.到此就可以编译运行了。

 

 

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:9696次
    • 积分:180
    • 等级:
    • 排名:千里之外
    • 原创:9篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条