之前是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 +
'}';
}
}
http://blog.csdn.net/z28126308/article/details/52914490