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

 

你可能感兴趣的:(mysql,xml,jdbc,jpa,Access)