-->
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.  [ 12 posts ] 
Author Message
 Post subject: saveOrUpdateCopy with different database?
PostPosted: Tue Jan 13, 2004 12:14 pm 
Beginner
Beginner

Joined: Tue Oct 28, 2003 12:09 pm
Posts: 46
Hello all,
I am trying to use hibernateSession.saveOrUpdateCopy to copy objects from one database to another.

Code:
            User test = (User) originalSession.load(User.class, new Long(3065));

            try {
                tx = archivalSession.beginTransaction();
                Object copy = archivalSession.saveOrUpdateCopy(test);
                tx.commit();
                archivalSession.flush();
                archivalSession.close();
            } catch (HibernateException e) {
                logger.warn(e.getMessage(), e);
                tx.rollback();
                //TODO log error to form
            }



I end up getting errors when hibernate tries to access (via reflection or cglib) the getters of the object to be copied:

Code:
java.lang.NullPointerException
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25
   at java.lang.reflect.Method.invoke(Method.java:324)
   at net.sf.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:96)
   at net.sf.hibernate.persister.AbstractEntityPersister.getPropertyValues(AbstractEntityPersister.java:242)
   at net.sf.hibernate.impl.SessionImpl.doCopy(SessionImpl.java:3826)
   at net.sf.hibernate.impl.SessionImpl.saveOrUpdateCopy(SessionImpl.java:3780)

I'm guessing that this is a mis-use of the saveOrUpdateCopy call, but it would be REALLY nice if I could archive things in this manner without having to do this in straight sql or traversing the object tree manually.

Any thoughts on why accessing the getters is failing?

Thanks,
Marcus
Code:


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 13, 2004 12:17 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Why don't you just save() or saveOrUpdate() ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 13, 2004 12:22 pm 
Beginner
Beginner

Joined: Tue Oct 28, 2003 12:09 pm
Posts: 46
Because I'm stupid?

Thanks for taking time to point out what should have been obvious.

I don't know why I jumped straight to the complicated approach.

-mp


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 13, 2004 12:48 pm 
Beginner
Beginner

Joined: Tue Oct 28, 2003 12:09 pm
Posts: 46
Hmmm....that fails in the slightly more complicated case of trying to save a persisted collections because it complains about a collection trying to be associated with two open sessions.

The collection is lazy so it's not possible to close the previous session before saving, and when I marked the collection as "not lazy" just to see, it looks like it didn't think the objects were dirty and didn't save them.

Thoughts?

-mp


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 13, 2004 1:19 pm 
Beginner
Beginner

Joined: Tue Oct 28, 2003 12:09 pm
Posts: 46
If one were to traverse an object tree using the metadata api, any ideas how to tell if a collection is inverse?

I see there is a getter to check that in net.sf.hibernate.mapping.Collection
but I don't see how to get that wrapping collectionwhen you only have the java collection, the object and the meta data.

-mp


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 13, 2004 1:28 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
mpopetz wrote:
it looks like it didn't think the objects were dirty and didn't save them.

Detach the object graph (by closing the session for example). Then, you have to update your destination mapping to allow correct save when using saveOrUpdate. Check unsaved-value attribute.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 13, 2004 1:47 pm 
Beginner
Beginner

Joined: Tue Oct 28, 2003 12:09 pm
Posts: 46
Quote:
Detach the object graph (by closing the session for example). Then, you have to update your destination mapping to allow correct save when using saveOrUpdate. Check unsaved-value attribute.


Yeah...I grok that and it would work. Any thoughts on the following though?

I'm using the same mappings for both the source database and the destination database. The destination is an EXACT copy of the source database with an admin tool to allow the client to move their data from source to destination piecemeal. (I tried to get out of this requirement...but they're convinced that it has to be done :( )

I'd rather not have to maintain a separate copy of the mappings with different unsaved-value attributes. Is there any way to change the unset-value in a mapping on the fly?

Any other thoughts on how I could manage this without duplicating the mappings? Right now I'm approaching it from a recursive copy standpoint...which is kinda ugly but I think will work in the end.

-mp


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 13, 2004 2:00 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
The recursive stuff would work if you have no cascading in your mapping.
May be a good mapping expand using would help you to maintaing a single metamodel.

You could also load the mapping in a DOM structure, change it and then add it to hibernate using configuration.addDocument().

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 13, 2004 2:12 pm 
Beginner
Beginner

Joined: Tue Oct 28, 2003 12:09 pm
Posts: 46
Quote:
May be a good mapping expand using would help you to maintaing a single metamodel.


I didn't understand that part.... I think maybe you had a typo in it?



Quote:
You could also load the mapping in a DOM structure, change it and then add it to hibernate using configuration.addDocument().


That's cool. I like that better than the deep recurse.

-mp


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 13, 2004 2:17 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Code:
<id name="myId" unsaved-value="@my.expanded.unsavedValue@"


Ant will replace @...@ by the appropriate value and generate 2 mappings while letting you to maintain only one.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 13, 2004 2:51 pm 
Beginner
Beginner

Joined: Tue Oct 28, 2003 12:09 pm
Posts: 46
Quote:
Ant will replace @...@ by the appropriate value and generate 2 mappings while letting you to maintain only one.


Ahhh....that's cool too.

Thanks for the miriad of solutions. I appreciate the help.

-mp


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 13, 2004 5:02 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
There was a posting here some time ago about using session.replicate() for something very similar to that, in the direction of replication across a cluster. Check if you find this.


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