hibernate xml解析intellij自动项目中的嵌套异常

时间:2012-11-19 05:47:23

标签: java hibernate intellij-idea

我在解析hibernate xml时遇到问题, 这是intellij使用这些文件生成的自动项目,但对于我添加到UserEntity.hbm.xml的doctype

我来回更改了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

2 个答案:

答案 0 :(得分:1)

将您的class地图包裹在<hibernate-mapping>标记中,如下所示:

<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类映射。

答案 1 :(得分: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">