在Eclipse中的项目构建路径上添加XML扩展不起作用

时间:2014-03-15 16:25:20

标签: java xml eclipse hibernate maven

enter image description here

我在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的新手,所以请指导我。

1 个答案:

答案 0 :(得分:0)

很高兴告诉Eclipse您已将xml文件添加到类路径中,但您还没有告诉Maven。 Maven将类路径文件分为两组:可编译(在src/main/javasrc/test/java下)和不可编译的又名资源(在src/main/resourcessrc/test/resources下)。首选解决方案是将这些xml文件移动到正确的文件夹中。使用m2eclipse时,只需更新项目,eclipse将为您添加这些源文件夹。 顺便说一句。这并不能解释编译错误,这只是您面临的问题之一。