-->
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.  [ 4 posts ] 
Author Message
 Post subject: mapping entity with collection, to 2 identical tables
PostPosted: Mon Feb 06, 2006 1:08 pm 
Newbie

Joined: Tue Jul 20, 2004 9:35 am
Posts: 5
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.1.2

I use entity-name to map one object into 2 tables: the "real" objects and the "archived" ones: i duplicate the hbm files, and replace some class="..." with entity-name="...".

Now i want to move a record from the real table into the archived table:
session.delete("real",o); session.save("archived",o);

Actually my object isn't just an object, it's a thing that is mapped to 3 tables (one for the object, one for a related set of objects, and another one-to-one table). So now i have 3 pairs of tables, and 3 pairs of hbm's (looking alike, but slightly different in their many-to-one an one-to-one).

Note that the object also relates to other tables (it has FK's pointing to other tables), which are not duplicated.

1) What's the best way to move a record, with all its related records, from the "real" tables, into the "archived" tables? Since all info is in the mapping files, i think hibernate should be able to do the "deep" copy...

2) What happens with o.getRelatedObjects(), when persistent object o is persisted in table 1, and is suddenly pushed in table 2 by doing save("table2",object)?

3) How can i make my object o transient, without deleting the record from the database? Hibernate.initialize(o.getRelatedObjects()), detach o (close session, and start another one), and invoke save("table2",o) in a fresh session, and cascade="all"?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 07, 2006 9:21 am 
Newbie

Joined: Tue Jul 20, 2004 9:35 am
Posts: 5
After much trial and error, i found a sequence that actually does what i need:

    1. enable cascade="replicate" (or "all") for the inner-relations between the 3 tables
    2. load("real") the object o and all its relations in memory (use Hibernate.initialize)
    3. detach this object graph: session.clear()
    4. use replicate("archived") to duplicate the object graph o
    5. after calling replicate, the persistent object o no longer represents the "real" object but the "archived" object
    6. therefore, to delete the "real" object you have to load it again by id.


In code:
Code:
moveToArchive(o)
{
  // backup id
  id = o.getId();
  // completely materialize the object  tree = load (visit) it's lazy relations
  Iterator set = o.getSomeSet().iterator();
  while (set.hasNext())
  {
    el = set.next();
    Hibernate.initialize(el);
    Hibernate.initialize(el.getSomeSubSet());
  }
  // detach all objects...
  session.clear();
  // ...because replication only works for detached objects
  session.replicate("archived", o, ReplicationMode.EXCEPTION);
  // and delete the original
  o = session.load("real", id);
  session.delete("real", o);
  // this flush is required (hibernate 3.1.2), without, it simply does not work!
  session.flush();
}


Last edited by karel1980 on Wed Feb 08, 2006 7:03 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 07, 2006 9:53 am 
Newbie

Joined: Tue Jul 20, 2004 9:35 am
Posts: 5
I'm still looking for the answers posted in my top post... about what happens to o.getSet() when i call save("archived",o)...


Top
 Profile  
 
 Post subject: Re:
PostPosted: Sat Dec 01, 2012 1:43 pm 
Newbie

Joined: Sat Dec 01, 2012 12:01 am
Posts: 3
karel1980 wrote:
I'm still looking for the answers posted in my top post... about what happens to o.getSet() when i call save("archived",o)...


Hi Karel1980: Did you got this functionality working. If Yes, Can you please help me and upload code to this post.


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