zoukankan      html  css  js  c++  java
  • JPA中persistence.xml模板

    持久化单元,持久化对象的集合。

    事务类型:本地事务和全局事务。某些应用场合,只能使用全局事务。

    有两个数据库,mysql和oracle.转账时扣钱从mysql进行,加钱是在oracle执行,怎样确保两个语句在同一个事务中执行。普通JDBC不能,只能用全局事务。事务的生命周期应该从Connection对象中抽取出来,不应该局限在Connection中。通常由容器(WebLogic,JBoss)提供JTA实现。二次提交协议,执行一条语句预提交到数据库,执行结果保存到某个变量中,再执行另一条语句,如果都成功,再进行第二次提交。

    类路径下META-INF/persistence.xml如下:

    <?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/persistencehttp://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
      <persistence-unit name="jpaPU" transaction-type="RESOURCE_LOCAL">
         <provider>org.hibernate.ejb.HibernatePersistence</provider>
          <properties>
             <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
             <property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver"/>
             <property name="hibernate.connection.username" value="root"/>
             <property name="hibernate.connection.password" value="123456"/>
             <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/hib?useUnicode=true&amp;characterEncoding=UTF-8"/>
             <property name="hibernate.max_fetch_depth" value="3"/>
             <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
          </properties>
      </persistence-unit>
    </persistence>

    这里使用的是Hibernate的JPA实现。不同的JPA产品properties属性配置是不同的。

    create-drop的意思是sessionFactory加载时创建表,销毁时删除。

    自定义封装类产生EntityManager

    package com.tazi.domin;

    import javax.persistence.EntityManager;
    import javax.persistence.EntityManagerFactory;
    import javax.persistence.Persistence;

    public final class JpaUtils {
    private static EntityManagerFactory emf;
    static{
    emf=Persistence.createEntityManagerFactory("jpaPU");
    //靠provider实现,这里的provider是org.hibernate.ejb.HibernatePersistence
    System.out.println(emf.getClass().getName());//org.hibernate.ejb.EntityManagerFactoryImpl
    }
    public static EntityManager getEntityManager(){
    EntityManager em=emf.createEntityManager();
    System.out.println(em.getClass().getName());//org.hibernate.ejb.EntityManagerImpl
    return em;
    }
    }

    例子:实体类Person

    package com.tazi.domin;

    import java.util.Date;

    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;

    @Entity
    public class Person {
        @Id
        @GeneratedValue
        private Integer id;
        private String name;
        private Date birthday;
        public Integer getId() {
            return id;
        }
        public void setId(Integer id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Date getBirthday() {
            return birthday;
        }
        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }
       
    }

    测试:

    package com.tazi.domin;

    import javax.persistence.EntityManager;
    import javax.persistence.EntityTransaction;


    public class Test1 {
        public static void main(String[] args) {
            Person person=new Person();
            person.setName("tazi");
            EntityManager em=null;
            EntityTransaction tx=null;
            try{
                em=JpaUtils.getEntityManager();
                tx=em.getTransaction();
                em.persist(person);
                tx.begin();
                tx.commit();
            }finally{
                if(em!=null){
                    em.close();
                }
            }
        }
    }

    这里的方法名都是JPA API,但其实实现是由Hibernate实现的。也就是底层其实是sessionFactory和session的操作。

    JPA里面


  • 相关阅读:
    发布两个android程序遇到证书不一致的问题!
    第一次下载程序到单片机
    人人网是明文传输,所以只要抓包就能知道用户名和密码
    [转]java调用cmd命令
    qq发送文件是在两个计算机之间建立tcp连接
    CardLayout实现分页效果
    控件 ListView1 的类 MSComctlLib.ListView 不是一个已加载的控件类
    VB中declare function 出现49错误
    oracle instant client
    WARN Please initialize the log4j system properly的解决办法【转载】
  • 原文地址:https://www.cnblogs.com/tazi/p/2311576.html
Copyright © 2011-2022 走看看