-->
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.  [ 7 posts ] 
Author Message
 Post subject: Reconstructing original object from Interceptor's flushDirty
PostPosted: Mon Feb 16, 2004 12:09 pm 
Beginner
Beginner

Joined: Thu Dec 11, 2003 11:59 am
Posts: 24
I have an Interceptor which has a flushDirty() which calls a static method in another class that does work based on old/new values of the object. Ideally, the code that I'm calling should not know anything about Hibernate or the names of the properties mentioned in the mapping files. However, flushDirty() receives as arguments: the current state of the object, a list of old values, a list of new values, and a list of property names. I can't pass the list of old values to what I'm calling, because it's mapping-dependent.

I have come up with 2 possible solutions:
1) I find some way to reconstuct the old object from the list of properties and then call myMethod(oldObject, newObject). I suspect that creating the new object will cause problems in the session, because I really don't want the object to be connected. It's just there to refer to the original state. Also, is there a way to set the properties of persisted objects directly? Something like setProperty(object, property, value)?

2) I put a method in my object that returns a DTO that describes what has been modified and I call myMethod(object). Then, myMethod will get the DTO from the object and work on that. For this to work, I need to have a way to get the modified attributes for an arbitrary object, as well as their old and new values (I know how to get the last one :) ).

Any help would be appreciated. I'm leaning towards the 2nd solution, but any solution that works is infinitely better than one I have now.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 12:13 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Well I don't really get why passing an object containing the values would be more "mapping dependant" than passing the arrays, however ...

For 1, you can use Reflection or if you want it more easily, something like Jakarta Beanutils. The second approach sounds really overcomplicated to me, in fact you do have every Information you need in the flushDirty method, don't you?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 12:26 pm 
Beginner
Beginner

Joined: Thu Dec 11, 2003 11:59 am
Posts: 24
gloeglm wrote:
Well I don't really get why passing an object containing the values would be more "mapping dependant" than passing the arrays, however ...


Well I have some access="field" properties that I don't want to expose to anyone. i.e. The object exposes a slightly different API than that seen by Hibernate. I want the class that I'm calling to deal with something that looks like the object, not the properties mentioned in the mapping files.

gloeglm wrote:
For 1, you can use Reflection or if you want it more easily, something like Jakarta Beanutils. The second approach sounds really overcomplicated to me, in fact you do have every Information you need in the flushDirty method, don't you?


Reflection would only work for properties that are access="property", right?

As you say, the second option may be a a bit complicated. However, it should be a straighforward thing to ask an object if it has been modified, what has been modified, and how it was modified (old/new value). These are common questions to ask of an object, so I'm sure that the answers can be provided by Hibernate. I just don't know how. :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 5:15 pm 
Beginner
Beginner

Joined: Thu Dec 11, 2003 11:59 am
Posts: 24
Is it safe for me to poke around in EntityEntry to get the info that I need?

Code:
EntityEntry entiryEntry = ((SessionImplementor) session).getEntity(key);


Then, use entityEntry.loadedState and entityEntry.deletedState.

I'd prefer not to have to poke this deeply if at all possible.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2004 3:36 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
PatrickBradley wrote:
Reflection would only work for properties that are access="property", right?

Hibernate use cglib/reflexion to access fields.
PatrickBradley wrote:
Is it safe for me to poke around in EntityEntry to get the info that I need?

No it's not safe and may change (especially in the 2.2 branch).

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2004 11:20 am 
Beginner
Beginner

Joined: Thu Dec 11, 2003 11:59 am
Posts: 24
emmanuel wrote:
PatrickBradley wrote:
Is it safe for me to poke around in EntityEntry to get the info that I need?
No it's not safe and may change (especially in the 2.2 branch).


What do you people think about adding the ability to get modification info about an object? It could be something as simple as:
Code:
session.getModifiedProperties(object)
session.getLoadedValue(object, property);
session.isModified(object);


Basically, I'm asking if it was a design decision to exclude this functionality or if it's just something that hasn't been added yet. If it's the latter, I'll add it to JIRA.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2004 1:03 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Yes, there are good reasons for not exposing this.

There are some borderline cases where semantics of isModified() would be doubtful.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.