0

我很难理解如何正确配置 Spring 3.2 和 Hibernate 4.2。在过去几天解决了几个问题后,我在尝试在我的 DAO 中执行查询时遇到了这个异常:

java.lang.UnsupportedOperationException: The application must supply JDBC connections
org.hibernate.service.jdbc.connections.internal.UserSuppliedConnectionProviderImpl.getConnection(UserSuppliedConnectionProviderImpl.java:62)
org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:292)
org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:214)
org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:157)
org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)
org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160)
org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1426)
org.hibernate.ejb.TransactionImpl.begin(TransactionImpl.java:59)
com.bsj.demo.rest.dao.DemoUserDaoImpl.getAllDemoUsers(DemoUserDaoImpl.java:41)

这是我的 Spring 和 Hibernate 文件:

上下文.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                    http://www.springframework.org/schema/tx
                    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:property-placeholder location="classpath:spring/data-access.properties"/>

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/restdemo"/>
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
    </property>
    <property name="persistenceUnitManager">
        <bean class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
             <property name="defaultDataSource" ref="dataSource" />
        </bean>
    </property>
    <property name="dataSource" ref="dataSource" />
</bean>

<context:component-scan base-package="com.bsj.demo.rest.*">
    <context:include-filter type="annotation"  expression="org.springframework.stereotype.Repository" />
</context:component-scan>

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

<context:spring-configured />
<context:annotation-config />
<bean class="com.bsj.demo.rest.spring.config.AppConfig"/>
</beans>

持久性.xml

<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_2_0.xsd"
version="2.0">
<persistence-unit name="RESTDemoJPA" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
    </properties>
</persistence-unit>

我错过了什么?我的理解是,使用 Spring 定义数据源和持久性单元,persistence.xml 中需要的配置应该很少。我在这里完全错了吗?

服务器是 Tomcat 7,依赖项由 Maven 管理。我知道这是关于 SO 的常见问题,但在尝试了几种解决方案后,我还没有找到解决此问题的方法。

4

2 回答 2

1

看看这里。也许它可以解决你的问题。

https://github.com/abdulwaheed18/Spring-HIbernate-Integration/blob/master/src/beans.xml

于 2013-05-31T05:08:43.140 回答
1

好吧,我发现了问题所在,它与配置无关。问题是我如何在我的代码中实例化 EntityManager。

在配置中,我需要将以下内容添加到 entityManager bean 定义中:

<property name="persistenceUnitName" value="RESTDemoJPA" />

更大的问题是我通过从 EntityManagerFactory 获取实例来在每个 DAO 中创建 EntityManager。相反,我应该允许 Spring 通过 PersistenceContext 注释注入它,如下所示:

@PersistenceContext(unitName="RESTDemoJPA")
private EntityManager em;

之后,我删除了所有对 的调用em.getTransaction(),将类注释为@Transactional,异常消失了。之后,我可以查询数据库。

TL;DR:配置正确减去在 entityManager 定义中定义持久性单元名称。将 EntityManager 实例化从手动更改为 Spring 注入。

于 2013-05-31T20:03:07.977 回答