hibernate拦截器:afterTransactionCompletion [英] hibernate interceptors : afterTransactionCompletion

查看:148
本文介绍了hibernate拦截器:afterTransactionCompletion的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个Hibernate拦截器:

I wrote a Hibernate interceptor :

public class MyInterceptor extends EmptyInterceptor {

private boolean isCanal=false;

public boolean onSave(Object entity, Serializable arg1, Object[] arg2, String[] arg3, Type[] arg4) throws CallbackException {

    for(int i=0;i<100;i++){
        System.out.println("Inside MyInterceptor(onSave) : "+entity.toString());
    }
    if(entity instanceof Canal){
        isCanal=true;
    }
    return false;
}

public void afterTransactionCompletion(Transaction tx){
    if(tx.wasCommitted()&&(isCanal)){
        for(int i=0;i<100;i++){
            System.out.println("Inside MyInterceptor(afterTransactionCompletion) : Canal was saved to DB.");
        }
    }
}

我可以看到onSave执行正常,但afterTransactionCompletion方法永远不会执行,即使事务成功提交到数据库。

I can see the method onSave executing fine, but afterTransactionCompletion method never gets executed even though the transaction is successfully commited to the database.

我需要一种方法来跟踪运算对象每次成功保存到数据库并通过打印一些消息来作出反应。这是可行的吗?以及如何?

I need a way to track every time a Canal object is successfully saved to the DB and react by printing some messages. is that feasible ? and how ?

以下是我用来将对象保存在DB中的方法:

Here is the method I use to save objects in the DB :

public static Object enregObjet(Object obj) throws UpdateException,
        EnregException, ErreurException {

Transaction tx = null;
    try {
        Session s = InterfaceBD.currentSession();
        tx = s.beginTransaction();
        try {
            // Positionner les champs dteUti et dteUtiModif
            Method dteUtiSetter = null;
            ;
            // Objet en insertion
            dteUtiSetter = obj.getClass().getMethod("setDteUti",
                    new Class[] { java.util.Date.class });
            dteUtiSetter.invoke(obj, new Object[] { new java.util.Date() });
        } catch (NoSuchMethodException ex) {
            ;// Le champ dteUtiModif n'existe pas
        }
        // Enregistrer
        IardNum.numeroterCode(obj);
        IardNum.numeroterId(obj);
        s.save(obj);
        s.flush();
        tx.commit();
        try {
            String id = "";
            // Positionner les champs dteUti et dteUtiModif
            Method idGetter = null;
            // Objet en insertion
            idGetter = obj.getClass().getMethod("getId");
            id = (String) idGetter.invoke(obj);
            Connection conn = InterfaceBD.getConn();
            IardGenerator3.cleanSeq(id, conn);
            conn.close();
        } catch (NoSuchMethodException ex) {
            ;// Le champ dteUtiModif n'existe pas
        }
        catch(ClassCastException ex){
            ;//just ignore it because we are dealing with a PK class (e.g : CausesAnnexesSinistrePK).
        }
        s.clear();
        return obj;
}


推荐答案

我会建议使用DAO设计模板。基本上你只需创建一个类,它将保存Canal对象并在需要保存的地方使用它。
在这个类中,您可以添加所需的所有逻辑。

I would suggest using DAO design template. Basicaly you just create an class, which will save Canal object and use it everywhere where you need to save it. In this class you can add all logic you need.

这篇关于hibernate拦截器:afterTransactionCompletion的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆