-->
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.  [ 3 posts ] 
Author Message
 Post subject: Deprecated session.save(Object, Serializable)
PostPosted: Thu May 18, 2006 4:15 pm 
Newbie

Joined: Thu May 18, 2006 2:49 pm
Posts: 5
Hi everybody,

I've been struggling with the same problems for a few days now and still can't find a solid solution. I just upgraded to Hibernate 3.1.3 from 2.1 and when going though the code realized that session.save(Object, Serializable) is deprecated now. I am in a pretty complicated situation now, as I relied on this method for restoring key persistent objects. For these objects I must preserve Hibernate IDs, as they are used to generate URLs through which these objects are referenced externally and the requirement is to keep these ids. Normally all news objects are saved with regular session.save calls, however it's only at backup and restore phases when the ids of some objects must be dealt with very carefully (i.e. preserved).

These two posts seem to address the same issue:

http://forum.hibernate.org/viewtopic.php?t=951515
http://forum.hibernate.org/viewtopic.php?t=959631

I tried to replace the call to session.save(Object, Serializable) with call to session.replicate(Object) where the parameter passed to the call has the id set, but it introduced 2 problems:

1) Ids didn't get preserved, but rather were replaced by newly generated ones
2) For certain objects "org.hibernate.TransientObjectException: instance with null id passed to replicate()" started to show up, which wasn't there when I used session.save(Object, Serializable)

(1) is a very strange observation, as JavaDocs for replicate say that it should reuse the current id value. (2) is something to expect, as I guess the whole network of the objects that get saved must have their ids set in order to be "savable". To fix that I manually persisted parts of the network first, and only then saved the larger object.

Altogether, there is nothing that stops me from using the classic session, where the save method that I am interested in is present. It's just I don't have the right feeling about having to use deprecated method for such basic operation as saving object with a certain id.

Is there a workaround to use the functionality from session.save(Object, Serializable) w.o. having to use deprecated methods?

Thank you very much for taking the time to read this post. I hope to see a reply here soon.

Sincerely,
Nick.

Hibernate version: 3.1.3

Name and version of the database you are using: MySQL 5.0


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 18, 2006 6:09 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
Why do not you use simple save( Object )?
If you cannot use 'assigned' generator you can create own generator that will not assign values if ID is already present.

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 19, 2006 3:59 pm 
Newbie

Joined: Thu May 18, 2006 2:49 pm
Posts: 5
Hi Konstantin,

Thank you very much for your suggestion. It solves all my problems with using the deprecated methods. However, now I am faced with another one - creating the generator itself.

I attempted to subclass IdentityGenerator and return object id in the same manner as it's done in Assigned generator. This works just fine for persisting objects with assigned ids, but fails for objects where ids must be generated automatically (i.e. forwared to IdentityGenerator).

I would greately appreciate if you or someone else can point out what I am doing wrong (code is attached below). Sorry, I am a really newbie in developing for Hibernate, so I just got lost in the source code for it.

Thank you very much for taking the time to read and go through this all with me.

Sincerely,
Nick.

Code:
public class AssignableIdentityGenerator extends IdentityGenerator implements Configurable {

    private String entityName;
   
    public Serializable generate(SessionImplementor session,
      Object object) {
        Serializable id = session.getEntityPersister(entityName, object)
            .getIdentifier(object, session.getEntityMode());       
        if (id == null) id = super.generate(session, object);
        return id;
    }
   
    public void configure(Type type, Properties params, Dialect d)
            throws MappingException {
        entityName = params.getProperty(ENTITY_NAME);
        if (entityName == null) {
            throw new MappingException("no entity name");
        }
    }
}


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