-->
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.  [ 2 posts ] 
Author Message
 Post subject: Interceptors: Previous Collection State = Current State
PostPosted: Fri Jan 29, 2010 8:38 pm 
Beginner
Beginner

Joined: Fri Jan 29, 2010 8:17 pm
Posts: 20
Location: Portland OR
Version info:
hibernate-annotations-3.4.0.GA.jar
hibernate-core-3.3.2.GA.jar
hibernate-entitymanager-3.4.0.GA.jar

I'm currently working on a interceptor for a more condensed form of logging than envers gives me(I need a single table to rebuild what happened more efficiently and send notifications to interested listeners in other apps). However, I'm running into some trouble with removals from collections. Previous state always has the same contents as current state(its the same collection object).

Is this intended? If so the only way I see around this is to always do shallow copies of collections and their contents and cache them in order to do the dirty checking myself? But since hibernate is clearly detecting this field as dirty(updates my association table just fine), I would have expected it to do something along those lines for me.

Here is my particular property mapping in case its related.
Code:
@ManyToMany(
        cascade = { CascadeType.MERGE, CascadeType.PERSIST})
@JoinTable(
        name="vehicle_association",
        joinColumns={@JoinColumn(name="parent_id")},
        inverseJoinColumns={@JoinColumn(name="vehicle_id")}
)
@Fetch(FetchMode.SUBSELECT)
private Set<Vehicle> vehicles=new HashSet<Vehicle>();

I've tried both ways of first merging a detached object, and just straight loading of the object via entityManager.find(..), followed by removal of vehicles from the set, just in case for some unknown reason previous state could not be determined using one method or the other.

Any info on whether or not this is working as designed or not would be helpful. Or if I can be pointed to a recipe for achieving what I want that would be great too.


Last edited by pushedbytime on Fri Jan 29, 2010 11:04 pm, edited 3 times in total.

Top
 Profile  
 
 Post subject: Re: Interceptors: Previous Collection State = Current State
PostPosted: Fri Jan 29, 2010 11:02 pm 
Beginner
Beginner

Joined: Fri Jan 29, 2010 8:17 pm
Posts: 20
Location: Portland OR
This still seems like a bug to me, but I figured it out after peeking at the Envers source and seeing how it works there. If you are having the same problem and don't want to dig, here is what I did:

Code:
if(previousState != null && previousState instanceof PersistentCollection){
    PersistentCollection collection = (PersistentCollection)previousState;
    previousState = collection.getStoredSnapshot();
    if(previousState instanceof Map){//apparently stored as a map under the covers
        previousState = ((Map<?, ?>)previousState).values();
    }
}


Obviously I'm trying to just deal with it as a collection. You may have differing needs.
Still seems to me something like this, only less hacky, could have been done to provide the user with the expected 'previous' state.


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