javax.persistenc.Persistence类提供了两个手动创建EntityManagerFactory对象的方法,它们的定义如下所示。
package javax.persistence;
import java.util.*;
public class Persistence {
/**
* 指定持久化单元,创建EntityManagerFactory对象
* @param persistenceUnitName 持久化单元的名称
*/
public static EntityManagerFactory createEntityManagerFactory(
String persistenceUnitName) {...}
/**
*指定持久化单元和实现者的属性配置,创建EntityManagerFactory对象
*
* @param persistenceUnitName持久化单元的名称
* @param 实现者的属性配置,这些配置将覆盖persistence.xml文件中的配置
*/
public static EntityManagerFactory createEntityManagerFactory(
String persistenceUnitName, Map properties) {...}
}
其中,createEntityManagerFactory(String persistenceUnitName, Map properties)方法中properties的参数将覆盖persistence.xml文件配置的参数。
例如persistence.xml中的<property>元素中的配置如下所示。
<?xml version="1.0" encoding="UTF-8"?>
<persistence>
<persistence-unit name="jpaUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>子
<property name = "hibernate.connection.driver_class" value = "com.mysql. jdbc.Dr iver"/>
<property name = "hibernate.connection.url" value = "jdbc:mysql: //localhost:3306/jp ademo"/>
<property name = "hibernate.connection.username" value = "root"/>
</properties>
<property name="hibernate.show_sql" value="true" />
</persistence-unit>
</persistence>
其中,属性 hibernate.show_sql的值为true,若创建EntityManagerFactory对象时采用以下的代码,则实际获得的 EntityManagerFactory对象属性hibernate.show_sql则为false,即为代码中所设置的将覆盖配置文件中所配置的,如下所示。
Map properties = new HashMap();
properties.put("hibernate.show_sql", "false");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpaUnit", properties);
分享到:
相关推荐
NULL 博文链接:https://lyndon-lin.iteye.com/blog/856002
javax.persistence.EntityManagerFactory.class javax.persistence.EntityNotFoundException.class javax.persistence.EntityResult.class javax.persistence.EntityTransaction.class javax.persistence.Enumerated...
主要包括Persistence、EntityManagerFactory、EntityManager和EntityTransaction的具体使用
通过参考和引用传智播客的免费教程,将springmvc4.1.6与hibernate4.3.10提供的JPA...7.注意web.xml,spring.xml,spring.xml,db.properties,persistence.xml文件的配置。 写了这么多,很辛苦,若对你有帮助,请给好评。
Java Persistence with Hibernate is the new bible of Hibernate. As a major revision of the popular Hibernate in Action, it builds on the same single example application to introduce and explain the ...
Manning出版 Java Persistence With Hibernate 第二版 清晰pdf Hibernate, an open source project that is almost 15 years old. In a recent poll, Hibernate was among the top five tools used by many Java ...
这是一个javax.persistence.Entity 的jar包文件。有时候创建JPA工程导入EclipseLink库的时候会缺少这种文件,可以选择在库中加入这个jar包即可
persistence,persistence-api-1.0.2.jar,java,hibernate-jpa
How to use Spring Persistence, including using persistence tools in Spring as well as choosing the best Java persistence frameworks outside of Spring How to work with Spring Framework features such as...
JavaPersistence 2.2.zip JavaPersistence 2.2.zip JavaPersistence 2.2.zip JavaPersistence 2.2.zip JavaPersistence 2.2.zip
jakarta.persistence-api-2.2.3.jar,适合做一些服务器、java映射等的开发,请注意版本!
javax.persistence-api-2.2.jar框架包,仅供大家学习使用,谢谢!
Spring Persistence with Hibernate
Java_Persistence_API 中文pdf
在使用Hibernate连接数据库时可能会报错 缺失的包persistence-api-2.2.1.jar
persistence-2.0.0.jar 包persistence-2.0.0.jar 包persistence-2.0.0.jar 包persistence-2.0.0.jar 包
spring-3.2-persistence.jar
自从起草EJB 3.0的规范开始,无论是在客户端还是在服务器端的应用程序里,Java的类就一直有一种单一的、标准的persistence机制。Java 5的Annotations(批注)功能很容易使用。本文将介绍如何使用它。在本教程里,...