-->
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: move objects between databases
PostPosted: Thu May 21, 2009 4:12 pm 
Newbie

Joined: Thu May 21, 2009 4:07 pm
Posts: 4
hi,
i have a requirement whereby i need to archive data from my database. this means i need to move an object and all it's dependents from one database to another. unfortunately i'm a bit stumped as to where to begin with this.

many thanks,
mike


Top
 Profile  
 
 Post subject: Re: move objects between databases
PostPosted: Mon May 25, 2009 4:16 pm 
Newbie

Joined: Thu May 21, 2009 4:07 pm
Posts: 4
after some digging, i've uncovered the ISession.Replicate method. however, it's a little more complicated. as you can't use lazy instantiate you need to create your source session factory with something like:
Code:
         NH.Cfg.Configuration config = new NH.Cfg.Configuration();
         config.Configure();

         foreach (NH.Mapping.PersistentClass c in config.ClassMappings)
             if (c.IsLazy)
                 c.IsLazy = false;
         foreach (NH.Mapping.Collection c in config.CollectionMappings)
             if (c.IsLazy)
                 c.IsLazy = false;

also, as you'll want to preserve your primary keys you'll need to create your destination session factory with something like (you might want to regenerate your schema as well):
Code:
         config = new NH.Cfg.Configuration();
         config.Configure(HttpContext.Current.Server.MapPath("~/Config/nh-replication.config"));

         foreach (NH.Mapping.PersistentClass c in config.ClassMappings)
            if (c.Identifier.IdentifierGeneratorStrategy.Equals("native"))
               c.Identifier.IdentifierGeneratorStrategy = "assigned";

         destSessionFactory = config.BuildSessionFactory();


then you can have:
Code:
         object item;

         using (NH.ISession session = sourceSessionFactory.OpenSession())
         {
//get item
         }

         using (NH.ISession session = destSessionFactory.OpenSession())
         {
            session.Replicate(item, NH.ReplicationMode.Overwrite);

            session.Flush();
         }


havn't totally tested this yet, but it's on the right track.


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.