0

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
4

2 に答える 2

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クラスマッピングが必要だと思います。

于 2012-11-19T05:58:34.173 に答える