`

Spring-JPA集成使用心得(转)

阅读更多

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>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics