spring-jpa (hibernate实现) 集成心得

本文分享了Spring JPA与Hibernate集成的经验,包括EntityManagerFactory的配置、事务管理、注解驱动的实体管理以及hibernate.hbm2ddl.auto属性的使用注意事项。详细介绍了在Spring配置文件中设置数据源、事务管理和实体管理的相关内容。
摘要由CSDN通过智能技术生成

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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值