2014-02-26 65 views
0

錯誤,我得到了在運行程序Hibernate的配置文件解析異常

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment). 
log4j:WARN Please initialize the log4j system properly. 
Exception in thread "main" org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml 
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1491) 
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1425) 
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1411) 
    at SimpleTest.main(SimpleTest.java:11) 
Caused by: org.dom4j.DocumentException: hibernate.sourceforge.net Nested exception: hibernate.sourceforge.net 
    at org.dom4j.io.SAXReader.read(SAXReader.java:484) 
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481) 
    ... 3 more 

配置文件的信息是,我使用 數據庫連接設置是否正確我知道這一點..

<?xml version='1.0' encoding='UTF-8'?> 
       <!DOCTYPE hibernate-configuration PUBLIC 
       "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
       "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 

       <hibernate-configuration> 

       <session-factory> 

       <property name="hibernate.bytecode.use_reflection_optimizer">false</property> 
     <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
     <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property> 
     <property name="hibernate.connection.username">root</property> 
     <property name="hibernate.connection.password"></property> 
     <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
     <property name="show_sql">true</property> 
        <property name="current_session_context_class">thread</property> 


        <!-- _________ Defining the Mapping Files ___________ --> 

        <mapping resource="Lecturer.hbm.xml" /> 

       </session-factory> 
       </hibernate-configuration> 

主要用於連接hibernate.cfg.xml的類是這樣的

public class SimpleTest { 

       public static void main(String[] args) { 
        System.out.println("innnnnnnnnn"); 
        SessionFactory sessionFactory = new 
         Configuration().configure().buildSessionFactory(); 
        Session session = sessionFactory.getCurrentSession(); 
        Transaction tx = session.beginTransaction(); 

        Lecturer lecturer1 = new Lecturer(); 
        lecturer1.setFirstName("Fatma"); 
        lecturer1.setLastName("Meawad"); 

        session.save(lecturer1); 
        tx.commit(); 

       } 
       } 

table and every think will be created 

普萊舍告訴我哪裏錯了

+0

哪個版本的Hibernate您使用的是? –

+0

您還沒有在配置文件中指定密碼。這可能是原因。 – RAS

回答

0

試試這個在您的SimpleTest的類,

Session session = sessionFactory.openSession(); 
+0

仍然無法工作1!其實問題在解析hibernate.cfg.xml fileSessionFactory sessionFactory = new Configuration()。configure()。buildSessionFactory();它不會返回sessionFactory對象... – Hitesh

+0

發佈您的項目結構的圖像。我想知道你在哪裏放置你的hibernate配置文件 –

+0

嗨Rishi,我把hibernate.cfg.xml文件直接放在src文件夾中...所有文件hibernate.cfg.xml,Lecturer.hbm.xml(映射文件),SimpleTest。的java(主類),在相同packase src文件夾 – Hitesh