简体   繁体   English

Hibernate 找不到我的 hibernate.cfg.xml 文件

[英]Hibernate can't find my hibernate.cfg.xml file

Pretty stupid/simple question.非常愚蠢/简单的问题。 Doing an assignment and hibernate can't find my hibernate.cfg.xml file.做作业和休眠找不到我的 hibernate.cfg.xml 文件。 I'm using IntelliJ and it is located inside my src folder.我正在使用 IntelliJ,它位于我的 src 文件夹中。 See my code below.请参阅下面的代码。

Main:主要的:

public class Main {
    public static void main(String[] args) {

        Employee tempEmployee = new Employee("Ronald", "Customer Service", true);
        EmployeeDAO employeeDAO = new EmployeeDAO();
        employeeDAO.saveEmployee(tempEmployee);

    }
}

DAO:道:

public class EmployeeDAO {

    SessionFactory sessionFactory = new Configuration()
            .configure()
            .addAnnotatedClass(Employee.class)
            .buildSessionFactory();


    public void saveEmployee(Employee employee){
        Session session = sessionFactory.getCurrentSession();

        try {

            session.beginTransaction();
            session.save(employee);
            session.getTransaction().commit();

        } finally {
            session.close();
        }

    }
}

Entity:实体:

@Entity
@Table(name = "employee")
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private long id;

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

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

    @Column(name = "working")
    private boolean working;

    public Employee(){}

    public Employee(String name, String department, boolean working) {
        this.name = name;
        this.department = department;
        this.working = working;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", department='" + department + '\'' +
                ", working=" + working +
                '}';
    }

    public long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public boolean isWorking() {
        return working;
    }

    public void setWorking(boolean working) {
        this.working = working;
    }
}

Hibernate config:休眠配置:

<?xml version="1.0" encoding="utf-8"?>
<!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>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/employee</property>
        <property name="connection.username">employee</property>
        <property name="connection.password">employee</property>
        <property name="dialect">com.hibernate.dialect.MySQL5Dialect</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">create</property>
        <property name="current_session_context_class">thread</property>

    </session-factory>
</hibernate-configuration>

add configuration.configure("classpath:com/resources/hibernate.cfg.xml");添加configuration.configure("classpath:com/resources/hibernate.cfg.xml"); this will locate your hibernate cfg.xml file also read this tutorial这将找到您的休眠 cfg.xml 文件另请阅读本教程

Mark your resources folder as resources from INTELLIJ IDEA.将您的资源文件夹标记为来自 INTELLIJ IDEA 的资源。 Go to: File->Project Structure to do it.转到:文件->项目结构来完成它。

Hope, it helps.希望能帮助到你。

Put your XML configuration file hibernate.cfg.xml under src/main/resources .将 XML 配置文件hibernate.cfg.xml放在src/main/resources下。

Also you can add it manually using File → Project Structure → Add → Hibernate :您也可以使用File → Project Structure → Add → Hibernate手动添加它:

在此处输入图像描述

And then add a descriptor in the same window as:然后在同一窗口中添加一个描述符:

在此处输入图像描述

specify the path to your descriptor and press OK .指定描述符的路径并按OK

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM