调用hibernate API
计应134 郭宏钰
public class Test {
public static void main(String[] args) {
Account act = null;
//step1: 创建Configration对象
Configuration config = new Configuration().configure();
//step2: 获取SessionFactorye
SessionFactory sf = config.buildSessionFactory();
//step3:获取Session和Transaction,进行持久化操作
Session s = null;
Transaction tran = null;
try {
s = sf.openSession();
tran = s.beginTransaction();//启动事务
act = new Account(12345, 2000.00);
s.save(act);//将帐户对象持久化
tran.commit();//提交事务
} catch (Exception e) {
if(tran != null){
tran.rollback();
e.printStackTrace();
} finally{
if(s!=null) s.close();
}
}
}
浙公网安备 33010602011771号