本文介绍了在休眠中创建保存点和回滚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建一个保存点以及如何在hibernate中发生异常时使用回滚我知道它是如何在jdbc中工作的,但我被困在Hibernate程序中创建保存点。



我的程序是'public class StudentStoredData {
public static void main(String [] args){

  String name =; 
int count = 0;
int cond = 0;
//创建配置对象
配置cfg = new Configuration();
cfg.configure(Student.cfg.xml); //填充配置文件的数据


SessionFactory factory = cfg.buildSessionFactory();
会话会话= factory.openSession();
Transaction t = session.beginTransaction();

尝试{
do {
Scanner scn = new Scanner(System.in);
System.out.println(输入1插入,0退出);
cond = scn.nextInt();
if(cond == 0)
break;
学生e1 =新生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); //持久化对象
System.out.println(saved saved);

t.commit();

} while(cond!= 0);
} catch(Exception e){
System.out.println(保存值发生错误,回滚最近的更改);
} finally {
session.close();
}

}
}
'
是学生类,id和name作为属性,setter和getter方法作为持久类。我在哪里获得连接变量..我是新的冬眠。

解决方案

为实现保存点,您需要实现工作界面。在执行过程中,您可以执行自定义任务,例如更新数据库等。

  Work work = new Work(){
public void execute(Connection arg0)throws SQLException {
// custom task
}
};

通话

如果有任何缩写,请致电


How to create a savepoint and how to use rollback if exception occurs in hibernate I know how it works in jdbc but i got stuck creating savepoint in hibernate program.

My program is '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();
}

}}'Their is student class with id and name as attribute and setter and getter method as persistent class. Where do i get the connection variable .. I am new to hibernate .

解决方案

For implementing savepoint, you need to implement Work interface. In the implementation , you would do your custom tasks like updating database etc.

Work work = new Work() {
    public void execute(Connection arg0) throws SQLException {
             //custom task
    }
};

The call

And if there is any exxception just call

这篇关于在休眠中创建保存点和回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 13:39