天下第一小白 2017-01-03 11:00 采纳率: 40%
浏览 1098
已采纳

hibernate 配置文件报错

图片说明
之前是create没问题,换成update就报错,但还是能执行。不过第二天数据库就报10061错误,不清楚问题到底出在哪里。下面是我的全部代码。

配置文件:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<session-factory>
    <property name="connection.username">root</property>
    <property name="connection.url">jdbc:mysql://localhost:5656/mysql</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="connection.password">123456</property>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>

    <property name="show_sql">true</property>
    <property name="format_sql">true</property>
    <property name="hbm2ddl.auto">update</property>
    <mapping resource="Students.hbm.xml"/>
</session-factory>

测试文件:

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.boot.registry.StandardServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.Date;

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

    @Before
    public void init() {
        //创建配置对象
        Configuration config = new Configuration().configure();
        //创建服务注册对象
      //  ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();
        //创建会话工厂对象
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
        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();
        s.setSname("POLY Ma");
        s.setBirthday(new Date());
        s.setAddress("Hong Kong");
        s.setGender("M");
        session.save(s); //保存对象进入数据库
    }
    @Test
    public void search(){//查找
        Students s=session.get(Students.class,1);//1 is primary key
        System.out.print(s);
    }
    @Test
    public void update(){//修改
        Students s=session.get(Students.class,2);//look on
        s.setAddress("Hang Zhou");
        session.update(s);
    }
    @Test
    public void delete(){//删除
        Students s=session.get(Students.class,1);
        session.delete(s);
    }
}

映射文件:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<class name="Students" table="students">
    <id name="sid" type="int">
        <column name="sid"/>
        <generator class="native"/>
    </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="date">
        <column name="birthday"/>
    </property>
   <property name="address" type="java.lang.String">
       <column name="address"/>
   </property>

</class>

主体:
import java.util.Date;

/**

  • Created by tcxd on 2016/12/28.
    */
    public class Students {
    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) {
    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 void setAddress(String address) {
    this.address = address;
    }
    public String getAddress() {
    return address;
    }

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

  • 写回答

2条回答 默认 最新

  • devmiao 2017-01-03 11:00
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微服务假死,一段时间后自动恢复,如何排查处理
  • ¥15 cplex运行后参数报错是为什么
  • ¥15 之前不小心删了pycharm的文件,后面重新安装之后软件打不开了
  • ¥15 vue3获取动态宽度,刷新后动态宽度值为0
  • ¥15 升腾威讯云桌面V2.0.0摄像头问题
  • ¥15 关于Python的会计设计
  • ¥15 聚类分析 设计k-均值算法分类器,对一组二维模式向量进行分类。
  • ¥15 stm32c8t6工程,使用hal库
  • ¥15 找能接spark如图片的,可议价
  • ¥15 关于#单片机#的问题,请各位专家解答!