高手指点一二!hibernate基础知识

共享to未来 2010-04-25 04:16:35
我写了以下代码:

User.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-mapping>
<class name="com.wwb.hibernate.po.User" table="t_user">
<id name="id" type="java.lang.Integer" column="id">
<generator class="increment"/>
</id>
<property name="name"></property>
</class>

</hibernate-mapping>


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="connection.url">
jdbc:mysql://localhost:3306/test
</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="show_sql">true</property>
<mapping resource="com/wwb/hibernate/po/User.hbm.xml" />
</session-factory>

</hibernate-configuration>



User:



public class User {
private int id;
private String name;
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;
}

}


测试类:

package com.wwb.hibernate.test;

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

import com.wwb.hibernate.po.User;

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Configuration cof=new Configuration();
cof.configure();
SessionFactory sf=cof.buildSessionFactory();
Session session=sf.openSession();
Transaction ts=session.beginTransaction();
User user=new User();
user.setName("dsad");
session.save(user);
ts.commit();
// session.get(User.class, 2);
// session.save(user);

}

}

怎么出现 thread "main" org.hibernate.MappingException: Could not read mappings from resource: com/wwb/hibernate/po/User.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:485)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1465)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1433)
...全文
92 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
cxj61126 2010-04-26
  • 打赏
  • 举报
回复
<property name="cache.use_query_cache">true</property>
<property name="cache.provider_class">
org.hibernate.cache.HashtableCacheProvider
</property>
在hibernate.cfg.xml中加入以上代码,再检查一下hibernate.cfg.xml和User.hbm.xml文件,把该加的都加上再调试一下
angel 2010-04-25
  • 打赏
  • 举报
回复
包包有没有导入?,还有路径检查下。。xml文件头换成这个
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
神之泪花 2010-04-25
  • 打赏
  • 举报
回复
友情帮顶了,
共享to未来 2010-04-25
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 bingking88 的回复:]

写了有错,不是我不写呀!
[/Quote]说错了--报错!
共享to未来 2010-04-25
  • 打赏
  • 举报
回复
写了有错,不是我不写呀!
pspddd 2010-04-25
  • 打赏
  • 举报
回复
还有就是修改hbm.xml文件

User.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-mapping>
<class name="com.wwb.hibernate.po.User" table="t_user">
<id name="id" type="java.lang.Integer" column="id">
<generator class="increment"/>
</id>
<property name="name" type="java.util.String">
<column name="name" /> //这里要写对应的在数据库的字段名
</property>
</class>

</hibernate-mapping>
limitclear 2010-04-25
  • 打赏
  • 举报
回复
[Quote=引用楼主 bingking88 的回复:]
<property name="name"></property>

[/Quote]
这一句没写对,还有对应的表里面的字段没写,这个属性的类型没写
共享to未来 2010-04-25
  • 打赏
  • 举报
回复
关键是前面的的mapping就没映射到呀!
pspddd 2010-04-25
  • 打赏
  • 举报
回复
你使用的是SessionFactory的openSession()方法,你试试在提交事务后,关闭session看看

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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