我使用Eclipse3.2+hibernate3.2下编一个入门的hibernate程序,可是老是出现如下错误,请高手指点

wonderful19891024 2008-06-13 09:46:33
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.MappingNotFoundException: resource: hibernate/ch01/UserInfo.java not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:569)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1593)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1561)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1540)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1514)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1434)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1420)
at hibernate.ch01.HibernateTest.main(HibernateTest.java:15)
我的所有代码如下:
1)持久化类UserInfo.java
package hibernate.ch01;

public class UserInfo {

// 对象id
private Integer id;

// 用户名
private String userName;

// 密码
private String password;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

}
2)映射文件UserInfo.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

<class name="hibernate.ch01.UserInfo" table="login">

<id name="id" type="integer">
<column name="id" />
<generator class="identity"/>
</id>

<property name="userName" type="string">
<column name="name" length="100" />
</property>

<property name="password" type="string">
<column name="password" length="100" />
</property>

</class>

</hibernate-mapping>
3)配置文件hibernate.cfg.xml
<?xml version='1.0' encoding='gb2312'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<!--配置数据库驱动-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!--配置数据库网络连接的url-->
<property name="hibernate.connection.url">jdbc:mysql://localhost/new_table</property>
<!--配置数据库网络连接的用户名,默认一般为root-->
<property name="hibernate.connection.username">root</property>
<!--配置数据库网络连接的密码-->
<property name="hibernate.connection.password">342425</property>
<!--配置数据库网络连接池的大小-->
<property name="hibernate.connection.pool.size">20</property>
<!--后台是否显示sql,开发时为true,运行时为false-->
<property name="hibernate.show_sql">true</property>
<!-- 设置JDBC的隔离级别-->
<property name="hibernate.connection.isolation">2</property>
<property name="hibernate.format_sql">true</property>
<property name="jdbc.fetch_size">50</property>
<property name="jdbc.batch_size">25</property>
<property name="jdbc.use_scrollable_resultset">false</property>
<property name="connection.useUnicode">true</property>
<!--编码方式,最好是gbk,用gb2312有的字符不全-->
<property name="connection.characterEncoding">gb2312</property>
<!--数据库方言,每个数据库都有方言,hibernate已经为大多数数据库指明了方言-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>

<mapping resource="hibernate/ch01/UserInfo.hbm.xml" />
<mapping resource="hibernate/ch01/UserInfo.java" />
</session-factory>
</hibernate-configuration>
4)测试文件HibernateTest.java
package hibernate.ch01;
import hibernate.ch01.UserInfo;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class HibernateTest {

public static void main(String[] args) {
SessionFactory sessions=new Configuration().configure().buildSessionFactory();
Session session = sessions.openSession();
Transaction tx = null;
try {

tx = session.beginTransaction();

UserInfo u = new UserInfo();
u.setUserName("FuJingZhou");
u.setPassword("123");
System.out.println("开始插入数据到数据库……");

session.save(u);

tx.commit();
tx = null;
System.out.println("hi,恭喜你,第一个程序运行成功!");
} catch (HibernateException e) {
e.printStackTrace();
if (tx != null) {
tx.rollback();
}
} finally {
session.close();
}

}
}

很烦恼,入门就碰壁,求助!!!
...全文
213 7 点赞 打赏 收藏 举报
写回复
7 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
谢谢各位的答复,一点小毛病,搞了几天,正是不好意思。
  • 打赏
  • 举报
回复
lixq2000 2008-06-13
MappingNotFoundException 没有找到映射!
  • 打赏
  • 举报
回复
老实的老陈 2008-06-13
<property name="mappingResources">
<list>
<value>hibernate/ch01/UserInfo.hbm.xml</value>

</list>
</property>
  • 打赏
  • 举报
回复
heting1024 2008-06-13
<mapping class="hibernate.ch01.UserInfo" /> 就没事了
  • 打赏
  • 举报
回复
TRMeister 2008-06-13
Exception in thread "main" org.hibernate.MappingNotFoundException: resource: hibernate/ch01/UserInfo.java not found

hbm文件没找到
  • 打赏
  • 举报
回复
interpb 2008-06-13
<mapping resource="hibernate/ch01/UserInfo.java" />

这个文件找不到 换换路径试试
  • 打赏
  • 举报
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2008-06-13 09:46
社区公告
暂无公告