public boolean save(TpTitempRel transientInstance) {
boolean returnValue=true;
Transaction tr = null;
try {
tr = getSession().beginTransaction();
getSession().save(transientInstance);
tr.commit();
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
tr.rollback();
returnValue=false;
throw re;
}finally{
this.closeSession();
}
return returnValue;
}
public void delete(String procode) {
log.debug("deleting TpTitempRel instance");
Session session = this.getSession();
try {
String hql ="delete from T_Titemp_Rel where product_code='"+procode+"'";
session.createSQLQuery(hql).executeUpdate();
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
在Service调用:
if (xx.save(ttr)) {
//........
}
如果不抛异常,save()返回true,就继续执行,否则,会抛异常,不会执行if
----------------------------------------------------------------------------------------------------
public class Test {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
System.out.println(Test.test());
}
public static boolean test(){
boolean flag = true;
System.out.println("Hello World!!!");
try{
//System.out.println(1/0); //第一种情况
System.out.println(1/1); //第二种情况
flag = false;
}catch(ArithmeticException e){
System.out.println("除数为0!");
throw e;
}
return flag;
}
}
执行结果:
//第一种情况
Hello World!!!
Exception in thread "main" java.lang.ArithmeticException: / by zero
at test.Test.test(Test.java:46)
at test.Test.main(Test.java:37)
除数为0!
注意:有异常的时候,就不会走后面的return语句啦!
//第二种情况
Hello World!!!
1
false