2014-05-15 62 views
3

其实我正在尝试做hibernate日志记录。这是我的hibernate.cfg.xml文件异常:无法解析配置:hibernate.cfg.xml

<?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="hbm2ddl.auto">update</property> 
      <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 
      <property name="connection.url">jdbc:mysql://localhost:3306/temp</property> 
      <property name="connection.username">root</property> 
      <property name="connection.password">root</property> 
      <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
     <mapping resource="employee.hbm.xml"/> 
     </session-factory> 
    </hibernate-configuration> 

这里是例外...

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 com.subhas.mypackage.StoreData.main(StoreData.java:13) 
Caused by: org.dom4j.DocumentException: Connection refused: connect Nested exception: Connection refused: connect 
    at org.dom4j.io.SAXReader.read(SAXReader.java:484) 
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481) 
    ... 2 more 

这是employee.hbm.xml

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

    <hibernate-mapping> 
     <class name="com.subhas.mypackage.Employee" table="emp1000"> 
     <id name="id"> 
     <generator class="assigned"></generator> 
     </id> 

     <property name="firstName"></property> 
     <property name="lastName"></property> 

     </class> 

    </hibernate-mapping> 

这是我的主类

public class StoreData { 
public static void main(String[] args) { 

    //creating configuration object 
    Configuration cfg=new Configuration(); 
    cfg.configure("hibernate.cfg.xml"); 

    //creating session factory object 
    SessionFactory factory=cfg.buildSessionFactory(); 


    //creating session object 
    Session session=factory.openSession(); 


    //creating transaction object 
    Transaction transaction=session.beginTransaction(); 

    Employee employee=new Employee(); 

    employee.setId(5000); 
    employee.setfName("Subhas"); 
    employee.setlName("Gupta"); 


    //persisting the object 
    session.persist(employee); 

    //commit the transaction object 
    transaction.commit(); 
    session.close(); 

    System.out.println("Data Saved Successfully"); 


    } 

    } 

我无法找到一个完美的解决方案。有人可以帮助我吗?谢谢。

+0

尝试删除':您的连接字符串的3306'端口。 – sp00m

+0

@ sp00m我已经尝试删除端口后,但它仍然有相同的例外。 –

+0

我认为您的hibernate.cfg.xml文件中的DOCTYPE标记存在问题。这就是解析器无法识别和解析xml文件的原因。你能否为上面的DOCTYPE添加正确的定义。请检查hibernate.org网站是否有相同的 – vikeng21

回答

3

尝试改变DOCTYPE这样:

<!DOCTYPE hibernate-configuration PUBLIC 
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 
1

我不知道,但我认为你缺少BasicConfiguration.configure()的一些log4j的配置,那么你就不会面对log4j的initailization例外。

0

我的朋友错误只是导致选择不正确的方言。请确保它是正确的,并且您还需要在映射文件中添加列属性。

<class name="Employee" table="EMPLOYEE"> 

    <id name="id" column="ID"> 
     <generator class="assigned"></generator> 
    </id> 

    <property name="name" column="NAME"></property> 
    <property name="mobile" column="MOBILE"></property> 
    <property name="email" column="EMAIL"></property> 

</class> 

敢肯定你的错误获取与此解决...

相关问题