SessionFactory在Hibernate应用程序中表示一个特定的逻辑数据存储配置。EntityManagerFactory在JPA应用程序中扮演着同样的角色,你用配置文件或者在应用程序代码中配置EntityManagerFactory(EMF),就像配置SessionFactory一样。EMF的配置,与一组映射元数据(通常是被注解的类)一起,被称作持久化单元(persistence unit)。
<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="helloworld"> <properties> <property name="hibernate.ejb.cfgfile" value="/hibernate.cfg.xml" /> </properties> </persistence-unit> </persistence>和一个持久化单元都需要一个名称,在这个例子中为helloworld。
<property name="src.etc.dir" value="etc" /> <!--复制xml、properties后缀文件到bin目录--> <target name="copymetafiles"> <copy todir="${build.dir}"> <fileset dir="${src.java.dir}"> <patternset refid="meta.files" /> </fileset> </copy> <copy todir="${build.dir}"> <fileset dir="${src.etc.dir}"> <patternset refid="meta.files" /> </fileset> </copy> </target>firstJta/etc目录下与meta.files模式相匹配的所有内容都被复制到构建输出目录,这是运行时classpath的一部分。
package cn.jbit.jta.util; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; public final class JPAUtil { private static final EntityManagerFactory emf; static { try{ emf = Persistence.createEntityManagerFactory("helloworld"); }catch(Throwable ex){ throw new ExceptionInInitializerError(ex); } } private JPAUtil(){ } public static EntityManagerFactory getEmf() { return emf; } public static EntityManager getEntityManager(){ return emf.createEntityManager(); } public static void shutdown(){ emf.close(); } }
package cn.jbit.jta.test; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.EntityTransaction; import cn.jbit.jta.entity.Message; import cn.jbit.jta.util.JPAUtil; public class EntityManagerTest { @SuppressWarnings("unchecked") public static void main(String[] args) { // 第一个单元 // createEntityMangaerFacoty参数"helloworld"是persistence.xml文件中持久化单元名称 EntityManager em = JPAUtil.getEntityManager(); EntityTransaction tx = em.getTransaction(); tx.begin(); Message message = new Message("Hello World"); em.persist(message); tx.commit(); em.close(); // 第二个单元 EntityManager secondEm = JPAUtil.getEntityManager(); EntityTransaction secondTx = secondEm.getTransaction(); secondTx.begin(); List<Message> messages = secondEm.createQuery( "select m from Message m order by m.text asc").getResultList(); System.out.println(messages.size() + "条消息记录"); for (Message m : messages) { System.out.println(m.getText()); } secondTx.commit(); secondEm.close(); JPAUtil.shutdown(); } }在这段代码中你可能注意到的第一点是不再有Hibernate导入,只有javax.persistence.*。用一个对persistence的静态调用和持久化单元的名称来创建EntityManagerFactory。其余代码应该是不言自明的——就像使用Hibernate一样使用JPA,虽然在API中有些小差别,并且方法的名称也略有不同。
<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="helloworld"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <!-- <class>cn.jbit.jta.entity.Message</class> --> <properties> <!-- <property name="hibernate.ejb.cfgfile" value="/hibernate.cfg.xml" /> --> <property name="hibernate.archive.autodetection" value="class,hbm"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.format_sql" value="true"/> <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/> <property name="hibernate.connection.url" value="jdbc:oracle:thin:@localhost:1521:orcl"/> <property name="hibernate.connection.username" value="system"/> <property name="hibernate.connection.password" value="bdqn"/> <property name="hibernate.c3p0.min_size" value="5"/> <property name="hibernate.c3p0.max_size" value="20"/> <property name="hibernate.c3p0.timeout" value="300"/> <property name="hibernate.c3p0.max_statements" value="50"/> <property name="hibernate.c3p0.idle_test_period" value="3000"/> <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/> <property name="hibernate.hbm2ddl.auto" value="create"/> </properties> </persistence-unit> </persistence>在这个配置文件中有3个值得关注的新元素,首先,你设置了应该为这个持久化单元所用的一个显式的<provider>。通常只在你在非Java EE的环境中部署,规范要求列出包含<class>元素的所有被注解的类——Hibernate处处支持映射元数据的自动侦测,使它变成可选。最后Hibernate配置设置hibernate.archive.autodetection告知Hibernate要自动扫描哪些元数据:被注解的类(class)和Hibernate XML映射文件(hbm)。默认情况下,Hibernate EntityManager对两者都进行扫描。
Hibernate实战_笔记10,布布扣,bubuko.com
原文:http://blog.csdn.net/com185272358/article/details/20774527