2016-07-29 80 views
1

我是新來的冬眠,我想寫一個簡單的程序,我面對錯誤此行Java代碼:java.lang.unsupportedoperationexception的應用程序必須提供JDBC連接

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", "[email protected]"); 

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

    } 
} 

我正在使用hibernate 5.2.1以防萬一。 我也檢查了其他問題,但他們都沒有幫助。

+0

你的問題呢?或任何錯誤消息?! –

回答

2

好像你有錯字錯誤。更改conncetionconnection,即

<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"> 
0

我想你應該像你的名字屬性使用休眠:

<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"> 

而且也拼錯誤使用連接而不是conncetion

相關問題