openjpa mysql_OpenJPA-MySQL 操作

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) 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;

}

}

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0">

com.jpa.chenhailong.Message

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';

0

1

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2011-06-28 09:12

浏览 2289

评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值