2012-11-19 58 views
0

我在解析hibernate xml的時候遇到了問題, 這是intellij用這個文件生成的自動項目,但是對於我添加到UserEntity.hbm.xml的doctypehibernate xml解析intellij自動項目中的嵌套異常

我改變了XSD至dtd來回,但仍得到一個異常 我的主要

public class Main { 
private static final SessionFactory ourSessionFactory; 
private static ServiceRegistry serviceRegistry; 

static { 
    try { 
     Configuration configuration = new Configuration(); 
     configuration.configure("hibernate.cfg.xml"); 
     serviceRegistry = new  ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); 
     ourSessionFactory = configuration.buildSessionFactory(serviceRegistry); 

    } catch (Throwable ex) { 
     throw new ExceptionInInitializerError(ex); 
    } 
} 

public static Session getSession() throws HibernateException { 
    return ourSessionFactory.openSession(); 
} 

public static void main(final String[] args) throws Exception { 
    final Session session = getSession(); 
    try { 
     System.out.println("querying all the managed entities..."); 
     final Map metadataMap = session.getSessionFactory().getAllClassMetadata(); 
     for (Object key : metadataMap.keySet()) { 
      final ClassMetadata classMetadata = (ClassMetadata) metadataMap.get(key); 
      final String entityName = classMetadata.getEntityName(); 
      final Query query = session.createQuery("from " + entityName); 
      System.out.println("executing: " + query.getQueryString()); 
      for (Object o : query.list()) { 
       System.out.println(" " + o); 
      } 
     } 
    } finally { 
     session.close(); 
    } 
} 

}

我的hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC 
    "-//Hibernate/Hibernate Configuration DTD//EN" 
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
<session-factory> 
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="hibernate.connection.url">jdbc:mysql://localhost/test</property> 
    <property name="hibernate.connection.username">root</property> 
    <property name="hibernate.connection.password">admin</property> 
    <property name="hibernate.connection.pool_size">10</property> 
    <property name="show_sql">true</property> 
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 
    <property name="hibernate.current_session_context_class">thread</property> 
    <mapping class="dao.UserEntity"/> 
    <mapping resource="dao/UserEntity.hbm.xml"/> 
    <!--<property name="connection.url">jdbc:mysql://localhost/test</property> 
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="connection.username">root</property> 
    <property name="connection.password">admin</property> 
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>--> 
    <!-- DB schema will be updated if needed --> 
    <!-- <property name="hbm2ddl.auto">update</property> --> 
</session-factory> 

我UserEntity.hbm.xml

<?xml version='1.0' encoding='utf-8'?> 
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping XSD//EN" 
    "http://www.hibernate.org/xsd/hibernate-mapping"> 
<hibernate-mapping xmlns="http://www.hibernate.org/xsd/hibernate-mapping" 
    xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-mapping http://www.hibernate.org/xsd/hibernate-mapping/hibernate-mapping-4.0.xsd" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<class name="dao.UserEntity" table="user" schema="" catalog="test"> 
    <id name="id"> 
     <column name="id" sql-type="int" length="10" not-null="true"/> 
    </id> 
    <property name="path"> 
     <column name="path" sql-type="varchar" length="25"/> 
    </property> 
</class> 

我的堆棧跟蹤

Nov 19, 2012 7:39:00 AM org.hibernate.annotations.common.Version <clinit> 
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final} 
Nov 19, 2012 7:39:00 AM org.hibernate.Version logVersion 
INFO: HHH000412: Hibernate Core {4.1.1} 
Nov 19, 2012 7:39:00 AM org.hibernate.cfg.Environment <clinit> 
INFO: HHH000206: hibernate.properties not found 
Nov 19, 2012 7:39:00 AM org.hibernate.cfg.Environment buildBytecodeProvider 
INFO: HHH000021: Bytecode provider name : javassist 
Nov 19, 2012 7:39:00 AM org.hibernate.cfg.Configuration configure 
INFO: HHH000043: Configuring from resource: hibernate.cfg.xml 
Nov 19, 2012 7:39:00 AM org.hibernate.cfg.Configuration getConfigurationInputStream 
INFO: HHH000040: Configuration resource: hibernate.cfg.xml 
Nov 19, 2012 7:39:00 AM org.hibernate.cfg.Configuration addResource 
INFO: HHH000221: Reading mappings from resource: dao/UserEntity.hbm.xml 
Exception in thread "main" java.lang.ExceptionInInitializerError 
at Main.<clinit>(Main.java:32) 
at java.lang.Class.forName0(Native Method) 
at java.lang.Class.forName(Class.java:186) 
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:113) 
Caused by: org.hibernate.InvalidMappingException: Unable to read XML 
at   org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:106) 
at org.hibernate.cfg.Configuration.add(Configuration.java:477) 
at org.hibernate.cfg.Configuration.add(Configuration.java:473) 
at org.hibernate.cfg.Configuration.add(Configuration.java:646) 
at org.hibernate.cfg.Configuration.addResource(Configuration.java:729) 
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2105) 
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2077) 
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2057) 
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2010) 
at org.hibernate.cfg.Configuration.configure(Configuration.java:1925) 
at Main.<clinit>(Main.java:27) 
... 3 more 
Caused by: org.dom4j.DocumentException: http://www.hibernate.org/xsd/hibernate-mapping  Nested exception: http://www.hibernate.org/xsd/hibernate-mapping 
at org.dom4j.io.SAXReader.read(SAXReader.java:484) 
at  org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:76) 
... 13 more 

回答

1

裹在<hibernate-mapping>標籤的class映射如下:

<hibernate-mapping> 
    <class name="dao.UserEntity" table="user" schema="" catalog="test"> 
     <id name="id"> 
     <column name="id" sql-type="int" length="10" not-null="true"/> 
     </id> 
     <property name="path"> 
      <column name="path" sql-type="varchar" length="25"/> 
     </property> 
    </class> 
    </hibernate-mapping> 

此外,我認爲,id屬性也應該有generator類映射以及。

+0

我認爲它使用<發生器類=「分配」 />默認,任何方式這似乎並不相關問題,因爲我添加它,並沒有改變,原因是,intellij中沒有休眠自動項目似乎開箱即用,因爲我創建了休眠3.5 3.6和4.1項目,但都導致錯誤,我確實有訪問數據庫併成功導入架構我也成功地從intellij控制檯查詢數據庫如果User.hbm.xml未映射,我會說它嚴格xml問題,我從xsd更改爲dtd來回沒有更改 – shaydel

+0

@shaydel:更新了ans再換一個發現。 –

+0

它工作後,我也改變爲dtd而不是xsd,new-><?xml version ='1.0'encoding ='utf-8'?> <!DOCTYPE hibernate-mapping PUBLIC 「 - // Hibernate/Hibernate Mapping DTD // EN「 」http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd「> shaydel

0

我認爲文件的.hbm.xml的頭部應該如下:

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