我在myeclipse中學習hibernate。我試圖逆向工程一個MySQL表,並遵循官方myeclipse教程。所有的XML文件生成,但寫一個簡單的測試程序,它給了我以下錯誤:錯誤在myeclipse中創建SessionFactory Hibernate反向工程數據庫
。
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>
我已閱讀有關的問題和我刪除了所有的尾隨線條和空間。但他們都沒有工作。我在這些文件中添加的唯一東西是Hitest.hbm.xml中的package =「sor.hibernate」屬性。 路徑和...都是正確的,它們都是自動生成的。
更多信息。當我鍵入從HQL hitest,它告訴我 org.hibernate.hql.ast.QuerySyntaxException:hitest沒有映射[從hitest] 而從sor.hibernate.Hitest效果很好
請注意,「來自hitest」正在尋找「hitest」而非「Hitest」的映射。名稱區分大小寫。 –
非常感謝!它適用於「來自Hitest」 – Johnny