-->
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: One IdentifierGenerator instance by SessionFactory
PostPosted: Tue Oct 04, 2005 1:59 pm 
Newbie

Joined: Mon Apr 04, 2005 5:31 am
Posts: 3
Hibernate version:3.0.5

I need to implement a IdentifierGenerator that must be one Singleton by SessionFactory.
Anyone can explain the better way to do this ?

Thanks on advance
--
fmg


Top
 Profile  
 
 Post subject: Re: One IdentifierGenerator instance by SessionFactory
PostPosted: Mon Oct 10, 2005 12:51 am 
Newbie

Joined: Thu Oct 06, 2005 7:23 pm
Posts: 7
fgamito wrote:
Hibernate version:3.0.5

I need to implement a IdentifierGenerator that must be one Singleton by SessionFactory.
Anyone can explain the better way to do this ?

fmg


Assuming the goal is that you simply want to generate ids from the same pool and not that you really need a singleton (you don't have control over this since the framework is responsible for creating identifier generators), something like the following should do the trick.
Note that this is overkill for a primitive integer (you can reduce the code to literally 5 lines, but this should work if you've got a fancier generation algorithm requiring objects.

Code:
public class SharedGenerator implements IdentifierGenerator {
   private static SharedGenerator singleton;
   private static long id = 0;
   
   public SharedGenerator() {
      // Generators are created during mapping, so there should be no need for synchronization
      if (singleton == null) {
         singleton = this;
      }
   }

   public Serializable generate(SessionImplementor session, Object object)
         throws HibernateException {
      if (this == singleton) {
         synchronized (singleton) {
            return new Long(++id);
         }
      } else {
         return singleton.generate(session, object);
      }
   }
}

_________________
Please remember to rate replies if they're helpful


Top
 Profile  
 
 Post subject: Re: One IdentifierGenerator instance by SessionFactory
PostPosted: Mon Oct 10, 2005 8:43 am 
Newbie

Joined: Thu Oct 06, 2005 7:23 pm
Posts: 7
Oops - that should read:
Code:
synchronized (singleton) {
   id += 1;
   return new Long(id);
}

_________________
Please remember to rate replies if they're helpful


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.