求解:一个hibernate报错的困惑

a88352871 2010-07-14 10:29:13
base.java
package cn.hibernate;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import cn.hibernate.domain.User;

public class Base {

/**
* @param args
*/
public static void main(String[] args) {
Configuration cfg =new Configuration();
cfg.configure();
SessionFactory sf =cfg.buildSessionFactory();

Session s=sf.openSession();
User user =new User();
user.setBirthday(new Date());
user.setName("name");
s.save(user);
s.close();
System.out.println("end");

}

}
User.java
package cn.hibernate.domain;

import java.util.Date;

public class User {
private int id;
private String name;
private Date birthday;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
hibernate.cfg.xml
<!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">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@172.20.3.102:1521:mes</property>
<property name="hibernate.username">test</property>
<property name="hibernate.password">test</property>

<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hbm2ddl.auto">create</property>
<mapping resource="cn/hibernate/domain/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
User.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
package="cn.hibernage.domain">
<class name="User">
<id name="id">
<generator class="native"/>
</id>

<property name="name"/>
<property name="birthday"/>
</class>

</hibernate-mapping>
不知道哪里错了,他报
Exception in thread "main" org.hibernate.InvalidMappingException: Could not parse mapping document from resource cn/hibernate/domain/User.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:588)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1606)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1574)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1553)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1527)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1447)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1433)
at cn.hibernate.Base.main(Base.java:18)
Caused by: org.hibernate.MappingException: class cn.hibernage.domain.User not found while looking for property: id
at org.hibernate.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:209)
at org.hibernate.mapping.SimpleValue.setTypeUsingReflection(SimpleValue.java:279)
at org.hibernate.cfg.HbmBinder.bindSimpleId(HbmBinder.java:401)
at org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValues(HbmBinder.java:334)
at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:273)
at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:144)
at org.hibernate.cfg.Configuration.add(Configuration.java:688)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:523)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:585)
... 7 more
Caused by: java.lang.ClassNotFoundException: cn.hibernage.domain.User
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:169)
at org.hibernate.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:205)
... 15 more
求指点。
...全文
135 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wj63558595 2010-07-14
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wj63558595 的回复:]

引用 4 楼 wj63558595 的回复:

Could not parse mapping document from resource cn/hibernate/domain/User.hbm.xml

在你的hibernate的主配置文件中,配置的User.hbm.xml 错误了。也许是名字错误,也许是路径错误吧
[/Quote]

不好意思,,差点误导你了!!

没看到下面的异常
class cn.hibernage.domain.User not found while looking for property: id

在你的 User.hbm.xml 中
出现了错误。

User.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="cn.hibernage.domain.User" table=""> <!-- 这里填写类的全路径与对应的表名 -->
<id name="id" column="id" > <!-- 这里要把类的属性与表的列名对应起来 -->
<generator class="native"/>
</id>

<property name="name" column="name"/> <!-- 这里要把类的属性与表的列名对应起来 -->
<property name="birthday" column="birthday"/> <!-- 这里要把类的属性与表的列名对应起来 -->
</class>



多看 看hibernate的配置方面的书吧。这些都是hibernate的基础
a88352871 2010-07-14
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 java_cxrs 的回复:]
<hibernate-mapping
package="cn.hibernage.domain">
一哆嗦打错字母了吧?
仔细看看这块哪写错了 呵呵
看看你包名是这个么?
[/Quote]呵呵~我刚才也发现了~晕~怎么之前没看到呢~
不善^ 2010-07-14
  • 打赏
  • 举报
回复
<hibernate-mapping
package="cn.hibernage.domain">
一哆嗦打错字母了吧?
仔细看看这块哪写错了 呵呵
看看你包名是这个么?
wj63558595 2010-07-14
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wj63558595 的回复:]

Could not parse mapping document from resource cn/hibernate/domain/User.hbm.xml
[/Quote]
在你的hibernate的主配置文件中,配置的User.hbm.xml 错误了。也许是名字错误,也许是路径错误吧
wj63558595 2010-07-14
  • 打赏
  • 举报
回复
Could not parse mapping document from resource cn/hibernate/domain/User.hbm.xml
rushly 2010-07-14
  • 打赏
  • 举报
回复
User.hbm.xml

你这个配置文件里面,,User字段你没配类型! 默认是String
soli11722984 2010-07-14
  • 打赏
  • 举报
回复
Could not parse mapping document from resource cn/hibernate/domain/User.hbm.xml
XML文件有错
bingmeng_1102 2010-07-14
  • 打赏
  • 举报
回复
cn.hibernage.domain.User 未找到
看看你的包里有没有这个文件。。

51,411

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧