是測試CURD的地方配置sessionfactory有錯誤
下面是兩種寫法,第二種的寫法已經過時,不要缺少下面紅色字體代碼:
<1>
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Teacher t = new Teacher();
//student.setId(1);
t.setId(4);
t.setName("cc");
t.setTitle("高級");
Configuration cg = new Configuration();
cg.configure();//不要缺少或寫在下面,這個是configure和serviceregistry之間
ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(cg.getProperties()).buildServiceRegistry();
SessionFactory sf = cg.buildSessionFactory(sr);
Session s = sf.openSession();
s.beginTransaction();
s.save(t);
s.getTransaction().commit();
s.close();
sf.close();
}
}
<2>
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Student student = new Student();
//student.setId(1);
student.setName("小李");
student.setAge(32);
Configuration cg = new Configuration();
SessionFactory sf = cg.configure().buildSessionFactory();
Session s = sf.openSession();
s.beginTransaction();
s.save(student);
s.getTransaction().commit();
s.close();
sf.close();
}
}