繁体   English   中英

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

[英]Add XML extension on project build path in Eclipse not working

在此处输入图片说明

我在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 Run As时,我没有在执行我的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 ,请指导我。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM