java - SessionFactory 和 Configuration() 的合适导入和 JAR 文件是什么

标签 java hibernate

我在代码中使用了 SessionFactory 和 Configuration()。我做了 Eclipse 建议导入,但是当将鼠标放在“SessionFactory”上时,我的 Eclipse 显示了建议,例如创建本地变量“SessionFactory”并创建一个字段、SessionFactory 的参数并删除分配。

import java.net.URL;
import org.xml.sax.SAXException;

我尝试了上述导入,但它对我不起作用

public class CreateStudentDemo {

    public static void main(String[] args) {

        // Create Session Factory
        SessionFactory = new Configuration()
                .configure("hibernate.cfg.xml")
                .addAnnotatedClass(Student.class)
                .buildSessionFactory();

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

        try {
            //use session object to save java object

    }
        finally{
            factory.close();
        }

适合 SessionFactory 和 Configuration() 的导入或 JAR 文件

最佳答案

我认为jar没有问题更改代码如下

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

关于java - SessionFactory 和 Configuration() 的合适导入和 JAR 文件是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56471491/

相关文章:

mysql - 在 Mysql 上使用 ddl 模式生成时未生成 ON DELETE CASCADE 选项

java - 尝试从文件夹打印数组列表时出错

java - Spring-RMI 事件导致 java.io.InvalidClassException : filter status: REJECTED

java - 从 map 获取列表?

java - AsyncTask 执行串行而不是并行(HTTPost TimeOutException)

java - Hibernate 分离标准

java - 插入新记录时 hibernate 缓存查询未更新

java - 如何根据 Avro 模式验证 JSON

hibernate - 运算符不存在 : bigint = bytea

hibernate - 如何使用Hibernate批注@ManyToOne和@OneToMany进行关联