【问题标题】:JPA: The application must supply JDBC connectionsJPA:应用程序必须提供 JDBC 连接
【发布时间】:2015-12-04 21:36:54
【问题描述】:

我已经在 J​​Boss 7.1 中部署了我的应用程序,并且数据库连接在 JPA persistence.xml 中完成。这是配置

<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
            <property name="hibernate.connection.username" value="postgres" />
            <property name="hibernate.connection.password" value="Prashant11" />
            <property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>  
            <property name="hibernate.connection.URL"  value="false"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost/"/>

获取原因:针对方法 public abstract java.util.List sp.ProfileSessionRemote.getTopProfile(int[]),组件 ProfileSession 上的 EJB 调用失败:javax.ejb.EJBException:java.lang.UnsupportedOperationException:应用程序必须提供 JDBC连接

【问题讨论】:

  • 呃,我假设“hibernate.connection.URL”应该是一个 URL!除此之外,您将 URL 指定为“javax.persistence.jdbc.url”,然后错过了所有其他 STANDARD javax.persistence.jdbc 属性。建议你阅读 JPA 的基础教程

标签: java hibernate jpa jdbc


【解决方案1】:

您使用的是哪个版本的 Hibernate? 看起来你在 hibernate.cfg.xml 文件中混合了 Hibernate 3.x 和 4.x 语法。

休眠 3.x

<properties>
      <property name="hibernate.connection.driver_class" value="...."/>
      <property name="hibernate.connection.url" value="...."/>
      <property name="hibernate.connection.username" value="...."/>
      <property name="hibernate.connection.password" value="...."/>   </properties>

休眠 4.x

<properties>
      <property name="javax.persistence.jdbc.driver" value="...."/>
      <property name="javax.persistence.jdbc.url" value="...."/>
      <property name="javax.persistence.jdbc.user" value="...."/>
      <property name="javax.persistence.jdbc.password" value="...."/>
 </properties>

【讨论】:

    【解决方案2】:

    如果您尝试使用独立连接进行连接,即

    <persistence-unit name="justshop-pu" transaction-type="RESOURCE_LOCAL">. 
    

    这可能是由于:

    <property name="hibernate.connection.URL"  value="false"/>
    

    使用

    <property name="hibernate.connection.url"  value="false"/> 
    

    相反。区分大小写。

    或者

    配置数据源:

    如果您在持久性单元中使用 JTA 作为事务类型,并且由于您尝试从 EJB 进行连接,该 EJB 是容器管理的上下文,因此它将默认为 JTA 事务。您需要配置数据源连接。

    这些是在 JBoss 7.1 中用于 mysql 数据源配置的相同步骤:

    <datasource jndi-name="java:jboss/datasources/ExpenseDS" pool-name="ExpenseDS" enabled="true" use-java-context="true">
                        <connection-url>jdbc:mysql://localhost/expense</connection-url>
                        <driver>com.mysql</driver>
                        <transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>
                        <pool>
                            <min-pool-size>10</min-pool-size>
                            <max-pool-size>100</max-pool-size>
                            <prefill>true</prefill>
                        </pool>
                        <security>
                            <user-name>username</user-name>
                            <password>pwd</password>
                        </security>
                        <statement>
                            <prepared-statement-cache-size>32</prepared-statement-cache-size>
                            <share-prepared-statements>true</share-prepared-statements>
                        </statement>
                    </datasource>
    
    • 还在standalone.xml中添加驱动入口,如下所示:
    <driver name="com.mysql" module="com.mysql">
    <driver-class>com.mysql.jdbc.Driver</driver-class>
    <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
    </driver>
    
    • 如下图创建module.xml文件
    <?xml version="1.0" encoding="UTF-8"?>
    
    <!--
      ~ JBoss, Home of Professional Open Source.
      ~ Copyright 2010, Red Hat, Inc., and individual contributors
      ~ as indicated by the @author tags. See the copyright.txt file in the
      ~ distribution for a full listing of individual contributors.
      ~
      ~ This is free software; you can redistribute it and/or modify it
      ~ under the terms of the GNU Lesser General Public License as
      ~ published by the Free Software Foundation; either version 2.1 of
      ~ the License, or (at your option) any later version.
      ~
      ~ This software is distributed in the hope that it will be useful,
      ~ but WITHOUT ANY WARRANTY; without even the implied warranty of
      ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
      ~ Lesser General Public License for more details.
      ~
      ~ You should have received a copy of the GNU Lesser General Public
      ~ License along with this software; if not, write to the Free
      ~ Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
      ~ 02110-1301 USA, or see the FSF site: http://www.fsf.org.
      -->
    
    <module xmlns="urn:jboss:module:1.1" name="com.mysql">
    
        <resources>
            <resource-root path="mysql-connector-java-5.1.31.jar"/>
            <!-- Insert resources here -->
        </resources>
        <dependencies>
            <module name="javax.api"/>
        </dependencies>
    </module>
    
    • 将它与 mysql 驱动程序 jar 一起作为模块放入 jboss。在jboss的modules中创建一个module文件夹: - 例如:对于 mysql,因为我们将模块作为 com.mysql,我们将创建文件夹 com/mysql 和另一个文件夹 main,并将驱动程序类和 module.xml 放在“com/mysql/main”中(请参阅 h2database 配置,如果你很困惑)

    【讨论】:

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