2017-04-22 179 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

指定描述符的路徑並按確定