【问题标题】:UnsupportedOperationException: The application must supply JDBC connections JPA Hibernate Spring JDBCUnsupportedOperationException:应用程序必须提供 JDBC 连接 JPA Hibernate Spring JDBC
【发布时间】:2017-07-02 06:28:27
【问题描述】:

当我尝试开始交易时

entityManager.getTransaction().begin();

在 AbstractDao 中,我得到了

java.lang.UnsupportedOperationException: The application must supply JDBC connections

我的persistence.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="pu" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
        <property name="hibernate.hbm2ddl.auto" value = "create-drop"/>
    </properties>
</persistence-unit>
</persistence>

我的 hibernate.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ewp?createIfNotExist=true
jdbc.username=root
jdbc.password=root
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.format_sql=false
hibernate.hbm2ddl.auto=create-drop

EntityManagerFactory bean:

    @Bean
public EntityManagerFactory entityManagerFactory(){
    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("pu");
    return entityManagerFactory;
}

EntityManager bean:

@Bean
public EntityManager entityManager(){
    return entityManagerFactory.createEntityManager();
}

【问题讨论】:

    标签: java spring hibernate jpa jdbc


    【解决方案1】:

    从休眠site,您的属性名称看起来不正确。应该是,

    "hibernate.connection.driver_class" for JDBC driver class
    "hibernate.connection.url" for JDBC URL
    "hibernate.connection.username" for database user
    "hibernate.connection.password" for database user password
    

    更正您的物业名称。

    【讨论】:

      【解决方案2】:

      如果你使用的是spring orm using hibernate template 那么你需要提供如下配置。 我们将 Hibernate-entitymanager 用于 Hibernate 作为 JPA 实现。 hibernate-entitymanager 依赖于 hibernate-core,这就是为什么我们不必将 hibernate-core 显式地放在 pom.xml 中。它通过 maven 传递依赖项被拉入我们的项目。

      在spring.xml中

      <beans xmlns=.....>
      <context:component-scan base-package="com.town.practice" />
      <context:annotation-config />
          <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
              <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
              <property name="url" value="jdbc:hsqldb:mem://productDb" />
              <property name="username" value="sa" />
              <property name="password" value="" />
          </bean>
      
          <bean id="entityManagerFactory" 
                  class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
                  p:packagesToScan="com.town.practice.model"
                  p:dataSource-ref="dataSource"
                  >
              <property name="jpaVendorAdapter">
                  <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                      <property name="generateDdl" value="true" />
                      <property name="showSql" value="true" />
                  </bean>
              </property>
          </bean>
      
          <!-- Transactions -->
          <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
              <property name="entityManagerFactory" ref="entityManagerFactory" />
          </bean>
          <!-- enable the configuration of transactional behavior based on annotations -->
          <tx:annotation-driven transaction-manager="transactionManager" />
      
      </beans>
      

      【讨论】:

        猜你喜欢
        • 2013-05-05
        • 2014-07-26
        • 2015-12-04
        • 2014-06-07
        • 2016-12-04
        • 2016-04-17
        • 1970-01-01
        • 2014-09-25
        相关资源
        最近更新 更多