SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-002设置JPA的EntityManagerFactory(<persistence-unit>、<jee:jndi-lookup>)
一、EntityManagerFactory的种类
1.The JPA specification defines two kinds of entity managers:
Application-managed—Entity managers are created when an application directly requests one from an entity manager factory. With application-managed entity managers, the application is responsible for opening or closing entity managers
and involving the entity manager in transactions. This type of entity manager is most appropriate for use in standalone applications that don’t run in a Java EE container.
Container-managed—Entity managers are created and managed by a Java EE container. The application doesn’t interact with the entity manager factory at all. Instead, entity managers are obtained directly through injection or from
JNDI . The container is responsible for configuring the entity manager factories.This type of entity manager is most appropriate for use by a Java EE container that wants to maintain some control over JPA configuration beyond what’s specified in persistence.xml.
2.Each flavor of entity manager factory is produced by a corresponding Spring factory bean:
LocalEntityManagerFactoryBean produces an application-managed EntityManagerFactory .
LocalContainerEntityManagerFactoryBean produces a container-managed EntityManagerFactory
二、设置方法
Application-managed entity-manager factories derive most of their configuration information from a configuration file called persistence.xml. This file must appear in the META-INF directory in the classpath.The purpose of the ersistence.xml file is to define one or more persistence units.A persistence unit is a grouping of one or more persistent classes that correspond to a single data source.
1.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="spitterPU">
<class>com.habuma.spittr.domain.Spitter</class>
<class>com.habuma.spittr.domain.Spittle</class>
<properties>
<property name="toplink.jdbc.driver" value="org.hsqldb.jdbcDriver" />
<property name="toplink.jdbc.url" value="jdbc:hsqldb:hsql://localhost/spitter/spitter" />
<property name="toplink.jdbc.user" value="sa" />
<property name="toplink.jdbc.password" value="" />
</properties>
</persistence-unit>
</persistence>
在另一个例子中
<?xml version="1.0"?>
<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="jun" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect"
value="org.hibernate.dialect.MySQLDialect"/><!--数据库方言-->
<property name="hibernate.connection.driver_class"
value="com.mysql.jdbc.Driver"/><!--数据库驱动类-->
<property name="hibernate.connection.username" value="root"/><!--数据库用户名-->
<property name="hibernate.connection.password" value="admin"/>
<property name="hibernate.connection.url"
value="jdbc:mysql://localhost:3306/quote;"/><!--数据库连接URL-->
<property name="hibernate.max_fetch_depth" value="3"/><!--外连接抓取树的最大深度 -->
<property name="hibernate.hbm2ddl.auto" value="update"/><!-- 自动输出schema创建DDL语句 -->
<property name="hibernate.jdbc.fetch_size" value="18"/><!-- JDBC的获取量大小 -->
<property name="hibernate.jdbc.batch_size" value="10"/><!-- 开启Hibernate使用JDBC2的批量更新功能 -->
<property name="hibernate.show_sql" value="true"/><!-- 在控制台输出SQL语句 -->
</properties>
</persistence-unit>
</persistence>
然后bean.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 配置哪些包下的类需要自动扫描 -->
<context:component-scan base-package="com.sanqing"/> <!-- 这里的jun要与persistence.xml中的 <persistence-unit name="jun" transaction-type="RESOURCE_LOCAL">
中的name值要一致,这样才能找到相关的数据库连接
-->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="jun"/>
</bean>
<!-- 配置事物管理器 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<!-- 配置使用注解来管理事物 -->
<tx:annotation-driven transaction-manager="transactionManager"/> </beans>
2.java
@Bean
public LocalEntityManagerFactoryBean entityManagerFactoryBean() {
LocalEntityManagerFactoryBean emfb = new LocalEntityManagerFactoryBean();
emfb.setPersistenceUnitName("spitterPU");
return emfb;
}
The reason much of what goes into creating an application-managed EntityManagerFactory is contained in persistence.xml has everything to do with what it means to be application-managed.
3.Container-managed JPA takes a different approach. When running in a container, an EntityManagerFactory can be produced using information provided by the container—Spring, in this case.Instead of configuring data-source details in persistence.xml, you can configure this information in the Spring application context.
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
LocalContainerEntityManagerFactoryBean emfb =
new LocalContainerEntityManagerFactoryBean();
emfb.setDataSource(dataSource);
emfb.setJpaVendorAdapter(jpaVendorAdapter);
return emfb;
} @Bean
public JpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setDatabase("HSQL");
adapter.setShowSql(true);
adapter.setGenerateDdl(false);
adapter.setDatabasePlatform("org.hibernate.dialect.HSQLDialect");
return adapter;
}
4.The primary purpose of the persistence.xml file is to identify the entity classes in a persistence unit. But as of Spring 3.1, you can do that directly with LocalContainerEntityManagerFactoryBean by setting the packagesToScan property:
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
LocalContainerEntityManagerFactoryBean emfb =
new LocalContainerEntityManagerFactoryBean();
emfb.setDataSource(dataSource);
emfb.setJpaVendorAdapter(jpaVendorAdapter);
emfb.setPackagesToScan("com.habuma.spittr.domain");
return emfb;
}
there’s no need to configure details about the database in persistence.xml. Therefore,there’s no need for persistence.xml whatsoever! Delete it, and let LocalContainerEntityManagerFactoryBean handle it for you.
三、从JNDI中获取EntityManagerFactory
1.xml
<jee:jndi-lookup id="emf" jndi-name="persistence/spitterPU" />
2.java
@Bean
public JndiObjectFactoryBean entityManagerFactory() {
JndiObjectFactoryBean jndiObjectFB = new JndiObjectFactoryBean();
jndiObjectFB.setJndiName("jdbc/SpittrDS");
return jndiObjectFB;
}
Although this method doesn’t return an EntityManagerFactory , it will result in an EntityManagerFactory bean. That’s because it returns JndiObjectFactoryBean ,which is an implementation of the FactoryBean interface that produces an EntityManagerFactory .
SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-002设置JPA的EntityManagerFactory(<persistence-unit>、<jee:jndi-lookup>)的更多相关文章
- SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-006Spring-Data的运行规则(@EnableJpaRepositories、<jpa:repositories>)
一.JpaRepository 1.要使Spring自动生成实现类的步骤 (1)配置文件xml <?xml version="1.0" encoding="UTF- ...
- SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-003编写JPA-based repository( @PersistenceUnit、 @PersistenceContext、PersistenceAnnotationBeanPostProcessor)
一.注入EntityManagerFactory的方式 package com.habuma.spittr.persistence; import java.util.List; import jav ...
- SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-001-使用Hibernate(@Inject、@EnableTransactionManagement、@Repository、PersistenceExceptionTranslationPostProcessor)
一.结构 二.Repository层 1. package spittr.db; import java.util.List; import spittr.domain.Spitter; /** * ...
- SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-005Spring-Data-JPA例子的代码
一.结构 二.Repository层 1. package spittr.db; import java.util.List; import org.springframework.data.jpa. ...
- SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-004JPA例子的代码
一.结构 二.Repository层 1. package spittr.db; import java.util.List; import spittr.domain.Spitter; /** * ...
- SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-004-以query parameters的形式给action传参数(@RequestParam、defaultValue)
一. 1.Spring MVC provides several ways that a client can pass data into a controller’s handler method ...
- SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-005- 异常处理@ResponseStatus、@ExceptionHandler、@ControllerAdvice
No matter what happens, good or bad, the outcome of a servlet request is a servlet response. If an e ...
- SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-003- 上传文件multipart,配置StandardServletMultipartResolver、CommonsMultipartResolver
一.什么是multipart The Spittr application calls for file uploads in two places. When a new user register ...
- SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-002- 在xml中引用Java配置文件,声明DispatcherServlet、ContextLoaderListener
一.所有声明都用xml 1. <?xml version="1.0" encoding="UTF-8"?> <web-app version= ...
随机推荐
- Java Day 13
线程的状态 被创建 运行 冻结 消亡 被创建--start()--> 运行 运行----run()----> 消亡 stop() 运行---sleep(time)---& ...
- git创建分支并提交项目
git 创建分支, 切换分支, 合并分支, 删除分支及提交[commit提交到本地仓库push名利提交到远程服务器], 检出[pull], 冲突修改, 本地仓库同步远程服务器[pul和push命令l] ...
- Android -- Home按键
游戏中常常需要监听android HOME键,当HOME键下压时,往往需要做一些状态保存,音效停止等操作,那么如何做,才能监听到HOME键呢?我们知道HOME是系统键,app中无法通过onKey这些函 ...
- Teamwork-Week3 职责划分及团队分数分配原则
本组人数:5. 一.人员职责划分 PM:1 柴泽华 PM的职责: 1)根据项目范围.质量.时间与成本的综合因素的考虑,进行项目的总体规划与阶段计划. 2)设置项目组中的各种角色, ...
- 嵌套回调异步与$.Deferred异步
HTML: <input type="button" id="btn1" value="嵌套回调异步"> <input t ...
- .NET-提取字符串实践总结
前阶段工作时遇到一个截取字符串的问题,由于字符串比较长,大概得几万字符吧(XML形式),要提取中间两个节点之间的内容,在网上费了好大功夫才找到能用的正则.工作当用的时候碰到这样的事最蛋疼了,网上的资源 ...
- Mysql主从数据库架构的复制原理及配置详解
1 复制概述 Mysql内建的复制功能是构建大型,高性能应用程序的基础.将Mysql的数据分布到多个系统上去,这种分布的机制,是通过将Mysql的某一台主机的数据复制到其它主机(slaves)上,并重 ...
- django构建blog--建立数据库部分+admin部分(eclipse+pydev)
本文介绍的是在eclipse+pydev 平台下,利用django 搭建blog的第1部分:建立数据库+admin部分 步骤一:创建myweb项目+blog应用 1. Eclipse下新建一个djan ...
- JS 时分秒验证
- HTTP1.1缓存策略
以下是一幅虽然信息包含量有限.但足够以最简洁的方式说明了“什么是HTTP1.1缓存策略”的图 缓存和缓存策略 web缓存(web cache)或代理缓存(proxy cache)是一种特殊的HTTP ...