这是我的文件目录。
我想测试一下,看是否能吧数据写入数据库。这是个测试类:
package com.qiujy.domain;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class CreateAccountTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session session=sessionFactory.openSession();
session.beginTransaction();
Account acc=new Account();
acc.setLoginname("zhangsan");
acc.setPassword("123456");
acc.setEmail("zs@yahoo.cn");
acc.setRegistrationTime(new Date());
session.save(acc);
session.getTransaction().commit();
session.close();
}
}
但是发生错误,信息如下:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.InvalidMappingException: Could not parse mapping document from resource Account.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:569)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1587)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1555)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1534)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1508)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1428)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1414)
at com.qiujy.domain.CreateAccountTest.main(CreateAccountTest.java:18)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from input stream
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:508)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:566)
... 7 more
Caused by: org.dom4j.DocumentException: http://www.jboss.org/dtd/hibernate/hibernate-Mapping-3.0.dtd Nested exception: http://www.jboss.org/dtd/hibernate/hibernate-Mapping-3.0.dtd
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:499)
... 8 more
我的Account.hbm.xml为:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-Mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
" http://hibernate.sourceforge.net/hibernate-Mapping-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-Mapping>
<class name="com.qiujy.domain.Account" table="account">
<id name="id" column="id" type="long">
<generator class="native">
</generator>
</id>
<property name="loginname" column="loginname" type="string" not-null="true"></property>
<property name="password" column="password" type="string" not-null="true"></property>
<property name="email" column="email" type="string"></property>
<property name="registrationTime" column="registrationTime" type="timestamp"></property>
</class>
</hibernate-Mapping>
我的hibernate.cfg.xml文件内容为:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
" http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="connection.url">jdbc:mysql://localhost:3306/myhibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">com.mysql.jdb.Driver</property>
<property name="dialect">org.hibernate.dialect.MYSQLDialect</property>
<mapping resource="Account.hbm.xml"/>
</session-factory>
</hibernate-configuration>
诚请大家帮帮忙,我是hibernate初学者,希望大家帮帮我,谢谢了。
--转自