简体   繁体   English

无法解析hibernate.cfg.xml

[英]cannot parse hibernate.cfg.xml

I am new to hibernate .. and I am facing some basic problems : 我是新手hibernate ..我面临一些基本问题:

My xml is 我的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>

I have written 2 classes one which is entity class and other with a main method to store / read operations. 我写了两个类,一个是实体类,另一个是用于存储/读取操作的主方法。 I am using hibernate 4 and once I run my class I get exception 我正在使用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

My jars are 我的罐子是

hibernate-entitymanager-4.3.7.Final hibernate-entitymanager-4.3.7.Final

antlr-2.7.7 ANTLR-2.7.7

dom4j-1.6.1 dom4j的-1.6.1

hibernate-commons-annotations-4.0.5.Final hibernate-commons-annotations-4.0.5.Final

hibernate-core-4.3.7.Final hibernate-core-4.3.7.Final

hibernate-jpa-2.1-api-1.0.0.Final hibernate-jpa-2.1-api-1.0.0.Final

jandex-1.1.0.Final jandex-1.1.0.Final

javassist-3.18.1-GA Javassist进行-3.18.1-GA

jboss-logging-3.1.3.GA jboss-logging-3.1.3.GA

jboss-logging-annotations-1.2.0.Beta1 JBoss的日志记录的注解,1.2.0.Beta1

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

and mysql-connector-java-5.1.33-bin 和mysql-connector-java-5.1.33-bin

am I missing out in jars or have wrong jars? 我错过了罐子里还是错误的罐子?

You need to put your XML processing instruction above your DOCTYPE declaration, so to get rid of the XML parsing error, the start of your file should look like this: 您需要将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">

Second problem: you're referencing both a DTD file (for Hibernate version 3) and an XML schema (for version 4). 第二个问题:您引用的是DTD文件(用于Hibernate版本3)和XML模式(用于版本4)。 You should probably get rid of the DOCTYPE declaration altogether. 您应该完全摆脱DOCTYPE声明。

First, the first line in all XML file, must be something similar to: 首先,所有XML文件中的第一行必须类似于:

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

So, you can try the next: 所以,你可以尝试下一个:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
  <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>

See also XML Validator online and Hibernate Configuration in www.tutorialspoint.com 另请参阅www.tutorialspoint.com中的 在线XML ConfatorHibernate配置

do it like 这样做

 <?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">

        <hibernate-configuration>
        <session-factory>

the problem you have done is you have again defined namespaced in hibernate-configuration tag. 你所遇到的问题是你在hibernate-configuration标签中再次定义了namespaced。

    <!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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM