簡體   English   中英

Hibernate(Session Factory)::和我的 Udemy 老師做同樣的事情,但我的一個不起作用?

[英]Hibernate(Session Factory)::Doing the same as my Udemy teacher but mine one doesnt work?

我正在 udemy 上這門課程。只是 spring 和 hibernate 的初學者。 我嘗試了互聯網上可用的所有方法,但沒有工作。每次錯誤指向“SessionFactory factory = new Configuration()”

   public class CreateStudentDemo {

    public static void main(String[] args) {

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

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

        try{
            System.out.println("Creating new student object....");
            Student tempStudent = new Student("Paul", "Wall", "paul@luv2code.com");
            session.beginTransaction();
            System.out.println("Saving the student..");
            session.save(tempStudent);
            session.getTransaction().commit();
            System.out.println("Done!");
        } finally {
            factory.close();
        }

    }

}

我希望你已經解決了這個問題,但以防萬一這是問題的主要因素。 當您執行第一個 new Configuration() 時,很可能它導入了不同的 Configuration() 而不是 hibernate 根目錄。

Configuration() 應該導入這個根

import org.hibernate.cfg.Configuration;

然后它會工作

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

檢查圖片

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM