源文件中的hibernate.cfg.xml仍未找到

时间:2017-08-07 03:45:33

标签: java xml hibernate

我的src文件中有hibernate.cfg.xml文件,但我仍然遇到此错误。请参阅我的HibernateUtil类,错误和xml文件。 我尝试了其他人在其他方面发布的大多数解决方案。

以下是结构的屏幕截图: enter image description here

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HiberUtil {

    private static final SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory from standard (hibernate.cfg.xml) 
            // config file.
            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Log the exception. 
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
        //        Configuration configuration = new Configuration().configure();
//        StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().
//                applySettings(configuration.getProperties());
//        sessionFactory = configuration.buildSessionFactory(builder.build());
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

这是错误:

Aug 06, 2017 10:40:29 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
Aug 06, 2017 10:40:29 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.1.Final}
Aug 06, 2017 10:40:29 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Aug 06, 2017 10:40:29 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Aug 06, 2017 10:40:29 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Aug 06, 2017 10:40:29 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Initial SessionFactory creation failed.org.hibernate.HibernateException: /hibernate.cfg.xml not found
Exception in thread "JavaFX Application Thread" java.lang.ExceptionInInitializerError

如果你需要,这是hibernate.cfg.xml:

<hibernate-configuration>
     <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/cokolada</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.show_sql">true</property>        
        <!-- Mapiranje klasa -->
        <mapping class="model.Cokolada"/>
    </session-factory>
</hibernate-configuration>

1 个答案:

答案 0 :(得分:2)

首先,了解您从其他线程尝试的完全将是有帮助的,以便能够排除故障并帮助诊断您的问题。

其次,它无法找到您的文件,因为它找不到合适的位置。 它需要在您的类路径中。我使用maven并且它必须在这里: 的src /主/资源

Check out this SO post, he gives the best answer here I think.

  

在类路径中需要文件可见的任何其他时间都是一样的。配置文件hibernate.cfg.xml需要在类路径上。确切地说,没有别的关键。这可以通过不同的方式完成,具体取决于您的项目... [关注链接并提升其他人的完整答案]