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

时间:2017-04-22 23:38:22

标签: java hibernate intellij-idea

非常愚蠢/简单的问题。执行赋值和休眠无法找到我的hibernate.cfg.xml文件。我正在使用IntelliJ,它位于我的src文件夹中。请参阅下面的代码。

主:

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
@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配置:

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

3 个答案:

答案 0 :(得分:0)

添加[{'name':'family','parent':None,'children':['family/eat/me','family/finance']}, {'name':'family/eat/me','parent':'family','children':[]}, {'name':'family/finance','parent':'family','children':[]}] 这也将找到您的hibernate cfg.xml文件 read this tutorial

答案 1 :(得分:0)

将您的资源文件夹标记为来自INTELLIJ IDEA的资源。 转到:文件 - &gt;项目结构来完成它。

希望,这有帮助。

答案 2 :(得分:0)

将您的XML配置文件hibernate.cfg.xml放在src/main/resources下。

您也可以使用文件→项目结构→添加→休眠手动添加:

enter image description here

然后在同一窗口中添加描述符:

enter image description here

指定描述符的路径,然后按确定