java.lang.UnsupportedOperationException: 应用程序必须提供 JDBC 连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23222632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:25:51  来源:igfitidea点击:

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

javahibernatejdbc

提问by ysfseu

I write some code to test my configuration of Hibernate.But I come across such error message:

我写了一些代码来测试我的 Hibernate 配置。但是我遇到了这样的错误消息:

java.lang.UnsupportedOperationException: The application must supply JDBC connections
at org.hibernate.engine.jdbc.connections.internal.UserSuppliedConnectionProviderImpl.getConnection(UserSuppliedConnectionProviderImpl.java:61)
at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:380)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:228)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:171)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:162)
at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1435)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:356)
at com.sun.proxy.$Proxy0.beginTransaction(Unknown Source)

My file structure is like below:

我的文件结构如下:

src
---test
-------Test.java(with main function)
-------User.java
-------User.hbm.xml
----hibernate.cfg.xml

This is not a web application,it's just an ordinary java project.The hibernate.cfg.xml is like below:

这不是一个web应用程序,它只是一个普通的java项目。hibernate.cfg.xml如下所示:

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>

    <session-factory>
        <!-- Configure MySQL -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.URL">jdbc:mysql://localhost:3306/mags</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">bysjysf</property>
        <property name="show_sql">true</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.current_session_context_class">thread</property>
        <!-- Mapping Files -->
        <mapping resource="test/User.hbm.xml" />
    </session-factory>

</hibernate-configuration>

The code in main function is like below:

main函数中的代码如下:

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Configuration cfg = new Configuration().configure("hibernate.cfg.xml");         
    StandardServiceRegistryBuilder sb = new StandardServiceRegistryBuilder();
    sb.applySettings(cfg.getProperties());
    StandardServiceRegistry standardServiceRegistry = sb.build();                   
    SessionFactory sessionFactory = cfg.buildSessionFactory(standardServiceRegistry);
    Session session = sessionFactory.getCurrentSession();
    System.out.println(session);
    Transaction tx = session.beginTransaction();
    User user = new User();
    user.setPassword("aaaa");
    user.setUsername("ysf");
    user.setAuthority(1);
    session.save(user);
    tx.commit();
    session.close();

}

According the error message, the error occurred in

根据错误信息,错误发生在

Transaction tx = session.beginTransaction();

I am a new user for Hibernate,and I have check my configuration file many times.Could any one help to figure out what the problem is? Thanks!

我是 Hibernate 的新用户,我已经多次检查我的配置文件。有人可以帮助找出问题所在吗?谢谢!

EDIT:the Hibernate version is 4.3.5

编辑:Hibernate 版本是 4.3.5

采纳答案by Mubin

The hibernate property names in the configuration file are case sensitive.

配置文件中的休眠属性名称区分大小写。

<property name="hibernate.connection.URL">

should be

应该

<property name="hibernate.connection.url">

回答by Khagesh Sharma

com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/mytest root root 10

com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/mytest root root 10

    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.hbm2ddl.auto">create</property>
    <!-- <property name="hbm2ddl.auto">update</property> -->
    <property name="hibernate.show_sql">true</property>

    <mapping resource="resources/student.hbm.xml" />
    <mapping resource="resources/course.hbm.xml" />
    <mapping resource="resources/department.hbm.xml" />
    <mapping resource="resources/employee.hbm.xml" />

</session-factory>

package test;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class client {

    public static void main(String[] args) {

        Configuration cfg = new Configuration();
        cfg.configure("resources/hibernate.cfg.xml");
        cfg.buildSessionFactory();

    }
}

I am getting an error in buildSessionFactory();

我在 buildSessionFactory() 中遇到错误;