之前是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 +
'}';
}
}