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

前端之家收集整理的这篇文章主要介绍了java.lang.UnsupportedOperationException:应用程序必须提供JDBC连接前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我写了一些代码来测试我的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)

我的文件结构如下:

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

这不是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>

主要功能代码如下:

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();

}

根据错误信息,发生错误

Transaction tx = session.beginTransaction();

我是Hibernate的新用户,我已经多次检查我的配置文件.有任何一个帮助找出问题是什么?谢谢!

编辑:Hibernate版本是4.3.5

解决方法

配置文件中的hibernate属性名称区分大小写.
<property name="hibernate.connection.URL">

应该

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

猜你在找的Java相关文章