-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Setting a Property in an Interceptor
PostPosted: Wed Aug 04, 2004 8:29 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
With an interceptor I'm trying to set a property of a persistent class, just before it's save or updated. Unfortu-nately the property I'm setting in the interceptor isn't written to the DB.

My Code looks as following:

Code:
    public boolean onFlushDirty(Object entity, Serializable id, Object[] state,
            Object[] arg3, String[] arg4, Type[] arg5) throws CallbackException {
        if (entity instanceof ClassB) {
            CapiPK pk = (CapiPK)id;
            ClassB b = (ClassB)entity;
            b.setApid(pk.getPid());
            return true;
        }
        return false;
    }

    public boolean onSave(Object entity, Serializable id, Object[] state,
            String[] propertyNames, Type[] types) throws CallbackException {
        if (entity instanceof ClassB) {
            CapiPK pk = (CapiPK)id;
            ClassB b = (ClassB)entity;
            b.setApid(pk.getPid());
            return true;
        }
        return false;
    }



As you can see I want to set the property Apid of the ClassB every time a ClassB gets saved or updated. By debugging the interceptor, I can see, that the code inside onSave/onFlushDirty works fine, but the Apid Column is never written to the DB.

I tried to achieve the same with the Livecycle interface. This does exactly what I want to have, but I don't like the idea of implementing Lifecycle in all my persistent classes very much.

Any ideas about what could be wrong or how to solve this kind of problems in an other nice way (without implementing Lifecycle) would be appreciated.

BTW:
Hibernate Version is 2.1.4
DB is MySQL 4.0.20a

Best Regards
Ernst


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 04, 2004 12:22 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Yeah, don't return true.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 04, 2004 3:21 pm 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
Mmm...

The JavaDoc says for onSave:
Returns: true if the user modified the state in any way.

What is this state thing and what does it mean to change it? From steve's answer I learned that changing the state has nothing to do with changing the entity, right?

Best Regards
Ernst


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 04, 2004 4:36 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Reagrding the "state" in "the user modified the state in any way", check out the parameter list to that method.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 05, 2004 7:31 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
Hi again

Returning false didn't solve the problem.

The point is, that changes to the entity are not written to the DB. Only changes to the state array are written to the DB. To be more precise: If entity and the state array are referencing the same value and somebody changes this value through the entity, the state is written to the DB, in my case setting a new reference to the Apid property of the entity was useless, because the state did-n't know anything about this reference.


After debugging the onSave call several times I did understand, that the state contains the values of the property names. IMHO it would help a lot to rename the variables from state to propertyValues to make the Java-Doc/Code more understandable.
Maybe adding a note, that changes to the entity are not necessarily written to the DB would help as well.

Best Regards
Ernst


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.