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: Generator. Increment&assigned at the same time
PostPosted: Mon Sep 11, 2006 6:21 am 
Newbie

Joined: Tue Jun 27, 2006 9:34 am
Posts: 9
Location: St. Petersburg
Hi, there!
Here is my problem:
I need to use 2 classes of generator at the same time: increment and assigned.
What for:
In usual work in adding items to the table increment generator is used. It's normal.

But I need to move old data to new database. I must feel the table with old IDs.
method setId() doesn't work if I use increment generator. If I use assigned generator I must use setId() always.

Can I use two at the same time, or i must find another solvation?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 11, 2006 5:24 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You can either map the class twice and distinguish between the two mappings via the entity-name attribute of the class element, or write your own generator class (see refdocs section 5.1.4.1 and elsewhere) which does something fancy like:
Code:
public class SometimesIncrementGenerator extends org.hibernate.id.IncrementGenerator
{
  @Override
  public synchronized Serializable
  generate(SessionImplementor session, Object object)
  {
    final Serializable id =
      session.getEntityPersister(entityName, obj)
             .getIdentifier(obj, session.getEntityMode());
    if (id == nulll || id == -1)
    {
      return super.generate(session, object);
    }

    return id;
  }
}
I don't know if autoboxing can handle Serializable, you may have to do something else to get the value from the id variable, probably involving instanceof and casting. That's left as an exercise for the reader :)

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 12, 2006 4:26 am 
Newbie

Joined: Tue Jun 27, 2006 9:34 am
Posts: 9
Location: St. Petersburg
I made my generator class.
Now I need to use it in mapping class.
How?
<generator class="SometimesIncrement"/> doesn't work


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 12, 2006 5:15 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You need to mention the package.

_________________
Code tags are your friend. Know them and use them.


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.