Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Spring项目中从HibernateAPI迁移到JPAAPI_Java_Spring_Jpa - Fatal编程技术网

Java 在Spring项目中从HibernateAPI迁移到JPAAPI

Java 在Spring项目中从HibernateAPI迁移到JPAAPI,java,spring,jpa,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" ur

我在我的应用程序中使用了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>

org.hibernate.jpa.HibernatePersistenceProvider
jdbc/iBank
错误的
修改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>

org.hibernate.dialen.oracle10galent
符合事实的
在web.xml中,我添加了:

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

jdbc/iBank
javax.sql.DataSource
容器


在应用程序中,我使用SpringSecurity登录,但在进行上述更改后,我无法登录。在服务器上,我没有任何错误。我在配置中犯了哪些错误?

当您实际上没有使用JTA时,为什么要尝试使用它?您不需要在persistence.xml文件中声明您的数据源,请删除它(以及web.xml中的配置和资源引用。事务类型应该是resource_LOCAL而不是JTA。@M.Deinum好的,一切正常。我改为resource_LOCAL,删除persistence.xml中的数据源,删除web.xml中的资源引用。我也删除Apache Tomcat服务器中context.xml中的资源,所有工作都正常。我不需要这个?不需要因为在spring配置中定义数据源是非常无用的。JNDI中的数据源只占用资源。如果要使用它,只需在spring中使用JNDI查找来获取数据源。