我是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>
我写了两个类,一个是实体类,另一个是用于存储/读取操作的主方法。 我正在使用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
我的罐子是
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测井的注解-1.2.0.Beta1
jboss-transaction-api_1.2_spec-1.0.0.Final
和mysql-connector-java-5.1.33-bin
我是否错过了罐子里或者有错误的罐子?答案 0 :(得分:2)
您需要将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
声明。
答案 1 :(得分:1)
首先,所有XML文件中的第一行行必须类似于:
<?xml version='1.0' encoding='utf-8'?>
所以,你可以尝试下一个:
<?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>
另请参阅XML Validator online和Hibernate Configuration in www.tutorialspoint.com
答案 2 :(得分:0)
就像
一样 <?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>
您所遇到的问题是您已在hibernate-configuration
标记中再次定义了命名空间。
<!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>