Java 找不到文件:hibernate.cgf.xml

Java 找不到文件:hibernate.cgf.xml,java,spring,hibernate,Java,Spring,Hibernate,我读了很多帖子,但没有一篇适合我的问题。 我将hibernate.cgf.xml放在eclipse的“src”文件夹下,这是根据我在互联网上阅读一些示例所学到的 但是当我运行我的应用程序时,仍然会出现错误“找不到文件:hibernate.cgf.xml” 这是我的web应用程序 public class TestHibernateInsert { public static void main(String[] args) { Session session =

我读了很多帖子,但没有一篇适合我的问题。 我将hibernate.cgf.xml放在eclipse的“src”文件夹下,这是根据我在互联网上阅读一些示例所学到的

但是当我运行我的应用程序时,仍然会出现错误
“找不到文件:hibernate.cgf.xml”

这是我的web应用程序

public class TestHibernateInsert {
    public static void main(String[] args)
    {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();

        //Add new Employee object
        EmployeeEntity emp = new EmployeeEntity();
        emp.setEmail("lokesh@mail.com");
        emp.setNome("Nome");
        emp.setCognome("Cognome");
        emp.setPassword("Password");

        //Save the employee in database
        session.save(emp);

        //Commit the transaction
        session.getTransaction().commit();
        HibernateUtil.shutdown();
    }
}

这是启动应用程序的类

public class TestHibernateInsert {
    public static void main(String[] args)
    {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();

        //Add new Employee object
        EmployeeEntity emp = new EmployeeEntity();
        emp.setEmail("lokesh@mail.com");
        emp.setNome("Nome");
        emp.setCognome("Cognome");
        emp.setPassword("Password");

        //Save the employee in database
        session.save(emp);

        //Commit the transaction
        session.getTransaction().commit();
        HibernateUtil.shutdown();
    }
}
这是SessionFactory类

   public class HibernateUtil
    {
        private static final SessionFactory sessionFactory = buildSessionFactory();

        private static SessionFactory buildSessionFactory()
        {
            try
            {
                // Create the SessionFactory from hibernate.cfg.xml
                return new AnnotationConfiguration().configure(new File("hibernate.cgf.xml")).buildSessionFactory();
            }
            catch (Throwable ex) {
                // Make sure you log the exception, as it might be swallowed
                System.err.println("Initial SessionFactory creation failed." + ex);
                throw new ExceptionInInitializerError(ex);
            }
        }

        public static SessionFactory getSessionFactory() {
            return sessionFactory;
        }

        public static void shutdown() {
            // Close caches and connection pools
            getSessionFactory().close();
        }
    }
这是持久对象类

Entity
@Table(name = "utente")
public class EmployeeEntity implements Serializable{

    private static final long serialVersionUID = -1798070786993154676L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    private Integer id;

    @Column(name = "email", unique = true, nullable = false, length = 100)
    private String email;

    @Column(name = "nome", unique = false, nullable = false, length = 100)
    private String nome;

    @Column(name = "cognome", unique = false, nullable = false, length = 100)
    private String cognome;

    @Column(name = "password", unique = false, nullable = false, length = 100)
    private String password;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getCognome() {
        return cognome;
    }

    public void setCognome(String cognome) {
        this.cognome = cognome;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    //Getters and setters

}

您似乎已经使用eclipse开发了一个web应用程序。默认情况下,hibernate在类路径的根目录中查找
hibernate.cfg.xml
文件


因此,最后该文件应该放在
WEB-INF/classes
目录中,该目录是WEB应用程序的类路径的根。

请将您的文件从
hibernate.cgf.xml
重命名为
hibernate.cfg.xml

并更改HibernateUtil类中的
buildSessionFactory()
方法

        private static SessionFactory buildSessionFactory()
        {
            try
            {
                // Create the SessionFactory from hibernate.cfg.xml
                return new AnnotationConfiguration().configure().buildSessionFactory();
            }
            catch (Throwable ex) {
                // Make sure you log the exception, as it might be swallowed
                System.err.println("Initial SessionFactory creation failed." + ex);
                throw new ExceptionInInitializerError(ex);
            }
        }

将现有配置文件重命名为hibernate.cfg.xml

或者使用以下代码创建会话

Configuration cfg=新配置();
configure(“hibernate.cgf.xml”)

请发布相关代码。@MatteoDepasquali,您能编辑您的问题并显示文件的位置吗?很抱歉浪费您的时间。我将xml文件放在WEB-INF/classes下,现在它可以工作了。我已经试过了,但没有解决问题,可能是eclipse的问题。我清理了我的项目,重新启动了eclipse,现在它可以工作了。谢谢:)