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

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.到此就可以编译运行了。

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值