Java Code Examples for org.hibernate.mapping.SimpleValue#setIdentifierGeneratorStrategy()

The following examples show how to use org.hibernate.mapping.SimpleValue#setIdentifierGeneratorStrategy() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BinderHelper.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * apply an id generator to a SimpleValue
 */
public static void makeIdGenerator(
		SimpleValue id,
		XProperty idXProperty,
		String generatorType,
		String generatorName,
		MetadataBuildingContext buildingContext,
		Map<String, IdentifierGeneratorDefinition> localGenerators) {
	log.debugf( "#makeIdGenerator(%s, %s, %s, %s, ...)", id, idXProperty, generatorType, generatorName );

	Table table = id.getTable();
	table.setIdentifierValue( id );
	//generator settings
	id.setIdentifierGeneratorStrategy( generatorType );

	Properties params = new Properties();

	//always settable
	params.setProperty(
			PersistentIdentifierGenerator.TABLE, table.getName()
	);

	final String implicitCatalogName = buildingContext.getBuildingOptions().getMappingDefaults().getImplicitCatalogName();
	if ( implicitCatalogName != null ) {
		params.put( PersistentIdentifierGenerator.CATALOG, implicitCatalogName );
	}
	final String implicitSchemaName = buildingContext.getBuildingOptions().getMappingDefaults().getImplicitSchemaName();
	if ( implicitSchemaName != null ) {
		params.put( PersistentIdentifierGenerator.SCHEMA, implicitSchemaName );
	}

	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, buildingContext.getObjectNameNormalizer() );
	params.put( IdentifierGenerator.GENERATOR_NAME, generatorName );

	if ( !isEmptyAnnotationValue( generatorName ) ) {
		//we have a named generator
		IdentifierGeneratorDefinition gen = getIdentifierGenerator(
				generatorName,
				idXProperty,
				localGenerators,
				buildingContext
		);
		if ( gen == null ) {
			throw new AnnotationException( "Unknown named generator (@GeneratedValue#generatorName): " + generatorName );
		}
		//This is quite vague in the spec but a generator could override the generate choice
		String identifierGeneratorStrategy = gen.getStrategy();
		//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);
		for ( Object o : gen.getParameters().entrySet() ) {
			Map.Entry elt = (Map.Entry) o;
			if ( elt.getKey() == null ) {
				continue;
			}
			params.setProperty( (String) elt.getKey(), (String) elt.getValue() );
		}
	}
	if ( "assigned".equals( generatorType ) ) {
		id.setNullValue( "undefined" );
	}
	id.setIdentifierGeneratorProperties( params );
}
 
Example 2
Source File: ModelBinder.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void makeIdentifier(
		final MappingDocument sourceDocument,
		IdentifierGeneratorDefinition generator,
		String unsavedValue,
		SimpleValue identifierValue) {
	if ( generator != null ) {
		String generatorName = generator.getStrategy();
		Properties params = new Properties();

		// see if the specified generator name matches a registered <identifier-generator/>
		IdentifierGeneratorDefinition generatorDef = sourceDocument.getMetadataCollector().getIdentifierGenerator( generatorName );
		if ( generatorDef != null ) {
			generatorName = generatorDef.getStrategy();
			params.putAll( generatorDef.getParameters() );
		}

		identifierValue.setIdentifierGeneratorStrategy( generatorName );

		// YUCK!  but cannot think of a clean way to do this given the string-config based scheme
		params.put( PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER, objectNameNormalizer);

		if ( database.getDefaultNamespace().getPhysicalName().getSchema() != null ) {
			params.setProperty(
					PersistentIdentifierGenerator.SCHEMA,
					database.getDefaultNamespace().getPhysicalName().getSchema().render( database.getDialect() )
			);
		}
		if ( database.getDefaultNamespace().getPhysicalName().getCatalog() != null ) {
			params.setProperty(
					PersistentIdentifierGenerator.CATALOG,
					database.getDefaultNamespace().getPhysicalName().getCatalog().render( database.getDialect() )
			);
		}

		params.putAll( generator.getParameters() );

		identifierValue.setIdentifierGeneratorProperties( params );
	}

	identifierValue.getTable().setIdentifierValue( identifierValue );

	if ( StringHelper.isNotEmpty( unsavedValue ) ) {
		identifierValue.setNullValue( unsavedValue );
	}
	else {
		if ( "assigned".equals( identifierValue.getIdentifierGeneratorStrategy() ) ) {
			identifierValue.setNullValue( "undefined" );
		}
		else {
			identifierValue.setNullValue( null );
		}
	}
}
 
Example 3
Source File: HbmBinder.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
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 );
			}
		}
	}