Java 无法分析应用程序中的配置:/hibernate.cfg.xml
我正在尝试在hibernate中执行CRUD操作。我已经为我的应用程序编写了以下步骤。 hibernate.cfg.xmlJava 无法分析应用程序中的配置:/hibernate.cfg.xml,java,xml,hibernate,Java,Xml,Hibernate,我正在尝试在hibernate中执行CRUD操作。我已经为我的应用程序编写了以下步骤。 hibernate.cfg.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibern
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/hibernate
</property>
<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password">
root
</property>
<property name="connection.pool_size">1</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create-drop</property>
<mapping resource="Employee.hbm.xml" />
</session-factory>
</hibernate-configuration>
这是我的util类
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure()
.buildSessionFactory();
} catch (Throwable ex) {
System.err.println("SessionFactory creation failed" + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
这是驾驶舱
import java.util.Iterator;
import java.util.List;
import com.bullraider.crud.util.*;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class Main {
public static void main(String[] args) {
Main m = new Main();
m.saveEmployee("Alex", "MANAGER", 50000, 10);
m.saveEmployee("Mike", "CLERK", 1000, 30);
m.saveEmployee("Tom", "SALESMAN", 2000, 10);
// m.retriveEmployee();
// m.deleteEmployee();
// m.updateEmployee();
}
public void saveEmployee(String ename, String job, int sal, int deptno) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
Employee emp = new Employee();
emp.setEname(ename);
emp.setJob(job);
emp.setSal(sal);
emp.setDeptno(deptno);
session.save(emp);
transaction.commit();
System.out.println("Records inserted sucessessfully");
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
public void retriveEmployee() {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
List employee = session.createQuery("from emp1000").list();
for (Iterator iterator = employee.iterator(); iterator.hasNext();) {
Employee employee1 = (Employee) iterator.next();
System.out.println(employee1.getEmpno() + " "
+ employee1.getEname() + " " + employee1.getJob()
+ " " + employee1.getSal() + " "
+ employee1.getDeptno());
}
transaction.commit();
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
public void deleteEmployee() {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
String queryString = "from emp1000 where deptno = :deptno";
Query query = session.createQuery(queryString);
query.setInteger("deptno", 30);
Employee employee = (Employee) query.uniqueResult();
session.delete(employee);
System.out.println("One employee is deleted!");
// Another way to write it
/*
* String hql = "delete from Employee insurance where deptno = 30";
* Query query1 = session.createQuery(hql); int row =
* query1.executeUpdate(); if (row == 0){
* System.out.println("Doesn't deleted any row!"); } else{
* System.out.println("Deleted Row: " + row); }
*/
System.out.println("One employee is deleted!");
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
public void updateEmployee() {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
String queryString = "from emp1000 where sal = :sal";
Query query = session.createQuery(queryString);
query.setInteger("sal", 8000);
Employee employee = (Employee) query.uniqueResult();
employee.setSal(11000);
session.update(employee);
System.out.println("One employee is updated!");
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
}
什么不起作用?错误信息是什么?你想从我们这里得到什么?!SessionFactory创建失败org.hibernate.hibernate异常:无法在com.bullraider.crud.util.HibernateUtil.(HibernateUtil.java:15)com.bullraider.crud.main.saveEmployee(main.java:24)com.bullraider.crud.main.main(main.java:15)上解析线程“main”java.lang.Exception中的配置:/hibernate.cfg.xml异常这是类路径中的错误消息is文件(在src/下)?是的,先生它在src com.bullraider.crud-->Employee.java&Main.java和com.bullraider.crud.util-->HibernateUtil.java下
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure()
.buildSessionFactory();
} catch (Throwable ex) {
System.err.println("SessionFactory creation failed" + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
import java.util.Iterator;
import java.util.List;
import com.bullraider.crud.util.*;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class Main {
public static void main(String[] args) {
Main m = new Main();
m.saveEmployee("Alex", "MANAGER", 50000, 10);
m.saveEmployee("Mike", "CLERK", 1000, 30);
m.saveEmployee("Tom", "SALESMAN", 2000, 10);
// m.retriveEmployee();
// m.deleteEmployee();
// m.updateEmployee();
}
public void saveEmployee(String ename, String job, int sal, int deptno) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
Employee emp = new Employee();
emp.setEname(ename);
emp.setJob(job);
emp.setSal(sal);
emp.setDeptno(deptno);
session.save(emp);
transaction.commit();
System.out.println("Records inserted sucessessfully");
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
public void retriveEmployee() {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
List employee = session.createQuery("from emp1000").list();
for (Iterator iterator = employee.iterator(); iterator.hasNext();) {
Employee employee1 = (Employee) iterator.next();
System.out.println(employee1.getEmpno() + " "
+ employee1.getEname() + " " + employee1.getJob()
+ " " + employee1.getSal() + " "
+ employee1.getDeptno());
}
transaction.commit();
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
public void deleteEmployee() {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
String queryString = "from emp1000 where deptno = :deptno";
Query query = session.createQuery(queryString);
query.setInteger("deptno", 30);
Employee employee = (Employee) query.uniqueResult();
session.delete(employee);
System.out.println("One employee is deleted!");
// Another way to write it
/*
* String hql = "delete from Employee insurance where deptno = 30";
* Query query1 = session.createQuery(hql); int row =
* query1.executeUpdate(); if (row == 0){
* System.out.println("Doesn't deleted any row!"); } else{
* System.out.println("Deleted Row: " + row); }
*/
System.out.println("One employee is deleted!");
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
public void updateEmployee() {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
String queryString = "from emp1000 where sal = :sal";
Query query = session.createQuery(queryString);
query.setInteger("sal", 8000);
Employee employee = (Employee) query.uniqueResult();
employee.setSal(11000);
session.update(employee);
System.out.println("One employee is updated!");
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
}