Created
          October 12, 2010 06:14 
        
      - 
      
- 
        Save anonymous/621743 to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | package hibernate; | |
| import org.hibernate.dialect.PostgreSQLDialect; | |
| public class PostgreSQLSerialDialect extends PostgreSQLDialect { | |
| public Class getNativeIdentifierGeneratorClass() { | |
| return SerialIdentifierGenerator.class; | |
| } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | package hibernate; | |
| import java.util.Properties; | |
| import org.hibernate.MappingException; | |
| import org.hibernate.cfg.DefaultNamingStrategy; | |
| import org.hibernate.cfg.NamingStrategy; | |
| import org.hibernate.cfg.ObjectNameNormalizer; | |
| import org.hibernate.dialect.Dialect; | |
| import org.hibernate.id.SequenceGenerator; | |
| import org.hibernate.mapping.Table; | |
| import org.hibernate.type.Type; | |
| public class SerialIdentifierGenerator extends SequenceGenerator { | |
| public void configure(Type type, Properties params, Dialect dialect) throws MappingException { | |
| String schemaName = params.getProperty(SCHEMA); | |
| String catalogName = params.getProperty(CATALOG); | |
| String tableName = params.getProperty(TABLE); | |
| String pkName = params.getProperty(PK); | |
| String sequenceName = tableName + "_" + pkName + "_seq"; | |
| sequenceName = Table.qualify(catalogName, schemaName, sequenceName); | |
| params.setProperty(SEQUENCE, sequenceName); | |
| params.put(IDENTIFIER_NORMALIZER, new DefaultObjectNameNormalizer()); | |
| super.configure(type, params, dialect); | |
| } | |
| private static class DefaultObjectNameNormalizer extends ObjectNameNormalizer { | |
| @Override | |
| protected NamingStrategy getNamingStrategy() { | |
| return new DefaultNamingStrategy(); | |
| } | |
| @Override | |
| protected boolean isUseQuotedIdentifiersGlobally() { | |
| return false; | |
| } | |
| } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | package hibernate; | |
| import static org.hibernate.id.PersistentIdentifierGenerator.PK; | |
| import static org.hibernate.id.PersistentIdentifierGenerator.SCHEMA; | |
| import static org.hibernate.id.PersistentIdentifierGenerator.TABLE; | |
| import java.util.Properties; | |
| import org.hibernate.dialect.Dialect; | |
| import org.hibernate.dialect.PostgreSQLDialect; | |
| import org.hibernate.type.LongType; | |
| import org.junit.After; | |
| import org.junit.Before; | |
| import org.junit.Test; | |
| import play.test.UnitTest; | |
| public class SerialIdentifierGeneratorTest extends UnitTest { | |
| SerialIdentifierGenerator generator; | |
| Dialect dialect; | |
| @Before | |
| public void initGenerator() { | |
| Properties properties = new Properties(); | |
| properties.setProperty(SCHEMA, "schema"); | |
| properties.setProperty(TABLE, "mytable"); | |
| properties.setProperty(PK, "id"); | |
| this.generator = new SerialIdentifierGenerator(); | |
| this.dialect = new PostgreSQLDialect(); | |
| try { | |
| this.generator.configure(new LongType(), properties, this.dialect); | |
| } catch (Exception ex) { | |
| ex.printStackTrace(); | |
| } | |
| } | |
| @Test | |
| public void verifyIfGeneratorIsConfigured() { | |
| String expectedSequenceName = "schema.mytable_id_seq"; | |
| String sequenceName = this.generator.getSequenceName(); | |
| assertEquals(expectedSequenceName, sequenceName); | |
| assertEquals(expectedSequenceName, this.generator.generatorKey()); | |
| assertEquals("create sequence " + expectedSequenceName, this.generator.sqlCreateStrings(this.dialect)[0]); | |
| assertEquals("drop sequence " + expectedSequenceName, this.generator.sqlDropStrings(this.dialect)[0]); | |
| } | |
| @After | |
| public void destroyGenerator() { | |
| this.generator = null; | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment