如果在休眠中发生异常,如何创建保存点以及如何使用回滚我知道它在 jdbc 中是如何工作的,但是我在休眠程序中创建保存点时遇到了困难。
我的程序是'public class StudentStoredData { public static void main(String[] args) {
String name = " ";
int count = 0;
int cond = 0;
// creating configuration object
Configuration cfg = new Configuration();
cfg.configure("Student.cfg.xml");// populates the data of the configuration file
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
Transaction t = session.beginTransaction();
try {
do {
Scanner scn = new Scanner(System.in);
System.out.println("enter 1 to insert and 0 to quit");
cond = scn.nextInt();
if (cond == 0)
break;
Student e1 = new Student();
System.out.println("Enter Id ");
int id = scn.nextInt();
e1.setId(id);
System.out.println("Enter name ");
name = scn.next();
e1.setName(name);
session.persist(e1);// persisting the object
System.out.println("successfully saved");
t.commit();
} while (cond != 0);
} catch (Exception e) {
System.out.println("error occured in saving values . rolling back the recent changes");
} finally {
session.close();
}
} } ' 他们是学生类,以 id 和 name 作为属性,setter 和 getter 方法作为持久类。我在哪里得到连接变量..我是休眠的新手。