在Spring项目中从Hibernate API迁移到JPA api

时间:2014-11-25 13:09:09

标签: java spring jpa

我在我的应用程序Hibernte API中使用,包括SessionFactory等,但我想转到JPA Api并使用EntityManager等。当我使用Hibernate API时一切正常,但是当我尝试使用JPA Api时我遇到了问题。我已经完成的步骤:

在我的Apache服务器中,我添加到了context.xml:

   <Resource name="jdbc/iBank" auth="Container" type="javax.sql.DataSource"
         url="jdbc:oracle:thin:@localhost:1521:XE"
         driverClassName="oracle.jdbc.driver.OracleDriver"
         username="HR" password="asdfghj"
         maxActive="20" maxIdle="3" maxWait="10000"
         poolPreparedStatements="true"
         maxOpenPreparedStatements="100"
         validationQuery="SELECT SYSDATE FROM DUAL" />

在src \ main \ resources \ META-INF

中添加了文件persistence.xml
    <?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
    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_2_0.xsd">
    <persistence-unit name="iBankPU" transaction-type="JTA">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <jta-data-source>jdbc/iBank</jta-data-source>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>
</persistence>

修改root-context.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:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    <!-- Root Context: defines shared resources visible to all other web components -->

    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:XE" />
        <property name="username" value="HR" />
        <property name="password" value="asdfghj" />
    </bean>

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="iBankPU" />
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="pl.piotr.ibank.model" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">
                org.hibernate.dialect.Oracle10gDialect
            </prop>
            <prop key="hibernate.show_sql">
                true
            </prop>
        </props>
    </property>
</bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <bean id="myAuthenticationSuccessHandler" class="pl.piotr.ibank.security.MyAuthenticationSuccessHandler" />

</beans>

在web.xml中我添加了:

<resource-ref>
<res-ref-name>jdbc/iBank</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>

在应用程序中我使用Spring Security登录,但经过上述更改后我无法登录。在服务器上我没有任何错误。我在配置中犯了什么错误?

0 个答案:

没有答案