1)EntityManagerFactory的persistenceXmlLocation属性可以不指定,会自动搜索
classpath:META-INF/persistence.xml.下面的该项配置相当于没有.
2)jpa必须启用事物,否则无法更新数据
到数据库.
3)若DAO中注入的是EntityManagerFactory,则只能自己写事物代码.spring无法接管.
4)若注入
的是EntityManager则可以让spring接管事物管理.
5)使用注解@PersistenceContext自动注入
EntityManager,则必须在pring配置文件中声明<context:annotation-config/>,context
命名空间以及schemaLocation.
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
6)
在persistence.xml中若配置了<property name="hibernate.hbm2ddl.auto"
value="create" />,那么每次访问数据库都会创建新的表。导致始终只插入最后一条数据。可能值有
none
无
validate 加载hibernate时,验证创建数据库表结构
create 每次加载hibernate,重新创建数据库表结构,这就是导致数据库表数据丢失的原因。
create-drop 加载hibernate时创建,退出是删除表结构
update
加载hibernate自动更新数据库结构
7)如果要使用hibernate自身的功能(JPA没有),则在spring配置文件中配置
entityManagerFactory时,必须配置其LocalContainerEntityManagerFactoryBean的
jpaVendorAdapter属性。
8)数据源即可以在spring配置文件中配置,也可以在persistence.xml中配置.若配置
在spring中,必须在jpaVendorAdapter中配置 database属性,其值为ORACLE,MYSQL等spring中定义枚举值
spring
配置文件:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com.hj.jms"/>
<!-- JPA EntityManagerFactoryBean for
EntityManager-->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="jms" />
<property name="jpaVendorAdapter">
<bean
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
</bean>
</property>
</bean>
<!-- Transaction manager for JPA -->
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory"><ref
bean="entityManagerFactory"/></property>
</bean>
<aop:config>
<aop:pointcut
id="productServiceMethods" expression="execution(*
com.hj.jms.*.*DAOImpl.*(..))"/>
<aop:advisor
advice-ref="txAdvice" pointcut-ref="productServiceMethods"/>
</aop:config>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="get*"
read-only="true" />
<tx:method name="*"
propagation="REQUIRES_NEW"/>
</tx:attributes>
</tx:advice>
</beans>
jpa配置文件
(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="jms"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property
name="hibernate.show_sql" value="true" />
<property
name="hibernate.hbm2ddl.auto" value="none" />
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.connection.driver_class"
value="com.mysql.jdbc.Driver"/>
<property
name="hibernate.connection.url" value="jdbc:mysql://127.0.0.1/jms"/>
<property name="hibernate.connection.username"
value="root"/>
<property
name="hibernate.connection.password" value="101"/>
<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.MySQL5Dialect" />
</properties>
</persistence-unit>
</persistence>
分享到:
相关推荐
赠送jar包:spring-data-jpa-2.0.9.RELEASE.jar; 赠送原API文档:spring-data-jpa-2.0.9.RELEASE-javadoc.jar; 赠送源代码:spring-data-jpa-2.0.9.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-data-jpa...
赠送jar包:spring-data-jpa-2.0.9.RELEASE.jar; 赠送原API文档:spring-data-jpa-2.0.9.RELEASE-javadoc.jar; 赠送源代码:spring-data-jpa-2.0.9.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-data-jpa...
spring-jpa.jar........................................
spring-data-jpa spring data jap
spring data jpa最新版本1.8.0,包含了spring-data-jpa-1.8.0.RELEASE.jar,spring-data-jpa-1.8.0.RELEASE-javadoc.jar以及 spring-data-jpa-1.8.0.RELEASE-sources.jar文档和源代码
spring-data-jpa
java运行依赖jar包
spring-data-jpa-reference1.3.pdf
使用SpringBoot 集成Spring-data-jpa,Druid连接池,thymeleaf模板实现的一个简单网上商城项目
使用SpringBoot和Spring-data-jpa进行分页查询可以大大简化编码流程并提高开发效率。 在使用Spring-data-jpa进行分页查询时,需要遵循以下步骤: 引入依赖 在pom.xml文件中加入spring-boot-starter-data-jpa依赖,...
JavaEE源代码 spring-jpaJavaEE源代码 ...jpaJavaEE源代码 spring-jpaJavaEE源代码 spring-jpaJavaEE源代码 spring-jpaJavaEE源代码 spring-jpaJavaEE源代码 spring-jpaJavaEE源代码 spring-jpaJavaEE源代码 spring-jpa
NULL 博文链接:https://mybar.iteye.com/blog/1863390
实现spring-data-jpa的增删改查方法,该项目是maven项目,需要maven支持
java运行依赖jar包
java运行依赖jar包
《Spring-Data-JPA快速使用---我瞎写了》从核心实现和企业应用两个方面,由浅入深、由易到难地对Spring源码展开了系统的讲解,包括Spring的设计理念和整体架构、容器的基本实现、默认标签的解析、自定义标签的解析、...
赠送jar包:hibernate-jpa-2.1-api-1.0.2.Final.jar; 赠送原API文档:hibernate-jpa-2.1-api-1.0.2.Final-javadoc.jar; 赠送源代码:hibernate-jpa-2.1-api-1.0.2.Final-sources.jar; 赠送Maven依赖信息文件:...
spring-data-jpa知识。
Spring Boot使用spring-data-jpa配置Mysql多数据源,可用版本
弹簧动力jpa Spring Dynamic JPA将使使用JpaRepository轻松实现动态查询变得容易。如何使用? 添加依赖implementation ' ...