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

共享to未来 共享to未来 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我写了以下代码:

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)
...全文
评论 9
收藏 已收藏
分享
返回顶部
共享to未来
Java EE 67,535 成员 225,843 内容