首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“找不到cfg.xml资源[hibernate.cfg.xml]”错误

“找不到cfg.xml资源[hibernate.cfg.xml]”错误
EN

Stack Overflow用户
提问于 2020-03-15 21:29:59
回答 1查看 1.1K关注 0票数 1

当我运行我的createStudentDemo类时,我得到以下错误:

代码语言:javascript
复制
INFO: HHH000412: Hibernate Core {5.4.11.Final}

Exception in thread "main"
org.hibernate.internal.util.config.ConfigurationException: Could not
locate cfg.xml resource [hibernate.cfg.xml] at
org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:53) at
org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:215) at org.hibernate.cfg.Configuration.configure(Configuration.java:258)
at org.hibernate.cfg.Configuration.configure(Configuration.java:244)
at
com.luv2code.hibernate.demo.CreateStudentDemo.main(CreateStudentDemo.java:15)

我不明白为什么我会有这个错误。

下面是我的createStudentDemo类的代码:

代码语言:javascript
复制
package com.luv2code.hibernate.demo;

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

这是我班上学生的代码:

代码语言:javascript
复制
package com.luv2code.hibernate.demo.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="student")
public class Student {

    public Student() {
    }
    @Id
    @Column(name="id")
    private int id;

    @Column(name="first_name")
    private String firstName;

    @Column(name="last_name")
    private String lastName;

    @Column(name="email")
    private String email;

    public Student(String firstName, String lastName, String email) {

        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "Student [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]";
    }
}

下面是我的hibernate.cfg.xml文件的代码:

代码语言:javascript
复制
    <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

    <hibernate-configuration>

        <session-factory>

            <!-- JDBC Database connection settings -->
            <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
            <property name="connection.url">jdbc:mysql://localhost:3306/hb_student_tracker?useSSL=false&amp;serverTimezone=UTC</property>
            <property name="connection.username">hbstudent</property>
            <property name="connection.password">hbstudent</property>

            <!-- JDBC connection pool settings ... using built-in test pool -->
            <property name="connection.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>

    </hibernate-configuration>

下面是我的文件的位置:

有人能帮帮我吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-15 21:32:38

您的hibernate.cfg.xml应该在类路径中。将其放在src文件夹中。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60693439

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档