我写了以下代码: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)

解决方案 »

  1.   

    你使用的是SessionFactory的openSession()方法,你试试在提交事务后,关闭session看看
      

  2.   

    关键是前面的的mapping就没映射到呀!
      

  3.   

    还有就是修改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>
      

  4.   

    包包有没有导入?,还有路径检查下。。xml文件头换成这个
    <!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
      

  5.   

    <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文件,把该加的都加上再调试一下