2017-08-07 70 views
1

我的src文件裏面有hibernate.cfg.xml文件,我仍然收到這個錯誤。請參閱我的HibernateUtil類,錯誤和xml文件。 我嘗試了其他人在其他人發佈的解決方案。源文件中的hibernate.cfg.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> 
+0

你的意思是在hibernate.cfg.xml?我已經做了。 – Camila

回答

1

首先,知道會有所幫助,確切地說你已經嘗試從其他線程爲了能夠排除東西,並幫助診斷你的問題。

其次,它找不到您的文件,因爲它不在適當的位置找到。 它需要在你的課程路徑。我使用Maven和它是在這裏要求: 的src/main /資源

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

It is the same for any other time you need a file visible on the classpath. The config file hibernate.cfg.xml needs to be on the classpath. Exactly that and nothing else is key. This can be accomplished in different ways, depending on your project... [follow link and upvote the other guy for the full answer]

相關問題