休眠:文档无效:找不到语法 [英] Hibernate: Document is invalid: no grammar found
问题描述
配置配置=新配置();使用下面的代码实例化Hibernate:
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder()。applySettings(
configuration.getProperties())。buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
我有以下例外情况:
org.xml.sax.SAXParseException:Document无效:找不到语法,
org.xml.sax.SAXParseException:文档根元素
hibernate-configuration,必须匹配DOCTYPE根目录null。
导致:org.hibernate.MappingException:无效配置
在org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2014)
在org.hibernate.cfg.Configuration.configure (Configuration.java:1931)
在org.hibernate.cfg.Configuration.configure(Configuration.java:1910)
在com.soccer.system.HibernateUtil。(HibernateUtil.java:23)
... 1 more
导致:org.xml.sax.SAXParseException:文档无效:找不到语法。
在Hibernate 4.1.6中使用以下 hibernate.cfg.xml
文件:
<?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>
<! - 数据库连接设置 - >
< 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连接池(使用内置) - >
< property name =connection.pool_size> 1< / property>
<! - - SQL方言 - >
< property name =dialect> org.hibernate.dialect.DerbyDialect< / property>
<! - 启用Hibernate的自动会话上下文管理 - >
< property name =current_session_context_class>线程< / property>
<! - 禁用二级缓存 - >
< property name =cache.provider_class> org.hibernate.cache.internal.NoCacheProvider< / property>
<! - 将所有执行的SQL回复到stdout - >
< property name =show_sql> true< / property>
<! - 在启动时删除并重新创建数据库模式 - >
< property name =hbm2ddl.auto>更新< / property>
< / session-factory>
< / hibernate-configuration>
我能做些什么来消除此异常?
添加行
<!DOCTYPE hibernate-configuration PUBLIC
- // Hibernate / Hibernate配置DTD 3.0 // EN
http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd>
作为第二行(在xml版本之后和< hibernate -configuration>
标签,然后它就可以工作了。
When I instantiate Hibernate using the following code:
Configuration configuration = new Configuration();
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(
configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
I got the following exceptions:
org.xml.sax.SAXParseException: Document is invalid: no grammar found., org.xml.sax.SAXParseException: Document root element "hibernate-configuration", must match DOCTYPE root "null". Caused by: org.hibernate.MappingException: invalid configuration at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2014) at org.hibernate.cfg.Configuration.configure(Configuration.java:1931) at org.hibernate.cfg.Configuration.configure(Configuration.java:1910) at com.soccer.system.HibernateUtil.(HibernateUtil.java:23) ... 1 more Caused by: org.xml.sax.SAXParseException: Document is invalid: no grammar found.
I use the following hibernate.cfg.xml
file with Hibernate 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>
What can I do to eliminate this exception?
Add the line
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
as the second line (after the xml version and before the <hibernate-configuration>
tag. Then it should work.
这篇关于休眠:文档无效:找不到语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!