【发布时间】:2014-04-21 00:15:33
【问题描述】:
我在 Java Build Path 中添加了 XML 扩展,但是当我运行 mvn test 时显示错误
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] F:\HibernateExample\src\main\java\net\dibyendu\hibernate\Main.java:[53,16] error: cannot find symbol
[INFO] 1 error
另外,当我尝试 Run As Java Application 时,我没有让我的 main class 执行。
这是我的 Main.java 文件编码:
package net.dibyendu.hibernate;
import java.sql.Date;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
public class Main {
public static void main(String[] args) {
// Read
System.out.println("******* READ *******");
List employees = list();
System.out.println("Total Employees: " + employees.size());
// Write
System.out.println("******* WRITE *******");
Employee empl = new Employee("Jack", "Bauer", new Date(System.currentTimeMillis()), "911");
empl = save(empl);
}
private static List list() {
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
List employees = session.createQuery("from Employee").list();
session.close();
return employees;
}
private static Employee read(Long id) {
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
Employee employee = (Employee) session.get(Employee.class, id);
session.close();
return employee;
}
private static Employee save(Employee employee) {
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
Long id = (Long) session.save(employee);
employee.setId(id);
session.getTransaction().commit();
session.close();
return employee;
}
private static Employee update(Employee employee) {
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.merge(employee);
session.getTransaction().commit();
session.close();
return employee;
}
private static void delete(Employee employee) {
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.delete(employee);
session.getTransaction().commit();
session.close();
}
}
我在try catch 中保留错误行时遇到很多错误:
[main] INFO org.hibernate.cfg.Environment - Hibernate 3.5.5-Final
[main] INFO org.hibernate.cfg.Environment - hibernate.properties not found
[main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
[main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
[main] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml
[main] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml
[main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : src/main/resources/Employee.hbm.xml
Initial SessionFactory creation failed.org.hibernate.MappingNotFoundException: resource: src/main/resources/Employee.hbm.xml not found
Exception in thread "main" java.lang.ExceptionInInitializerError
at net.dibyendu.hibernate.HibernateUtil.buildSessionFactory(HibernateUtil.java:18)
at net.dibyendu.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:8)
at net.dibyendu.hibernate.Main.list(Main.java:31)
at net.dibyendu.hibernate.Main.main(Main.java:17)
Caused by: org.hibernate.MappingNotFoundException: resource: src/main/resources/Employee.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:665)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1679)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1647)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1626)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1600)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1520)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1506)
at net.dibyendu.hibernate.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)
... 3 more
我是hibernate 的新手,所以请指导我。
【问题讨论】:
-
为我们提供一些代码。
-
@Henrik ,你要我提供什么代码?
-
Main.java:[53,16] 错误:找不到符号这是哪一行?
-
这是
private static Employee save(Employee employee) {中的[53,16]employee.setId(id); -
你能在 try and catch 中做到这一点并提供异常吗?
标签: java xml eclipse hibernate maven