/** * Check strategy configuration. */ @Test public void testConfiguration() { final Properties params = new Properties(); params.put("identity_tables", "summy.seg"); params.put(PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, new ObjectNameNormalizer() { @Override protected MetadataBuildingContext getBuildingContext() { return null; } }); OptimizedSequenceStyleGenerator optimizedSequenceStyleGenerator = newStyleGenerator(); optimizedSequenceStyleGenerator.configure(StringType.INSTANCE, params, newServiceRegistry()); }
private void manageIdentityGenerator(Mappings mappings, Table tab, SimpleValue id) { id.setIdentifierGeneratorStrategy(PortofinoIdentityGenerator.class.getName()); //"identity"); Properties params = new Properties(); params.put(PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, mappings.getObjectNameNormalizer()); if (mappings.getSchemaName() != null) { params.setProperty( PersistentIdentifierGenerator.SCHEMA, mappings.getObjectNameNormalizer().normalizeIdentifierQuoting(mappings.getSchemaName())); } if (mappings.getCatalogName() != null) { params.setProperty( PersistentIdentifierGenerator.CATALOG, mappings.getObjectNameNormalizer().normalizeIdentifierQuoting(mappings.getCatalogName())); } id.setIdentifierGeneratorProperties(params); id.setNullValue(null); }
private void manageTableGenerator(Mappings mappings, Table tab, SimpleValue id, com.manydesigns.portofino.model.database.TableGenerator generator) { id.setIdentifierGeneratorStrategy("enhanced-table"); Properties params = new Properties(); params.put(TableGenerator.TABLE, tab); params.put(TableGenerator.TABLE_PARAM, quoteIdentifier(generator.getTable())); params.put(PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, mappings.getObjectNameNormalizer()); params.put(TableGenerator.SEGMENT_COLUMN_PARAM, quoteIdentifier(generator.getKeyColumn())); params.put(TableGenerator.SEGMENT_VALUE_PARAM, generator.getKeyValue()); params.put(TableGenerator.VALUE_COLUMN_PARAM, quoteIdentifier(generator.getValueColumn())); params.setProperty( TableGenerator.SCHEMA, quoteIdentifier(tab.getSchema())); id.setIdentifierGeneratorProperties(params); id.setNullValue(null); }
private void manageTableGenerator(Mappings mappings, Table tab, SimpleValue id, com.manydesigns.portofino.model.database.TableGenerator generator) { id.setIdentifierGeneratorStrategy("enhanced-table"); Properties params = new Properties(); params.put(TableGenerator.TABLE, tab); params.put(TableGenerator.TABLE_PARAM, escapeName(generator.getTable())); params.put(PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, mappings.getObjectNameNormalizer()); params.put(TableGenerator.SEGMENT_COLUMN_PARAM, escapeName(generator.getKeyColumn())); params.put(TableGenerator.SEGMENT_VALUE_PARAM, generator.getKeyValue()); params.put(TableGenerator.VALUE_COLUMN_PARAM,escapeName(generator.getValueColumn())); params.setProperty( TableGenerator.SCHEMA,escapeName(tab.getSchema())); id.setIdentifierGeneratorProperties(params); id.setNullValue(null); }
public void validateSchema(Dialect dialect, DatabaseMetadata databaseMetadata)throws HibernateException { secondPassCompile(); String defaultCatalog = properties.getProperty( Environment.DEFAULT_CATALOG ); String defaultSchema = properties.getProperty( Environment.DEFAULT_SCHEMA ); Iterator iter = getTableMappings(); while ( iter.hasNext() ) { Table table = (Table) iter.next(); if ( table.isPhysicalTable() ) { TableMetadata tableInfo = databaseMetadata.getTableMetadata( table.getName(), ( table.getSchema() == null ) ? defaultSchema : table.getSchema(), ( table.getCatalog() == null ) ? defaultCatalog : table.getCatalog(), table.isQuoted()); if ( tableInfo == null ) { throw new HibernateException( "Missing table: " + table.getName() ); } else { table.validateColumns( dialect, mapping, tableInfo ); } } } iter = iterateGenerators( dialect ); while ( iter.hasNext() ) { PersistentIdentifierGenerator generator = (PersistentIdentifierGenerator) iter.next(); Object key = generator.generatorKey(); if (key instanceof String) { key = normalizer.normalizeIdentifierQuoting( (String) key ); } if ( !databaseMetadata.isSequence( key ) && !databaseMetadata.isTable( key ) ) { throw new HibernateException( "Missing sequence or table: " + key ); } } }
@SuppressWarnings( {"unchecked"} ) void resolve() { for ( EntityBinding entityBinding : metadata.getEntityBindings() ) { if ( entityBinding.isRoot() ) { Properties properties = new Properties( ); properties.putAll( metadata.getServiceRegistry() .getService( ConfigurationService.class ) .getSettings() ); //TODO: where should these be added??? if ( ! properties.contains( AvailableSettings.PREFER_POOLED_VALUES_LO ) ) { properties.put( AvailableSettings.PREFER_POOLED_VALUES_LO, "false" ); } if ( ! properties.contains( PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER ) ) { properties.put( PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, new ObjectNameNormalizerImpl( metadata ) ); } entityBinding.getHierarchyDetails().getEntityIdentifier().createIdentifierGenerator( metadata.getIdentifierGeneratorFactory(), properties ); } } }
public void validateSchema(Dialect dialect, DatabaseMetadata databaseMetadata) throws HibernateException { secondPassCompile(); String defaultCatalog = properties.getProperty( Environment.DEFAULT_CATALOG ); String defaultSchema = properties.getProperty( Environment.DEFAULT_SCHEMA ); Iterator iter = getTableMappings(); while ( iter.hasNext() ) { Table table = (Table) iter.next(); if ( table.isPhysicalTable() ) { TableMetadata tableInfo = databaseMetadata.getTableMetadata( table.getName(), ( table.getSchema() == null ) ? defaultSchema : table.getSchema(), ( table.getCatalog() == null ) ? defaultCatalog : table.getCatalog(), table.isQuoted()); if ( tableInfo == null ) { throw new HibernateException( "Missing table: " + table.getName() ); } else { table.validateColumns( dialect, mapping, tableInfo ); } } } iter = iterateGenerators( dialect ); while ( iter.hasNext() ) { PersistentIdentifierGenerator generator = (PersistentIdentifierGenerator) iter.next(); Object key = generator.generatorKey(); if ( !databaseMetadata.isSequence( key ) && !databaseMetadata.isTable( key ) ) { throw new HibernateException( "Missing sequence or table: " + key ); } } }
@Override protected QualifiedName determineSequenceName(Properties params, Dialect dialect, JdbcEnvironment jdbcEnv) { String tableName = params.getProperty(PersistentIdentifierGenerator.TABLE); String columnName = params.getProperty(PersistentIdentifierGenerator.PK); if (tableName != null && columnName != null) { StringBuilder sequenceNameBuilder = new StringBuilder(); sequenceNameBuilder.append(tableName); sequenceNameBuilder.append(SEQUENCE_NAME_SEPARATOR); sequenceNameBuilder.append(columnName); sequenceNameBuilder.append(SEQUENCE_NAME_SEPARATOR); sequenceNameBuilder.append(SEQUENCE_NAME_SUFFIX); // todo : need to incorporate implicit catalog and schema names final Identifier catalog = jdbcEnv.getIdentifierHelper().toIdentifier( ConfigurationHelper.getString( CATALOG, params ) ); final Identifier schema = jdbcEnv.getIdentifierHelper().toIdentifier( ConfigurationHelper.getString( SCHEMA, params ) ); return new QualifiedNameParser.NameParts( catalog, schema, jdbcEnv.getIdentifierHelper().toIdentifier( sequenceNameBuilder.toString() ) ); } throw new IllegalStateException("Unable to build the sequence name"); }
private void manageIncrementGenerator(Mappings mappings, Table tab, SimpleValue id, String entityName) { id.setIdentifierGeneratorStrategy("increment"); Properties params = new Properties(); params.put(PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, mappings.getObjectNameNormalizer()); params.setProperty(PersistentIdentifierGenerator.SCHEMA, quoteIdentifier(tab.getSchema())); params.put(IncrementGenerator.ENTITY_NAME, entityName); id.setIdentifierGeneratorProperties(params); id.setNullValue(null); }
public void configure(Type type, Properties params, Dialect d) throws MappingException { table = params.getProperty("table"); if (table == null) table = params.getProperty(PersistentIdentifierGenerator.TABLE); column = params.getProperty("column"); if (column == null) column = params.getProperty(PersistentIdentifierGenerator.PK); String schema = params.getProperty(PersistentIdentifierGenerator.SCHEMA); String minIdParam = params.getProperty("minId"); if(null != minIdParam) minId = Long.parseLong(minIdParam); selectsql = "select max(" + column + ") from " + (schema == null ? table : schema + "." + table); returnClass = type.getReturnedClass(); }
private void manageIdentityGenerator(Mappings mappings, Table tab, SimpleValue id) { id.setIdentifierGeneratorStrategy("identity"); Properties params = new Properties(); params.put(PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, mappings.getObjectNameNormalizer()); params.setProperty( PersistentIdentifierGenerator.SCHEMA, escapeName(tab.getSchema())); id.setIdentifierGeneratorProperties(params); id.setNullValue(null); }
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 void manageIncrementGenerator(Mappings mappings, Table tab, SimpleValue id, String entityName) { id.setIdentifierGeneratorStrategy("increment"); Properties params = new Properties(); params.put(PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, mappings.getObjectNameNormalizer()); params.setProperty(PersistentIdentifierGenerator.SCHEMA, escapeName(tab.getSchema())); params.put(IncrementGenerator.ENTITY_NAME, entityName); id.setIdentifierGeneratorProperties(params); id.setNullValue(null); }
@Override public boolean includeGenerator(PersistentIdentifierGenerator pig) { return includeGenerators; }
private static void makeIdentifier(Element node, SimpleValue model, Mappings mappings) { // GENERATOR Element subnode = node.element( "generator" ); if ( subnode != null ) { final String generatorClass = subnode.attributeValue( "class" ); model.setIdentifierGeneratorStrategy( generatorClass ); Properties params = new Properties(); // YUCK! but cannot think of a clean way to do this given the string-config based scheme params.put( PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, mappings.getObjectNameNormalizer() ); if ( mappings.getSchemaName() != null ) { params.setProperty( PersistentIdentifierGenerator.SCHEMA, mappings.getObjectNameNormalizer().normalizeIdentifierQuoting( mappings.getSchemaName() ) ); } if ( mappings.getCatalogName() != null ) { params.setProperty( PersistentIdentifierGenerator.CATALOG, mappings.getObjectNameNormalizer().normalizeIdentifierQuoting( mappings.getCatalogName() ) ); } Iterator iter = subnode.elementIterator( "param" ); while ( iter.hasNext() ) { Element childNode = (Element) iter.next(); params.setProperty( childNode.attributeValue( "name" ), childNode.getTextTrim() ); } model.setIdentifierGeneratorProperties( params ); } model.getTable().setIdentifierValue( model ); // ID UNSAVED-VALUE Attribute nullValueNode = node.attribute( "unsaved-value" ); if ( nullValueNode != null ) { model.setNullValue( nullValueNode.getValue() ); } else { if ( "assigned".equals( model.getIdentifierGeneratorStrategy() ) ) { model.setNullValue( "undefined" ); } else { model.setNullValue( null ); } } }
/** * apply an id generator to a SimpleValue */ public static void makeIdGenerator( SimpleValue id, String generatorType, String generatorName, Mappings mappings, Map<String, IdGenerator> localGenerators) { Table table = id.getTable(); table.setIdentifierValue( id ); //generator settings id.setIdentifierGeneratorStrategy( generatorType ); Properties params = new Properties(); //always settable params.setProperty( PersistentIdentifierGenerator.TABLE, table.getName() ); if ( id.getColumnSpan() == 1 ) { params.setProperty( PersistentIdentifierGenerator.PK, ( (org.hibernate.mapping.Column) id.getColumnIterator().next() ).getName() ); } // YUCK! but cannot think of a clean way to do this given the string-config based scheme params.put( PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, mappings.getObjectNameNormalizer() ); if ( !isEmptyAnnotationValue( generatorName ) ) { //we have a named generator IdGenerator gen = mappings.getGenerator( generatorName, localGenerators ); if ( gen == null ) { throw new AnnotationException( "Unknown Id.generator: " + generatorName ); } //This is quite vague in the spec but a generator could override the generate choice String identifierGeneratorStrategy = gen.getIdentifierGeneratorStrategy(); //yuk! this is a hack not to override 'AUTO' even if generator is set final boolean avoidOverriding = identifierGeneratorStrategy.equals( "identity" ) || identifierGeneratorStrategy.equals( "seqhilo" ) || identifierGeneratorStrategy.equals( MultipleHiLoPerTableGenerator.class.getName() ); if ( generatorType == null || !avoidOverriding ) { id.setIdentifierGeneratorStrategy( identifierGeneratorStrategy ); } //checkIfMatchingGenerator(gen, generatorType, generatorName); Iterator genParams = gen.getParams().entrySet().iterator(); while ( genParams.hasNext() ) { Map.Entry elt = (Map.Entry) genParams.next(); params.setProperty( (String) elt.getKey(), (String) elt.getValue() ); } } if ( "assigned".equals( generatorType ) ) id.setNullValue( "undefined" ); id.setIdentifierGeneratorProperties( params ); }
@Override public void registerPersistentGenerators(Map generatorMap) { if ( PersistentIdentifierGenerator.class.isInstance( subGenerator ) ) { generatorMap.put( ( (PersistentIdentifierGenerator) subGenerator ).generatorKey(), subGenerator ); } }
public IdentifierGenerator createIdentifierGenerator( IdentifierGeneratorFactory identifierGeneratorFactory, Dialect dialect, String defaultCatalog, String defaultSchema, RootClass rootClass) throws MappingException { Properties params = new Properties(); //if the hibernate-mapping did not specify a schema/catalog, use the defaults //specified by properties - but note that if the schema/catalog were specified //in hibernate-mapping, or as params, they will already be initialized and //will override the values set here (they are in identifierGeneratorProperties) if ( defaultSchema!=null ) { params.setProperty(PersistentIdentifierGenerator.SCHEMA, defaultSchema); } if ( defaultCatalog!=null ) { params.setProperty(PersistentIdentifierGenerator.CATALOG, defaultCatalog); } //pass the entity-name, if not a collection-id if (rootClass!=null) { params.setProperty( IdentifierGenerator.ENTITY_NAME, rootClass.getEntityName() ); params.setProperty( IdentifierGenerator.JPA_ENTITY_NAME, rootClass.getJpaEntityName() ); } //init the table here instead of earlier, so that we can get a quoted table name //TODO: would it be better to simply pass the qualified table name, instead of // splitting it up into schema/catalog/table names String tableName = getTable().getQuotedName(dialect); params.setProperty( PersistentIdentifierGenerator.TABLE, tableName ); //pass the column name (a generated id almost always has a single column) String columnName = ( (Column) getColumnIterator().next() ).getQuotedName(dialect); params.setProperty( PersistentIdentifierGenerator.PK, columnName ); if (rootClass!=null) { StringBuilder tables = new StringBuilder(); Iterator iter = rootClass.getIdentityTables().iterator(); while ( iter.hasNext() ) { Table table= (Table) iter.next(); tables.append( table.getQuotedName(dialect) ); if ( iter.hasNext() ) tables.append(", "); } params.setProperty( PersistentIdentifierGenerator.TABLES, tables.toString() ); } else { params.setProperty( PersistentIdentifierGenerator.TABLES, tableName ); } if (identifierGeneratorProperties!=null) { params.putAll(identifierGeneratorProperties); } // TODO : we should pass along all settings once "config lifecycle" is hashed out... params.put( Environment.PREFER_POOLED_VALUES_LO, mappings.getConfigurationProperties().getProperty( Environment.PREFER_POOLED_VALUES_LO, "false" ) ); identifierGeneratorFactory.setDialect( dialect ); return identifierGeneratorFactory.createIdentifierGenerator( identifierGeneratorStrategy, getType(), params ); }
IdentifierGenerator createIdentifierGenerator( IdGenerator idGenerator, IdentifierGeneratorFactory identifierGeneratorFactory, Properties properties) { Properties params = new Properties(); params.putAll( properties ); // use the schema/catalog specified by getValue().getTable() - but note that // if the schema/catalog were specified as params, they will already be initialized and //will override the values set here (they are in idGenerator.getParameters().) Schema schema = getValue().getTable().getSchema(); if ( schema != null ) { if ( schema.getName().getSchema() != null ) { params.setProperty( PersistentIdentifierGenerator.SCHEMA, schema.getName().getSchema().getName() ); } if ( schema.getName().getCatalog() != null ) { params.setProperty( PersistentIdentifierGenerator.CATALOG, schema.getName().getCatalog().getName() ); } } // TODO: not sure how this works for collection IDs... //pass the entity-name, if not a collection-id //if ( rootClass!=null) { params.setProperty( IdentifierGenerator.ENTITY_NAME, getContainer().seekEntityBinding().getEntity().getName() ); //} //init the table here instead of earlier, so that we can get a quoted table name //TODO: would it be better to simply pass the qualified table name, instead of // splitting it up into schema/catalog/table names String tableName = getValue().getTable().getQualifiedName( identifierGeneratorFactory.getDialect() ); params.setProperty( PersistentIdentifierGenerator.TABLE, tableName ); //pass the column name (a generated id almost always has a single column) if ( getSimpleValueSpan() > 1 ) { throw new MappingException( "A SimpleAttributeBinding used for an identifier has more than 1 Value: " + getAttribute().getName() ); } SimpleValue simpleValue = (SimpleValue) getValue(); if ( !Column.class.isInstance( simpleValue ) ) { throw new MappingException( "Cannot create an IdentifierGenerator because the value is not a column: " + simpleValue.toLoggableString() ); } params.setProperty( PersistentIdentifierGenerator.PK, ( (Column) simpleValue ).getColumnName().encloseInQuotesIfQuoted( identifierGeneratorFactory.getDialect() ) ); // TODO: is this stuff necessary for SimpleValue??? //if (rootClass!=null) { // StringBuffer tables = new StringBuffer(); // Iterator iter = rootClass.getIdentityTables().iterator(); // while ( iter.hasNext() ) { // Table table= (Table) iter.next(); // tables.append( table.getQuotedName(dialect) ); // if ( iter.hasNext() ) tables.append(", "); // } // params.setProperty( PersistentIdentifierGenerator.TABLES, tables.toString() ); //} //else { params.setProperty( PersistentIdentifierGenerator.TABLES, tableName ); //} params.putAll( idGenerator.getParameters() ); return identifierGeneratorFactory.createIdentifierGenerator( idGenerator.getStrategy(), getHibernateTypeDescriptor().getResolvedTypeMapping(), params ); }
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 ); }
private static void bindTableGenerator(MetadataImplementor metadata, AnnotationInstance generator) { String name = JandexHelper.getValue( generator, "name", String.class ); Map<String, String> parameterMap = new HashMap<String, String>(); addStringParameter( generator, "catalog", parameterMap, PersistentIdentifierGenerator.CATALOG ); addStringParameter( generator, "schema", parameterMap, PersistentIdentifierGenerator.SCHEMA ); boolean useNewIdentifierGenerators = metadata.getOptions().useNewIdentifierGenerators(); String strategy = EnumConversionHelper.generationTypeToGeneratorStrategyName( GenerationType.TABLE, useNewIdentifierGenerators ); if ( useNewIdentifierGenerators ) { parameterMap.put( TableGenerator.CONFIG_PREFER_SEGMENT_PER_ENTITY, "true" ); addStringParameter( generator, "table", parameterMap, TableGenerator.TABLE_PARAM ); addStringParameter( generator, "pkColumnName", parameterMap, TableGenerator.SEGMENT_COLUMN_PARAM ); addStringParameter( generator, "pkColumnValue", parameterMap, TableGenerator.SEGMENT_VALUE_PARAM ); addStringParameter( generator, "valueColumnName", parameterMap, TableGenerator.VALUE_COLUMN_PARAM ); parameterMap.put( TableGenerator.INCREMENT_PARAM, String.valueOf( JandexHelper.getValue( generator, "allocationSize", String.class ) ) ); parameterMap.put( TableGenerator.INITIAL_PARAM, String.valueOf( JandexHelper.getValue( generator, "initialValue", String.class ) + 1 ) ); } else { addStringParameter( generator, "table", parameterMap, MultipleHiLoPerTableGenerator.ID_TABLE ); addStringParameter( generator, "pkColumnName", parameterMap, MultipleHiLoPerTableGenerator.PK_COLUMN_NAME ); addStringParameter( generator, "pkColumnValue", parameterMap, MultipleHiLoPerTableGenerator.PK_VALUE_NAME ); addStringParameter( generator, "valueColumnName", parameterMap, MultipleHiLoPerTableGenerator.VALUE_COLUMN_NAME ); parameterMap.put( TableHiLoGenerator.MAX_LO, String.valueOf( JandexHelper.getValue( generator, "allocationSize", Integer.class ) - 1 ) ); } if ( JandexHelper.getValue( generator, "uniqueConstraints", AnnotationInstance[].class ).length > 0 ) { LOG.ignoringTableGeneratorConstraints( name ); } metadata.addIdGenerator( new IdGenerator( name, strategy, parameterMap ) ); LOG.tracef( "Add table generator with name: %s", name ); }
@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); }
private static void makeIdentifier(Element node, SimpleValue model, Mappings mappings) { // GENERATOR Element subnode = node.element( "generator" ); if ( subnode != null ) { model.setIdentifierGeneratorStrategy( subnode.attributeValue( "class" ) ); Properties params = new Properties(); if ( mappings.getSchemaName() != null ) { params.setProperty( PersistentIdentifierGenerator.SCHEMA, mappings.getSchemaName() ); } if ( mappings.getCatalogName() != null ) { params.setProperty( PersistentIdentifierGenerator.CATALOG, mappings.getCatalogName() ); } Iterator iter = subnode.elementIterator( "param" ); while ( iter.hasNext() ) { Element childNode = (Element) iter.next(); params.setProperty( childNode.attributeValue( "name" ), childNode.getText() ); } model.setIdentifierGeneratorProperties( params ); } model.getTable().setIdentifierValue( model ); // ID UNSAVED-VALUE Attribute nullValueNode = node.attribute( "unsaved-value" ); if ( nullValueNode != null ) { model.setNullValue( nullValueNode.getValue() ); } else { if ( "assigned".equals( model.getIdentifierGeneratorStrategy() ) ) { model.setNullValue( "undefined" ); } else { model.setNullValue( null ); } } }
public IdentifierGenerator createIdentifierGenerator( Dialect dialect, String defaultCatalog, String defaultSchema, RootClass rootClass) throws MappingException { Properties params = new Properties(); //if the hibernate-mapping did not specify a schema/catalog, use the defaults //specified by properties - but note that if the schema/catalog were specified //in hibernate-mapping, or as params, they will already be initialized and //will override the values set here (they are in identifierGeneratorProperties) if ( defaultSchema!=null ) { params.setProperty(PersistentIdentifierGenerator.SCHEMA, defaultSchema); } if ( defaultCatalog!=null ) { params.setProperty(PersistentIdentifierGenerator.CATALOG, defaultCatalog); } //pass the entity-name, if not a collection-id if (rootClass!=null) { params.setProperty( IdentifierGenerator.ENTITY_NAME, rootClass.getEntityName() ); } //init the table here instead of earlier, so that we can get a quoted table name //TODO: would it be better to simply pass the qualified table name, instead of // splitting it up into schema/catalog/table names String tableName = getTable().getQuotedName(dialect); params.setProperty( PersistentIdentifierGenerator.TABLE, tableName ); //pass the column name (a generated id almost always has a single column) String columnName = ( (Column) getColumnIterator().next() ).getQuotedName(dialect); params.setProperty( PersistentIdentifierGenerator.PK, columnName ); if (rootClass!=null) { StringBuffer tables = new StringBuffer(); Iterator iter = rootClass.getIdentityTables().iterator(); while ( iter.hasNext() ) { Table table= (Table) iter.next(); tables.append( table.getQuotedName(dialect) ); if ( iter.hasNext() ) tables.append(", "); } params.setProperty( PersistentIdentifierGenerator.TABLES, tables.toString() ); } else { params.setProperty( PersistentIdentifierGenerator.TABLES, tableName ); } if (identifierGeneratorProperties!=null) { params.putAll(identifierGeneratorProperties); } return IdentifierGeneratorFactory.create( identifierGeneratorStrategy, getType(), params, dialect ); }
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); }
@Override public synchronized void configure(Type type, Properties params, Dialect d) throws MappingException { tableName = params.getProperty(PersistentIdentifierGenerator.TABLE); }
boolean includeGenerator(PersistentIdentifierGenerator pig);