【问题标题】:java.lang.unsupportedoperationexception The application must supply JDBC connectionsjava.lang.unsupportedoperationexception 应用程序必须提供 JDBC 连接
【发布时间】:2016-12-04 10:36:29
【问题描述】:

我是 hibernate 新手,我正在尝试编写一个简单的程序,但我在 java 代码中遇到了这一行的错误:

session.beginTransaction();

这是我的hibernate.cfg.xml 代码:

<session-factory>

    <!-- JDBC Database conncetion settings -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="conncetion.url">jdbc:mysql://127.0.0.1:3306/hb_student_tracker?useSSL=false</property>
    <property name="conncetion.username">hbstudent</property>
    <property name="connection.password">hbstudent</property>

    <!-- JDBC conncetion pool settings ... using built-in test pool -->
    <property name="conncetion.pool_size">1</property>

    <!-- Select our SQL dialect -->
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

    <!-- Echo the SQL to stdout -->
    <property name="show_sql">true</property>

    <!-- set the current session context -->
    <property name="current_session_context_class">thread</property>

</session-factory>

还有java文件:

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

import com.luv2code.hibernate.demo.entity.Student;

public class CreateStudentDemo {

    public static void main(String[] args) {

        //create session factory
        SessionFactory factory = new Configuration()
                                    .configure("hibernate.cfg.xml")
                                    .addAnnotatedClass(Student.class)
                                    .buildSessionFactory();

        //create session
        Session session = factory.getCurrentSession();

        try{
            // use the session object to save java object

            //create a student object
            System.out.println("Creating new student object");
            Student tempStudent = new Student("Paul", "Wall", "paul@luv2code.com");

            //start a transaction
            session.beginTransaction();

            //save the student object
            System.out.println("Saving the student...");
            session.save(tempStudent);

            //commit transaction
            session.getTransaction().commit();
            System.out.println("Done!");

        }
        finally{
           factory.close();
        }

    }
}

我正在使用休眠 5.2.1 以防万一。 我还检查了其他问题,但没有一个可以提供帮助。

【问题讨论】:

  • 你的问题呢?或任何错误信息?!

标签: java mysql spring hibernate jdbc


【解决方案1】:

好像你有拼写错误。将conncetion改为connection,即

<property name="conncetion.url">
<property name="conncetion.username">
<property name="conncetion.pool_size">

<property name="connection.url">
<property name="connection.username">
<property name="connection.pool_size">

【讨论】:

    【解决方案2】:

    我认为您应该在 name 属性中使用 hibernate,例如:

    <property name="hibernate.connection.driver_class">
    <property name="hibernate.connection.url">
    <property name="hibernate.connection.username">
    <property name="hibernate.connection.password">
    <property name="hibernate.connection.pool_size">
    

    还有拼写错误使用connection而不是conncetion

    【讨论】:

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