-->
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: Using sequences in Hibernate 5.0
PostPosted: Thu May 12, 2016 10:18 am 
Newbie

Joined: Thu May 12, 2016 9:57 am
Posts: 2
I am trying to migrate my code from Hibernate 4.3 to 5.0. In Hibernate 4.3 I used a SequenceStyleGenerator to get numbers from a sequence with the following code:

Code:
   fGenerator = new SequenceStyleGenerator();
      Properties params = new Properties();
      params.setProperty(SequenceStyleGenerator.SEQUENCE_PARAM, sequenceName);
      params.setProperty(SequenceStyleGenerator.OPT_PARAM, StandardOptimizerDescriptor.POOLED.getExternalName());
      params.put( PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, new ObjectNameNormalizer() {

         @Override
         protected boolean isUseQuotedIdentifiersGlobally() {
            return false;
         }

         @Override
         protected NamingStrategyDelegator getNamingStrategyDelegator() {
            return ImprovedNamingStrategyDelegator.DEFAULT_INSTANCE;
         }

         @Override
         protected NamingStrategy getNamingStrategy() {
            return DefaultNamingStrategy.INSTANCE;
         }

      });

      fGenerator.configure(StandardBasicTypes.LONG, params, dialect);


In hibernate 5.0 both the ObjectNameNormalizer and the configure method have changed. I tried the following

Code:
   fGenerator = new SequenceStyleGenerator();
      Properties params = new Properties();
      params.setProperty(SequenceStyleGenerator.SEQUENCE_PARAM, sequenceName);
      params.setProperty(SequenceStyleGenerator.OPT_PARAM, StandardOptimizerDescriptor.POOLED.getExternalName());
      params.put( PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, new ObjectNameNormalizer() {

         @Override
         protected MetadataBuildingContext getBuildingContext() {
            return null;
         }
         
      });

      fGenerator.configure(StandardBasicTypes.LONG, params, registry);


This does not work. I found out that in order make the generator work the method registerExportables(Database database) must be called. But I don't know where I can get the correct object of type Database from. I should somehow get it from the configuration but I don't know how. Or do I have to register my sequence object so that the registerExportables method is called for me?
I also don't know what kind of ObjectNameNormalizer I should use or what MetadataBuildingContext I should return.
Does anyone know how to use sequences without using entities or JPA?


Top
 Profile  
 
 Post subject: Re: Using sequences in Hibernate 5.0
PostPosted: Mon May 16, 2016 7:50 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
I guess that you are bootstrapping Hibernate programmatically, so you already have access to the StandardServiceRegistry. In that case, you should change your code to:

Code:
StandardServiceRegistry serviceRegistry = ...;

properties.put(
        PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER,
        new ObjectNameNormalizer() {
            @Override
            protected MetadataBuildingContext getBuildingContext() {
                return new MetadataBuildingContextTestingImpl(serviceRegistry);
            }
        }
);


Top
 Profile  
 
 Post subject: Re: Using sequences in Hibernate 5.0
PostPosted: Mon May 23, 2016 8:55 am 
Newbie

Joined: Thu May 12, 2016 9:57 am
Posts: 2
Using the MetadataBuildingContextTestingImpl class alone did not work. This is the code that finally worked:

Code:
   public MySequenceImpl(String sequenceName, final StandardServiceRegistry registry) {
        fGenerator = new SequenceStyleGenerator();
        Properties params = new Properties();
        params.setProperty(SequenceStyleGenerator.SEQUENCE_PARAM, sequenceName);
        params.setProperty(SequenceStyleGenerator.OPT_PARAM, StandardOptimizerDescriptor.POOLED.getExternalName());
        final MetadataBuildingContext context = new MetadataBuildingContextTestingImpl(registry);
        params.put( PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, new ObjectNameNormalizer() {

         @Override
         protected MetadataBuildingContext getBuildingContext() {
                        return context;
         }
         
      });

      fGenerator.configure(StandardBasicTypes.LONG, params, registry);
      fGenerator.registerExportables(context.getMetadataCollector().getDatabase());
   }


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.