package com.jpa.chenhailong;
import java.util.*;
import javax.persistence.*;
/**
* A very simple, stand-alone program that stores a new entity in the
* database and then performs a query to retrieve it.
*/
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();
}
}
package com.jpa.chenhailong;
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;
}
}
<?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="hellojpa" transaction-type="RESOURCE_LOCAL">
<class>com.jpa.chenhailong.Message</class>
<properties>
<property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost/openjpa" />
<property name="openjpa.ConnectionDriverName" value="org.gjt.mm.mysql.Driver" />
<property name="openjpa.ConnectionUserName" value="****" />
<property name="openjpa.ConnectionPassword" value="****" />
</properties>
</persistence-unit>
</persistence>
create database openjpa
use openjpa
CREATE TABLE `openjpa`.`Message` (
`id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`message` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`)
)
ENGINE = InnoDB
COMMENT = 'test openjpa';
分享到:
相关推荐
赠送jar包:hibernate-jpa-2.1-api-1.0.2.Final.jar; 赠送原API文档:hibernate-jpa-2.1-api-1.0.2.Final-javadoc.jar; 赠送源代码:hibernate-jpa-2.1-api-1.0.2.Final-sources.jar; 赠送Maven依赖信息文件:...
赠送jar包:hibernate-jpa-2.1-api-1.0.2.Final.jar; 赠送原API文档:hibernate-jpa-2.1-api-1.0.2.Final-javadoc.jar; 赠送源代码:hibernate-jpa-2.1-api-1.0.2.Final-sources.jar; 赠送Maven依赖信息文件:...
JPA Overview and OpenJPA Reference Guide
apache的jpa,版本是1.1.0的,apache主页上能找的到
hibernate-jpa-2.1-api-1.0.0.final-sources.jar 源码 hibernate-jpa-2.1-api-1.0.0.final-sources.jar 源码
apache-openjpa-2.2.1-binary
OpenJPA 封装了和关系型数据库交互的操作,让开发者把注意力集中在编写业务逻辑上。OpenJPA 可以作为独立的持久层框架发挥作用,也可以轻松的与其它 Java EE 应用框架或者符合 EJB 3.0 标准的容器集成。
hibernate-jpa-2.0-api-1.0.1.Final.jar
apache-openjpa-2.2.0-source.zip
openjpa-persistence-2.1.0.jar
hibernate-jpa-2.1-api-1.0.0.final.jar.zip,内含hibernate-jpa-2.1-api-1.0.0.final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar官方下载,请放心使用
hibernate-jpa-2.0-api-1.0.1.Final-sources.jar hibernate jpa 源代码
JPA规范要求对实体对象...OpenJPA支持两种增强 方法,但强烈建议使用字节码编织增强。以下“ 实体增强” 页面包括有关两种增强类型的更多详细信息,以及有关如何在ANT,Maven和Eclipse环境中设置构建时间增强的示例。
JPA-Hibernate包, JPA-Hibernate包, JPA-Hibernate包, JPA-Hibernate包, JPA-Hibernate包, JPA-Hibernate包
jpa--2.持久化操作jpa--2.持久化操作jpa--2.持久化操作jpa--2.持久化操作jpa--2.持久化操作jpa--2.持久化操作jpa--2.持久化操作
java.lang.NoClassDefFoundError: javax/persistence/spi/ProviderUtil 或javax/persistence/entitylistener问题
jar包,官方版本,自测可用
jar包,官方版本,自测可用
jar包,官方版本,自测可用