-->
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: Recovering from non-managed changes to database
PostPosted: Wed Aug 23, 2006 10:44 am 
Newbie

Joined: Wed Aug 23, 2006 10:11 am
Posts: 9
This is cross-posted from the JBoss Persistence forum. I apologize if this is un-cool - I wasn't sure how the two are related, and no one on that forum seemed to have an answer.

I have some entities that are in many-to-many, bidirectional relationships with very large numbers of other entities (millions). Deleting one of them the naive way (walking through all the related entities and remove()-ing it from their collections) is not feasible, so I used a native SQL query to remove all references from the join table. This almost works fine. Unfortunately, some small number of those millions of related entities are usually being managed, but I don't know which ones. If I knew which they were, I could call refresh() on each.

Is there some way of finding out which entities of a given type are currently being managed? Is there some better way of accomplishing this delete?

Thanks,

Joe


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 24, 2006 1:02 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Isn't entity.getCollection().clear(); fast enough? I think Hibernate implement that in a smart way

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 24, 2006 2:23 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
The one-shot delete with collection.clear() only works for collections of value types. The solution is not to map the many-to-many association but only the foreign keys to many-to-one.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 24, 2006 7:12 am 
Newbie

Joined: Wed Aug 23, 2006 10:11 am
Posts: 9
Thanks for the replies! I'm not sure I understand your recommendations, though. My classes look something like this:

Code:
class Person
{
    // stuff . . .

    @ManyToMany
    @JoinTable(name = "person_group_join",
          joinColumns = @JoinColumn(name="person_id"),
          inverseJoinColumns = @JoinColumn(name="group_id"))
    public Set<Group> getGroups() {return groups;}

    // more stuff . . .
}


class Group
{
    // stuff . . .

    @ManyToMany(fetch=FetchType.LAZY, mappedBy="groups")
    @LazyCollection(LazyCollectionOption.EXTRA)
    public Set<Person> getPersons() {return persons;}

    // more stuff . . .
}


The persons collection in Group has millions of entries. When I delete a Group, any Persons currently being managed may have that Group in their groups collection. I don't want to walk through all of them, and I don't know how to find those few.

Perhaps I just misunderstand how Hibernate works? I was under the impression that Hibernate won't clean up the other side of my relationships if I just use group.getPersons().clear(). Is that not correct?
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 24, 2006 6:51 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
you're correct, but you haven't told me that before ;-)

_________________
Emmanuel


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.