本帖最后由 lilvhuiling 于 2012-01-15 16:37:57 编辑

解决方案 »

  1.   

    下面是我的源码:package com.lv.hibernate.model;public class Student {
    private int id;
    private String sname;
    private int age;

    public int getAge() {
    return age;
    }

    public int getId() {
    return id;
    }

    public String getSname() {
    return sname;
    }

    public void setAge(int age) {
    this.age = age;
    }

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

    public void setSname(String sname) {
    this.sname = sname;
    }
    }
      

  2.   

    上面是model类,现在是测试类:import org.hibernate.SessionFactory;
    import org.hibernate.cfg.*;
    import org.hibernate.Session;
    import com.lv.hibernate.model.Student;
    public class StudentTest {
    public static void main(String[] args) {
    Student s = new Student();
    s.setId(1);
    s.setSname("s1");
    s.setAge(1);

    Configuration cfg = new Configuration();
    SessionFactory sf = cfg.configure().buildSessionFactory();
    //SessionFactory sf = new Configuration().configure().buildSessionFactory();
    Session session = sf.openSession();
    session.beginTransaction();
    session.save(s);
    session.getTransaction().commit();
    session.close();
    sf.close();
    }
    }
      

  3.   

    我的配置文件:hibernate.cfg.xml<?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration>    <session-factory>        <!-- Database connection settings -->
            <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="connection.url">jdbc:mysql://localhost/hibernate</property>
            <property name="connection.username">sa</property>
            <property name="connection.password">lvailing</property>        <!-- JDBC connection pool (use the built-in) -->
            <!-- <property name="connection.pool_size">1</property> -->        <!-- SQL dialect -->
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>        <!-- Enable Hibernate's automatic session context management -->
            <!-- <property name="current_session_context_class">thread</property> -->        <!-- Disable the second-level cache  -->
            <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>        <!-- Echo all executed SQL to stdout -->
            <property name="show_sql">true</property>        <!-- Drop and re-create the database schema on startup -->
            <!-- <property name="hbm2ddl.auto">update</property> -->
            
            <mapping resource="com/lv/hibernate/model/Student.hbm.xml"/>    </session-factory></hibernate-configuration
    >
      

  4.   

    Student.hbm.xml配置文件中class标签的name属性应该是Student,你写成student了
      

  5.   

    student Student 很明显。。,
      

  6.   

    <hibernate-mapping package="com.lv.hibernate.model">
        <class name="student">
            <id name="id"></id>
            <property name="sname"></property>
            <property name="age"></property>
        </class>
    </hibernate-mapping>改成大写的
      

  7.   

    Student.hbm.xml配置文件中class标签的name属性应该是Student,你写成student了
      

  8.   


    com/lv/hibernate/model/student (wrong name: com/lv/hibernate/model/Student
    看来楼主要多学学英文了啊、Student.hbm.xml他找不到这个类
    <class name="student">
    类名都是开头大写的、上面都已经回答的很仔细了。
    程序员得仔细些心细