【问题标题】:Add XML extension on project build path in Eclipse not working在 Eclipse 中的项目构建路径上添加 XML 扩展不起作用
【发布时间】: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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-02
    • 2012-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    相关资源
    最近更新 更多