English 中文(简体)
hibernate interceptors : afterTransactionCompletion
原标题:

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.");
        }
    }
}

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 ?

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;
}
问题回答

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.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签