gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:33:12 25 4
gpt4 key购买 nike

非常愚蠢/简单的问题。进行作业和 hibernate 找不到我的 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>

最佳答案

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

关于java - Hibernate 找不到我的 hibernate.cfg.xml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43565809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com