jpa动态创建EntityManagerFactory 态设置数据库连接 EntityManager;
2013-12-10 12:42
1371 查看
//jpa动态创建EntityManagerFactory 态设置数据库连接EntityManager;createEntityManagerFactory(String persistenceUnitName, Map properties)方法中properties的参数将覆盖persistence.xml文件配置的参数。
Map<String, String> properties = new HashMap<String, String>();
properties.put("hibernate.connection.url","jdbc:sqlserver://localhost;DatabaseName=test");
properties.put("hibernate.connection.username","sa");
properties.put("hibernate.connection.password","1234");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpaper", properties);
EntityManager em=emf.createEntityManager();
======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/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<!--定义持久单元 -->
<persistence-unit name="jpaper"
transaction-type="RESOURCE_LOCAL">
<!--这里采用Hibernate提供的持久化类 -->
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<!--定义持久化属性 -->
<properties>
<!--定义方言、fetch深度、是否显示sql -->
<property name="hibernate.dialect" value="org.hibernate.dialect.SybaseDialect" />
<property name="hibernate.max_fetch_depth" value="3" />
<property name="hibernate.show_sql" value="true" />
<!--定义是否自动生成表,create表示每次加载都重新生成,update表示每次加载只是更新表 -->
<!-- <property name="hibernate.hbm2ddl.auto" value="update" /> -->
<!--定义数据库连接的相关属性 -->
<property name="hibernate.connection.driver_class"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="hibernate.connection.url"
value="jdbc:sqlserver://192.168.1.181;DatabaseName=smartbusiness" />
<!--定义数据库连接的用户名、密码等 -->
<property name="hibernate.connection.username" value="sa" />
<property name="hibernate.connection.password" value="1.0" />
</properties>
</persistence-unit>
</persistence>
Map<String, String> properties = new HashMap<String, String>();
properties.put("hibernate.connection.url","jdbc:sqlserver://localhost;DatabaseName=test");
properties.put("hibernate.connection.username","sa");
properties.put("hibernate.connection.password","1234");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpaper", properties);
EntityManager em=emf.createEntityManager();
======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/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<!--定义持久单元 -->
<persistence-unit name="jpaper"
transaction-type="RESOURCE_LOCAL">
<!--这里采用Hibernate提供的持久化类 -->
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<!--定义持久化属性 -->
<properties>
<!--定义方言、fetch深度、是否显示sql -->
<property name="hibernate.dialect" value="org.hibernate.dialect.SybaseDialect" />
<property name="hibernate.max_fetch_depth" value="3" />
<property name="hibernate.show_sql" value="true" />
<!--定义是否自动生成表,create表示每次加载都重新生成,update表示每次加载只是更新表 -->
<!-- <property name="hibernate.hbm2ddl.auto" value="update" /> -->
<!--定义数据库连接的相关属性 -->
<property name="hibernate.connection.driver_class"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="hibernate.connection.url"
value="jdbc:sqlserver://192.168.1.181;DatabaseName=smartbusiness" />
<!--定义数据库连接的用户名、密码等 -->
<property name="hibernate.connection.username" value="sa" />
<property name="hibernate.connection.password" value="1.0" />
</properties>
</persistence-unit>
</persistence>
相关文章推荐
- JPA+Spring下,applicationContext.xml中的entityManagerFactory中的class设置问题
- 解决集成jpa时无法创建entityManagerFactory的问题
- Spring三种方法创建JPA EntityManagerFactory的方法
- SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-002设置JPA的EntityManagerFactory(<persistence-unit>、<jee:jndi-lookup>)
- JPA与Spring2.5整合时发生不能创建entityManagerFactory的问题解决方法
- 在Spring环境中建立JPA Spring JPA 提供了三种方法创建JPA EntityManagerFactory:
- JPA+Spring下,applicationContext.xml中的entityManagerFactory中的class设置问题
- SpringBoot实践之---JPA连接数据库+idea全新创建该工程
- 动态改变数据库连接 in Entity Framework 5
- 《Entity Framework 6 Recipes》中文翻译系列 (38) ------ 第七章 使用对象服务之动态创建连接字符串和从数据库读取模型
- 运行时动态设置并加密App.Config中的数据库连接字符串
- JPA--Caused by: javax.persistence.PersistenceException: [PersistenceUnit: mysqlJPA] Unable to configure EntityManagerFactory
- 动态创建数据库和动态连接数据库
- JPA EntityManagerFactory
- JPA EntityManagerFactory Vs Hibernate' SessionFactory
- 动态设置和修改Membership/Profile/RoleProvider的ConnectionString数据库连接字符串
- 动态设置并加密App.Config中数据库连接字符串
- 动态创建数据库连接
- JPA EntityManagerFactory Vs Hibernate’s SessionFactory
- 如何设置对VB数据库连接的动态路径