gpt4 book ai didi

java - 无法解析 hibernate.cfg.xml

转载 作者:行者123 更新时间:2023-11-29 05:14:55 25 4
gpt4 key购买 nike

我是 hibernate 新手..我面临一些基本问题:

我的xml是

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<?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">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/database</property>
<property name="connection.username">user</property>
<property name="connection.password">user123</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</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.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">create</property>
<mapping class="hibernateDTO.StudentDetails"/>
</session-factory>
</hibernate-configuration>

我写了 2 个类,一个是实体类,另一个是用 main 方法来存储/读取操作。我正在使用 hibernate 4,一旦我开始上课,我就会遇到异常

Nov 13, 2014 10:58:58 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.7.Final}
Nov 13, 2014 10:58:58 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Nov 13, 2014 10:58:58 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Nov 13, 2014 10:58:58 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Nov 13, 2014 10:58:58 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Exception in thread "main" org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2165)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2077)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2056)
at hibernateTest.Hirbernate.main(Hirbernate.java:20)
Caused by: org.dom4j.DocumentException: Error on line 5 of document : The processing instruction target matching "[xX][mM][lL]" is not allowed. Nested exception: The processing instruction target matching "[xX][mM][lL]" is not allowed.
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2157)
... 3 more

我的 jar 是

hibernate-entitymanager-4.3.7.Final

antlr-2.7.7

dom4j-1.6.1

hibernate-commons-annotations-4.0.5.Final

hibernate-core-4.3.7.Final

hibernate-jpa-2.1-api-1.0.0.Final

jandex-1.1.0.Final

javassist-3.18.1-GA

jboss-logging-3.1.3.GA

jboss-logging-annotations-1.2.0.Beta1

jboss-transaction-api_1.2_spec-1.0.0.Final

和 mysql-connector-java-5.1.33-bin

我是漏掉了 jar 还是放错了 jar ?

最佳答案

您需要将 XML 处理指令放在 DOCTYPE 声明之上,以便消除 XML 解析错误,文件的开头应如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

第二个问题:您正在引用 DTD 文件(对于 Hibernate 版本 3)和 XML 模式(对于版本 4)。您可能应该完全摆脱 DOCTYPE 声明。

关于java - 无法解析 hibernate.cfg.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26922727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com