/** * Constructs a new DefaultIdentifierGeneratorFactory. */ public DefaultIdentifierGeneratorFactory() { register( "uuid2", UUIDGenerator.class ); register( "guid", GUIDGenerator.class ); // can be done with UUIDGenerator + strategy register( "uuid", UUIDHexGenerator.class ); // "deprecated" for new use register( "uuid.hex", UUIDHexGenerator.class ); // uuid.hex is deprecated register( "hilo", TableHiLoGenerator.class ); register( "assigned", Assigned.class ); register( "identity", IdentityGenerator.class ); register( "select", SelectGenerator.class ); register( "sequence", SequenceGenerator.class ); register( "seqhilo", SequenceHiLoGenerator.class ); register( "increment", IncrementGenerator.class ); register( "foreign", ForeignGenerator.class ); register( "sequence-identity", SequenceIdentityGenerator.class ); register( "enhanced-sequence", SequenceStyleGenerator.class ); register( "enhanced-table", TableGenerator.class ); }
@Override public void configure( Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException { final JdbcEnvironment jdbcEnvironment = serviceRegistry.getService(JdbcEnvironment.class); final Dialect dialect = jdbcEnvironment.getDialect(); final ConfigurationService configurationService = serviceRegistry.getService(ConfigurationService.class); String globalEntityIdentifierPrefix = configurationService.getSetting( "entity.identifier.prefix", String.class, "SEQ_" ); sequencePrefix = ConfigurationHelper.getString( SEQUENCE_PREFIX, params, globalEntityIdentifierPrefix); final String sequencePerEntitySuffix = ConfigurationHelper.getString( SequenceStyleGenerator.CONFIG_SEQUENCE_PER_ENTITY_SUFFIX, params, SequenceStyleGenerator.DEF_SEQUENCE_SUFFIX); final String defaultSequenceName = ConfigurationHelper.getBoolean( SequenceStyleGenerator.CONFIG_PREFER_SEQUENCE_PER_ENTITY, params, false) ? params.getProperty(JPA_ENTITY_NAME) + sequencePerEntitySuffix : SequenceStyleGenerator.DEF_SEQUENCE_NAME; sequenceCallSyntax = dialect.getSequenceNextValString( ConfigurationHelper.getString( SequenceStyleGenerator.SEQUENCE_PARAM, params, defaultSequenceName)); }
private void manageSequenceGenerator(Mappings mappings, Table tab, SimpleValue id, SequenceGenerator generator) { id.setIdentifierGeneratorStrategy ("enhanced-sequence"); Properties params = new Properties(); params.put(PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, mappings.getObjectNameNormalizer()); params.put(SequenceStyleGenerator.SEQUENCE_PARAM, escapeName(generator.getName())); params.setProperty( SequenceStyleGenerator.SCHEMA, escapeName(tab.getSchema())); id.setIdentifierGeneratorProperties(params); id.setNullValue(null); }
private static String generatorType(GenerationType generatorEnum, Mappings mappings) { boolean useNewGeneratorMappings = mappings.useNewGeneratorMappings(); switch ( generatorEnum ) { case IDENTITY: return "identity"; case AUTO: return useNewGeneratorMappings ? org.hibernate.id.enhanced.SequenceStyleGenerator.class.getName() : "native"; case TABLE: return useNewGeneratorMappings ? org.hibernate.id.enhanced.TableGenerator.class.getName() : MultipleHiLoPerTableGenerator.class.getName(); case SEQUENCE: return useNewGeneratorMappings ? org.hibernate.id.enhanced.SequenceStyleGenerator.class.getName() : "seqhilo"; } throw new AssertionFailure( "Unknown GeneratorType: " + generatorEnum ); }
private static void bindSequenceGenerator(MetadataImplementor metadata, AnnotationInstance generator) { String name = JandexHelper.getValue( generator, "name", String.class ); Map<String, String> parameterMap = new HashMap<String, String>(); addStringParameter( generator, "sequenceName", parameterMap, SequenceStyleGenerator.SEQUENCE_PARAM ); boolean useNewIdentifierGenerators = metadata.getOptions().useNewIdentifierGenerators(); String strategy = EnumConversionHelper.generationTypeToGeneratorStrategyName( GenerationType.SEQUENCE, useNewIdentifierGenerators ); if ( useNewIdentifierGenerators ) { addStringParameter( generator, "catalog", parameterMap, PersistentIdentifierGenerator.CATALOG ); addStringParameter( generator, "schema", parameterMap, PersistentIdentifierGenerator.SCHEMA ); parameterMap.put( SequenceStyleGenerator.INCREMENT_PARAM, String.valueOf( JandexHelper.getValue( generator, "allocationSize", Integer.class ) ) ); parameterMap.put( SequenceStyleGenerator.INITIAL_PARAM, String.valueOf( JandexHelper.getValue( generator, "initialValue", Integer.class ) ) ); } else { if ( JandexHelper.getValue( generator, "initialValue", Integer.class ) != 1 ) { LOG.unsupportedInitialValue( AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS ); } parameterMap.put( SequenceHiLoGenerator.MAX_LO, String.valueOf( JandexHelper.getValue( generator, "allocationSize", Integer.class ) - 1 ) ); } metadata.addIdGenerator( new IdGenerator( name, strategy, parameterMap ) ); LOG.tracef( "Add sequence generator with name: %s", name ); }
/** * Simple constructor registering strategies. */ public SequenceIdentifierGeneratorStrategyProvider() { strategies.put("enhanced-sequence", OptimizedSequenceStyleGenerator.class); strategies.put(SequenceStyleGenerator.class.getName(), OptimizedSequenceStyleGenerator.class); }
/** * Check strategy configuration. */ @Test public void testFactoryConfiguration() throws InstantiationException, IllegalAccessException { Assert.assertEquals(OptimizedSequenceStyleGenerator.class, SequenceIdentifierGeneratorStrategyProvider.class.newInstance().getStrategies().get(SequenceStyleGenerator.class.getName())); }
@SuppressWarnings("unchecked") protected void bindSimpleId(PersistentProperty identifier, RootClass entity, InFlightMetadataCollector mappings, Identity mappedId, String sessionFactoryBeanName) { Mapping mapping = getMapping(identifier.getOwner()); boolean useSequence = mapping != null && mapping.isTablePerConcreteClass(); // create the id value SimpleValue id = new SimpleValue(mappings, entity.getTable()); // set identifier on entity Properties params = new Properties(); entity.setIdentifier(id); if (mappedId == null) { // configure generator strategy id.setIdentifierGeneratorStrategy(useSequence ? "sequence-identity" : "native"); } else { String generator = mappedId.getGenerator(); if("native".equals(generator) && useSequence) { generator = "sequence-identity"; } id.setIdentifierGeneratorStrategy(generator); params.putAll(mappedId.getParams()); if(params.containsKey(SEQUENCE_KEY)) { params.put(SequenceStyleGenerator.SEQUENCE_PARAM, params.getProperty(SEQUENCE_KEY)); } if ("assigned".equals(generator)) { id.setNullValue("undefined"); } } String schemaName = getSchemaName(mappings); String catalogName = getCatalogName(mappings); params.put(PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, this.metadataBuildingContext.getObjectNameNormalizer()); if (schemaName != null) { params.setProperty(PersistentIdentifierGenerator.SCHEMA, schemaName); } if (catalogName != null) { params.setProperty(PersistentIdentifierGenerator.CATALOG, catalogName); } id.setIdentifierGeneratorProperties(params); // bind value bindSimpleValue(identifier, null, id, EMPTY_PATH, mappings, sessionFactoryBeanName); // create property Property prop = new Property(); prop.setValue(id); // bind property bindProperty(identifier, prop, mappings); // set identifier property entity.setIdentifierProperty(prop); id.getTable().setIdentifierValue(id); }
protected void bindSimpleValue(PersistentProperty grailsProp, PersistentProperty parentProperty, SimpleValue simpleValue, String path, PropertyConfig propertyConfig, String sessionFactoryBeanName) { setTypeForPropertyConfig(grailsProp, simpleValue, propertyConfig); final PropertyConfig mappedForm = (PropertyConfig) grailsProp.getMapping().getMappedForm(); if (mappedForm.isDerived() && !(grailsProp instanceof TenantId)) { Formula formula = new Formula(); formula.setFormula(propertyConfig.getFormula()); simpleValue.addFormula(formula); } else { Table table = simpleValue.getTable(); boolean hasConfig = propertyConfig != null; String generator = hasConfig ? propertyConfig.getGenerator() : null; if(generator != null) { simpleValue.setIdentifierGeneratorStrategy(generator); Properties params = propertyConfig.getTypeParams(); if(params != null) { Properties generatorProps = new Properties(); generatorProps.putAll(params); if(generatorProps.containsKey(SEQUENCE_KEY)) { generatorProps.put(SequenceStyleGenerator.SEQUENCE_PARAM, generatorProps.getProperty(SEQUENCE_KEY)); } simpleValue.setIdentifierGeneratorProperties( generatorProps ); } } // Add the column definitions for this value/property. Note that // not all custom mapped properties will have column definitions, // in which case we still need to create a Hibernate column for // this value. List<?> columnDefinitions = hasConfig ? propertyConfig.getColumns() : Arrays.asList(new Object[] { null }); if (columnDefinitions.isEmpty()) { columnDefinitions = Arrays.asList(new Object[] { null }); } for (Object columnDefinition : columnDefinitions) { ColumnConfig cc = (ColumnConfig) columnDefinition; Column column = new Column(); // Check for explicitly mapped column name and SQL type. if (cc != null) { if (cc.getName() != null) { column.setName(cc.getName()); } if (cc.getSqlType() != null) { column.setSqlType(cc.getSqlType()); } } column.setValue(simpleValue); if (cc != null) { if (cc.getLength() != -1) { column.setLength(cc.getLength()); } if (cc.getPrecision() != -1) { column.setPrecision(cc.getPrecision()); } if (cc.getScale() != -1) { column.setScale(cc.getScale()); } if(!mappedForm.isUniqueWithinGroup()) { column.setUnique(cc.isUnique()); } } bindColumn(grailsProp, parentProperty, column, cc, path, table, sessionFactoryBeanName); if (table != null) { table.addColumn(column); } simpleValue.addColumn(column); } } }
@Around("execution(* org.hibernate.id.enhanced.SequenceStyleGenerator.configure(..))") public Object enableOneSequencePerEntity(final ProceedingJoinPoint pjp) throws Throwable { final Properties properties = (Properties) pjp.getArgs()[1]; properties.put(SequenceStyleGenerator.CONFIG_PREFER_SEQUENCE_PER_ENTITY, true); return pjp.proceed(); }
@Test public void testAspectWorking() { final Type type = null; final Properties params = new Properties(); final ServiceRegistry serviceRegistry = null; try { new SequenceStyleGenerator().configure(type, params, serviceRegistry); Assertions.fail("exception expected"); } catch (final NullPointerException e) { //SUPPRESS CHECKSTYLE single line //ignore } Assertions.assertThat(params.get(SequenceStyleGenerator.CONFIG_PREFER_SEQUENCE_PER_ENTITY)).isEqualTo(true); }
public void testNormalBoundary() { EntityPersister persister = sfi().getEntityPersister( Entity.class.getName() ); assertClassAssignability( SequenceStyleGenerator.class, persister.getIdentifierGenerator().getClass() ); SequenceStyleGenerator generator = ( SequenceStyleGenerator ) persister.getIdentifierGenerator(); assertClassAssignability( OptimizerFactory.PooledOptimizer.class, generator.getOptimizer().getClass() ); OptimizerFactory.PooledOptimizer optimizer = ( OptimizerFactory.PooledOptimizer ) generator.getOptimizer(); int increment = optimizer.getIncrementSize(); Entity[] entities = new Entity[ increment + 1 ]; Session s = openSession(); s.beginTransaction(); for ( int i = 0; i < increment; i++ ) { entities[i] = new Entity( "" + ( i + 1 ) ); s.save( entities[i] ); assertEquals( 2, generator.getDatabaseStructure().getTimesAccessed() ); // initialization calls seq twice assertEquals( increment + 1, optimizer.getLastSourceValue() ); // initialization calls seq twice assertEquals( i + 1, optimizer.getLastValue() ); assertEquals( increment + 1, optimizer.getLastSourceValue() ); } // now force a "clock over" entities[ increment ] = new Entity( "" + increment ); s.save( entities[ increment ] ); assertEquals( 3, generator.getDatabaseStructure().getTimesAccessed() ); // initialization (2) + clock over assertEquals( ( increment * 2 ) + 1, optimizer.getLastSourceValue() ); // initialization (2) + clock over assertEquals( increment + 1, optimizer.getLastValue() ); s.getTransaction().commit(); s.beginTransaction(); for ( int i = 0; i < entities.length; i++ ) { assertEquals( i + 1, entities[i].getId().intValue() ); s.delete( entities[i] ); } s.getTransaction().commit(); s.close(); }
public void testNormalBoundary() { EntityPersister persister = sfi().getEntityPersister( Entity.class.getName() ); assertClassAssignability( SequenceStyleGenerator.class, persister.getIdentifierGenerator().getClass() ); SequenceStyleGenerator generator = ( SequenceStyleGenerator ) persister.getIdentifierGenerator(); int count = 5; Entity[] entities = new Entity[count]; Session s = openSession(); s.beginTransaction(); for ( int i = 0; i < count; i++ ) { entities[i] = new Entity( "" + ( i + 1 ) ); s.save( entities[i] ); long expectedId = i + 1; assertEquals( expectedId, entities[i].getId().longValue() ); assertEquals( expectedId, generator.getDatabaseStructure().getTimesAccessed() ); assertEquals( expectedId, generator.getOptimizer().getLastSourceValue() ); } s.getTransaction().commit(); s.beginTransaction(); for ( int i = 0; i < count; i++ ) { assertEquals( i + 1, entities[i].getId().intValue() ); s.delete( entities[i] ); } s.getTransaction().commit(); s.close(); }
public void testNormalBoundary() { EntityPersister persister = sfi().getEntityPersister( Entity.class.getName() ); assertClassAssignability( SequenceStyleGenerator.class, persister.getIdentifierGenerator().getClass() ); SequenceStyleGenerator generator = ( SequenceStyleGenerator ) persister.getIdentifierGenerator(); assertClassAssignability( OptimizerFactory.HiLoOptimizer.class, generator.getOptimizer().getClass() ); OptimizerFactory.HiLoOptimizer optimizer = ( OptimizerFactory.HiLoOptimizer ) generator.getOptimizer(); int increment = optimizer.getIncrementSize(); Entity[] entities = new Entity[ increment + 1 ]; Session s = openSession(); s.beginTransaction(); for ( int i = 0; i < increment; i++ ) { entities[i] = new Entity( "" + ( i + 1 ) ); s.save( entities[i] ); assertEquals( 1, generator.getDatabaseStructure().getTimesAccessed() ); // initialization assertEquals( 1, optimizer.getLastSourceValue() ); // initialization assertEquals( i + 1, optimizer.getLastValue() ); assertEquals( increment + 1, optimizer.getHiValue() ); } // now force a "clock over" entities[ increment ] = new Entity( "" + increment ); s.save( entities[ increment ] ); assertEquals( 2, generator.getDatabaseStructure().getTimesAccessed() ); // initialization assertEquals( 2, optimizer.getLastSourceValue() ); // initialization assertEquals( increment + 1, optimizer.getLastValue() ); assertEquals( ( increment * 2 ) + 1, optimizer.getHiValue() ); s.getTransaction().commit(); s.beginTransaction(); for ( int i = 0; i < entities.length; i++ ) { assertEquals( i + 1, entities[i].getId().intValue() ); s.delete( entities[i] ); } s.getTransaction().commit(); s.close(); }
/** * Test all params defaulted with a dialect supporting sequences */ public void testDefaultedSequenceBackedConfiguration() { Dialect dialect = new SequenceDialect(); Properties props = new Properties(); SequenceStyleGenerator generator = new SequenceStyleGenerator(); generator.configure( Hibernate.LONG, props, dialect ); assertClassAssignability( SequenceStructure.class, generator.getDatabaseStructure().getClass() ); assertClassAssignability( OptimizerFactory.NoopOptimizer.class, generator.getOptimizer().getClass() ); assertEquals( SequenceStyleGenerator.DEF_SEQUENCE_NAME, generator.getDatabaseStructure().getName() ); }
/** * Test all params defaulted with a dialect which does not support sequences */ public void testDefaultedTableBackedConfiguration() { Dialect dialect = new TableDialect(); Properties props = new Properties(); SequenceStyleGenerator generator = new SequenceStyleGenerator(); generator.configure( Hibernate.LONG, props, dialect ); assertClassAssignability( TableStructure.class, generator.getDatabaseStructure().getClass() ); assertClassAssignability( OptimizerFactory.NoopOptimizer.class, generator.getOptimizer().getClass() ); assertEquals( SequenceStyleGenerator.DEF_SEQUENCE_NAME, generator.getDatabaseStructure().getName() ); }
/** * Test default optimizer selection for table backed generators * based on the configured increment size. Here we always prefer * pooled. */ public void testDefaultOptimizerBasedOnIncrementBackedByTable() { Properties props = new Properties(); props.setProperty( SequenceStyleGenerator.INCREMENT_PARAM, "10" ); Dialect dialect = new TableDialect(); SequenceStyleGenerator generator = new SequenceStyleGenerator(); generator.configure( Hibernate.LONG, props, dialect ); assertClassAssignability( TableStructure.class, generator.getDatabaseStructure().getClass() ); assertClassAssignability( OptimizerFactory.PooledOptimizer.class, generator.getOptimizer().getClass() ); assertEquals( SequenceStyleGenerator.DEF_SEQUENCE_NAME, generator.getDatabaseStructure().getName() ); }
/** * Test forcing of table as backing strucuture with dialect supporting sequences */ public void testForceTableUse() { Dialect dialect = new SequenceDialect(); Properties props = new Properties(); props.setProperty( SequenceStyleGenerator.FORCE_TBL_PARAM, "true" ); SequenceStyleGenerator generator = new SequenceStyleGenerator(); generator.configure( Hibernate.LONG, props, dialect ); assertClassAssignability( TableStructure.class, generator.getDatabaseStructure().getClass() ); assertClassAssignability( OptimizerFactory.NoopOptimizer.class, generator.getOptimizer().getClass() ); assertEquals( SequenceStyleGenerator.DEF_SEQUENCE_NAME, generator.getDatabaseStructure().getName() ); }
@Override public Map<String, Class<?>> getStrategies() { return ImmutableMap.<String, Class<?>> builder() // overriding default SequenceStyleGenerator; our SequenceStyleGenerator is configured to // always generate a sequence by table .put(SequenceStyleGenerator.class.getName(), PerTableSequenceStyleGenerator.class).build(); }
private void manageSequenceGenerator(Mappings mappings, Table tab, SimpleValue id, SequenceGenerator generator) { id.setIdentifierGeneratorStrategy ("enhanced-sequence"); Properties params = new Properties(); params.put(PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, mappings.getObjectNameNormalizer()); params.put(SequenceStyleGenerator.SEQUENCE_PARAM, quoteIdentifier(generator.getName())); params.setProperty( SequenceStyleGenerator.SCHEMA, quoteIdentifier(tab.getSchema())); id.setIdentifierGeneratorProperties(params); id.setNullValue(null); }
/** Returns the identifier generator created for the specified sequence and session. */ @SuppressWarnings("resource") private static IdentifierGenerator createIdentifierGenerator(String sequenceName, Session session) { SessionFactory sessionFactory = session.getSessionFactory(); if (sessionFactory instanceof SessionFactoryImpl) { SessionFactoryImpl sessionFactoryImpl = (SessionFactoryImpl) sessionFactory; ServiceRegistry registry = sessionFactoryImpl.getServiceRegistry(); Properties params = new Properties(); params.setProperty("sequence", sequenceName); SequenceStyleGenerator sequenceStyleGenerator = new SequenceStyleGenerator(); sequenceStyleGenerator.configure(LongType.INSTANCE, params, registry); return sequenceStyleGenerator; } else { throw new IllegalStateException("Not yet know how to handle the session factory of the given session!"); } }