休眠:文档无效:未找到语法

Syd*_*ney 10 hibernate

当我使用以下代码实例化Hibernate时:

        Configuration configuration = new Configuration();
        configuration.configure();
        serviceRegistry = new ServiceRegistryBuilder().applySettings(
                configuration.getProperties()).buildServiceRegistry();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Run Code Online (Sandbox Code Playgroud)

我有以下例外情况:

org.xml.sax.SAXParseException:文档无效:未找到语法.,org.xml.sax.SAXParseException:文档根元素"hibernate-configuration"必须与DOCTYPE根"null"匹配.由以下原因引起:org.hibernate.MappingException:在org.hibernate的org.hibernate.cfg.Configuration.configure(Configuration.java:1931)org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2014)中配置无效. cfg.Configuration.configure(Configuration.java:1910)at com.soccer.system.HibernateUtil.(HibernateUtil.java:23)... 1更多引起:org.xml.sax.SAXParseException:文档无效:无语法找到.

我在hibernate.cfg.xmlHibernate 4.1.6中使用以下文件:

<?xml version='1.0' encoding='utf-8'?>

<hibernate-configuration
    xmlns="http://www.hibernate.org/xsd/hibernate-configuration"
    xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-configuration-4.0.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
        <property name="connection.url">jdbc:derby://localhost:1527/K:\db</property>
        <property name="connection.username"></property>
        <property name="connection.password"></property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.DerbyDialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>
    </session-factory>
</hibernate-configuration>
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能消除这种异常?

Joh*_*nna 15

添加行

<!DOCTYPE hibernate-configuration PUBLIC
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
      "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
Run Code Online (Sandbox Code Playgroud)

作为第二行(在xml版本之后和<hibernate-configuration>标记之前.然后它应该工作.

  • 添加`DOCTYPE`是不够的,我还必须从`hibernate-configuration`元素中删除所有属性.发现hibernate更喜欢"DTD"而不是"XSD".您也可以更新`DTD` url以使用`"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"`而不是旧的sourceforge URL. (5认同)