Ошибка создания SessionFactory Hibernate в базе данных обратного проектирования myeclipse
Я изучаю спящий в myeclipse. Я попытался перепроектировать таблицу MySQL и следовать официальному учебнику по myeclipse. Все XML-файлы созданы, но при написании простой тестовой программы возникает следующая ошибка:
,
17-dic-2012 19.18.57 org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
17-dic-2012 19.18.57 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.4.Final}
17-dic-2012 19.18.57 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
17-dic-2012 19.18.57 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
17-dic-2012 19.18.57 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
17-dic-2012 19.18.57 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
17-dic-2012 19.18.57 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: sor/hibernate/Hitest.hbm.xml
%%%% Error Creating SessionFactory %%%%
org.hibernate.InvalidMappingException: Unable to read XML
at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:109)
at org.hibernate.cfg.Configuration.add(Configuration.java:478)
at org.hibernate.cfg.Configuration.add(Configuration.java:474)
at org.hibernate.cfg.Configuration.add(Configuration.java:647)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:730)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2109)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2081)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2061)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2014)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1929)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1908)
at sor.hibernate.HibernateSessionFactory.<clinit>(HibernateSessionFactory.java:32)
at sor.hibernate.BaseHibernateDAO.getSession(BaseHibernateDAO.java:13)
at sor.hibernate.HitestTest.main(HitestTest.java:15)
Caused by: org.dom4j.DocumentException: http://www.jboss.org/dtd/hibernate/hibernate-mapping-4.0.dtd%20 Nested exception: http://www.jboss.org/dtd/hibernate/hibernate-mapping-4.0.dtd%20
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:78)
... 13 more
17-dic-2012 19.18.59 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
17-dic-2012 19.18.59 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
17-dic-2012 19.18.59 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: sor/hibernate/Hitest.hbm.xml
%%%% Error Creating SessionFactory %%%%
org.hibernate.InvalidMappingException: Unable to read XML
at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:109)
at org.hibernate.cfg.Configuration.add(Configuration.java:478)
at org.hibernate.cfg.Configuration.add(Configuration.java:474)
at org.hibernate.cfg.Configuration.add(Configuration.java:647)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:730)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2109)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2081)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2061)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2014)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1929)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1908)
at sor.hibernate.HibernateSessionFactory.rebuildSessionFactory(HibernateSessionFactory.java:71)
at sor.hibernate.HibernateSessionFactory.getSession(HibernateSessionFactory.java:55)
at sor.hibernate.BaseHibernateDAO.getSession(BaseHibernateDAO.java:13)
at sor.hibernate.HitestTest.main(HitestTest.java:15)
Caused by: org.dom4j.DocumentException: http://www.jboss.org/dtd/hibernate/hibernate-mapping-4.0.dtd%20 Nested exception: http://www.jboss.org/dtd/hibernate/hibernate-mapping-4.0.dtd%20
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:78)
... 14 more
Exception in thread "main" java.lang.NullPointerException
at sor.hibernate.HitestTest.main(HitestTest.java:15)
Тестовая программа
package sor.hibernate;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class HitestTest {
public static void main(String[] args) {
HitestDAO dao = new HitestDAO();
Transaction tx = dao.getSession().beginTransaction();
Hitest hitest[] = new Hitest[10];
for (int i = 0; i < hitest.length; i++) {
hitest[i] = new Hitest();
hitest[i].setGreeting("saluto numero " + i*23);
hitest[i].setRef(i+10);
hitest[i].setDate(new Date(System.currentTimeMillis()));
dao.save(hitest[i]);
}
tx.commit();
dao.getSession().close();
}
}
и hibernate.cfg.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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/efeulib
</property>
<property name="connection.username">root</property>
<property name="connection.password">qwerty</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">
mysqlJDriver
</property>
<mapping resource="sor/hibernate/Hitest.hbm.xml" />
</session-factory>
</hibernate-configuration>
в то время как Hitest.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd ">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping package="sor.hibernate">
<class name="sor.hibernate.Hitest" table="hitest" catalog="test">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="assigned" />
</id>
<property name="greeting" type="java.lang.String">
<column name="greeting" length="200" />
</property>
<property name="date" type="java.util.Date">
<column name="date" length="10" />
</property>
<property name="ref" type="java.lang.Integer">
<column name="ref" />
</property>
</class>
</hibernate-mapping>
Я прочитал соответствующие вопросы и удалил все завершающие строки и пробелы. Но ни один из них не сработал. Единственное, что я добавляю к этим файлам, читая вопросы, - это атрибут package="sor.hibernate" в Hitest.hbm.xml. Пути и... все правильные и все они генерируются автоматически.
Дополнительная информация. Когда я печатаю из hitest в HQL, он говорит мне org.hibernate.hql.ast.QuerySyntaxException: hitest не отображается [from hitest], а из sor.hibernate.Hitest работает хорошо
2 ответа
Попробуйте удалить пробел в
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd "
^-- here
И в любом другом месте, где вы могли совершить ту же ошибку.
Решая проблему, указанную @JB_Nizet, я столкнулся с новой ошибкой о проверке. (Примечание: проблема с пространством была ошибкой myeclipse. Я попытался с проектом, и именно myeclipse добавил это злое пространство.) Я добавил следующую строку в hibernate.cfg.xml, и проблема была частично решена. Новая ошибка была (%%%% Ошибка создания SessionFactory %%%% org.hibernate.HibernateException: Ошибка применения реляционных ограничений BeanValidation)
<property name="javax.persistence.validation.mode">none</property>
Новая ошибка была hibernate sql ошибка запроса. Идентификатор должен был быть назначен, поэтому я изменил Hitest.hbm.xml на
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="sor.hibernate.Hitest" table="hitest" catalog="test">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="increment" />
</id>
<property name="greeting" type="java.lang.String">
<column name="greeting" length="200" />
</property>
<property name="date" type="java.util.Date">
<column name="date" length="10" />
</property>
<property name="ref" type="java.lang.Integer">
<column name="ref" />
</property>
</class>
</hibernate-mapping>