大神帮忙!!!找不到students表,ERROR: Table 'hibernate.students' doesn't exist

来源:1-11 通过Hibernate API编写访问数据库的代码

沉寂的蜗牛

2016-04-03 18:15

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>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql:///hibernate?userUnicode=true&amp;characterEncoding=UTF-8</property>       
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>       
        <property name="hbm2ddl.auto">create</property>
        <mapping resource="Students.hbm.xml"/>
    </session-factory>
</hibernate-configuration>


Students.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">
<!-- Generated 2016-4-3 15:32:14 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="Students" table="STUDENTS">
        <id name="sid" type="int">
            <column name="SID" />
            <generator class="assigned" />
        </id>
        <property name="sname" type="java.lang.String">
            <column name="SNAME" />
        </property>
        <property name="gender" type="java.lang.String">
            <column name="GENDER" />
        </property>
        <property name="birthday" type="java.util.Date">
            <column name="BIRTHDAY" />
        </property>
        <property name="address" type="java.lang.String">
            <column name="ADDRESS" />
        </property>
    </class>
</hibernate-mapping>


Students.java


import java.util.Date;

//学生类
public class Students {
    // 1.共有的类
    // 2.提供共有的不带参数的默认的构造方法
    // 3.属性私有
    // 4.属性getter/setter方法

    private int sid;
    private String sname;
    private String gender;
    private Date birthday;
    private String address;

    public Students() {

    }

    public Students(int sid, String sname, String gender, Date birthday, String address) {
//        super();
        this.sid = sid;
        this.sname = sname;
        this.gender = gender;
        this.birthday = birthday;
        this.address = address;
    }

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

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

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Students [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", birthday=" + birthday
                + ", address=" + address + "]";
    }
    
    
}


StudentsTest.java

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class StudentsTest {
    
    private SessionFactory sessionFactory;
    private Session session;
    private Transaction transaction;

    @Before
    public void init(){
        //创建配置对象
        Configuration config=new Configuration().configure();
        //创建服务注册对象
        ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
        //创建会话工厂对象
        sessionFactory=config.buildSessionFactory(serviceRegistry);
        //会话对象
        session=sessionFactory.openSession();
        //开启事物
        transaction=session.beginTransaction();
        
    }
    
    @After
    public void destory(){
        transaction.commit();
        session.close();
        sessionFactory.close();
        
    }
    
    @Test
    public void testSaveStudents(){
        Students s=new Students(1,"张三丰","男",new Date(),"武当山");
        session.save(s);
    }
}


写回答 关注

3回答

  • qq_无形的浪子_0
    2016-04-03 22:12:39
    已采纳

    你是自动生成的hbm.xml吗

    沉寂的蜗牛

    谢谢啦。。。是eclipse抽风了吧,隔了两天试试又有用了

    2016-04-06 09:04:15

    共 2 条回复 >

  • SayYouLoveMe
    2016-04-08 01:11:48

    <mapping resource="Students.hbm.xml"/>

    你如果没把Students.hbm.xml放在src下就必须要加上他的跟路径eg:

    <mapping  resource="com/entity/Student.hbm.xml"/>

    我的就是放在com.entity的,找不到是因为你映射路径对不上!

    沉寂的蜗牛

    哦,谢谢。

    2016-04-08 08:22:39

    共 1 条回复 >

  • 沉寂的蜗牛
    2016-04-06 08:46:54

    是的,可能是什么问题,会不会是Hibernatetools安装出问题了?

Hibernate初探之单表映射

Java持久化框架Hibernate入门教程,掌握Hibernate基本概念

74810 学习 · 793 问题

查看课程

相似问题