2017-04-22 177 views
0

很愚蠢/简单的问题。执行任务和hibernate无法找到我的hibernate.cfg.xml文件。我使用IntelliJ,它位于我的src文件夹内。看到我的代码如下。Hibernate无法找到我的hibernate.cfg.xml文件

主营:

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> 

回答

0

add configuration.configure("classpath:com/resources/hibernate.cfg.xml");这将找到您的休眠cfg.xml文件也 read this tutorial

+0

试过这个,没有工作 –

+0

你把'cgf.xml'文件放在哪里?在IntelliJ中提到路径 –

+0

,我在Appname/src –

0

从INTELLIJ IDEA资源标记为资源。 转到:文件 - >项目结构来做到这一点。

希望它有帮助。

0

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

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

enter image description here

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

enter image description here

指定描述符的路径并按确定