org.hibernate.tuple.GenerationTiming Java Examples

The following examples show how to use org.hibernate.tuple.GenerationTiming. 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: EntityMetamodel.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public GenerationStrategyPair(
		InMemoryValueGenerationStrategy inMemoryStrategy,
		InDatabaseValueGenerationStrategy inDatabaseStrategy) {
	// perform some normalization.  Also check that only one (if any) strategy is specified
	if ( inMemoryStrategy == null ) {
		inMemoryStrategy = NoInMemoryValueGenerationStrategy.INSTANCE;
	}
	if ( inDatabaseStrategy == null ) {
		inDatabaseStrategy = NoInDatabaseValueGenerationStrategy.INSTANCE;
	}

	if ( inMemoryStrategy.getGenerationTiming() != GenerationTiming.NEVER
			&& inDatabaseStrategy.getGenerationTiming() != GenerationTiming.NEVER ) {
		throw new ValueGenerationStrategyException(
				"in-memory and in-database value generation are mutually exclusive"
		);
	}

	this.inMemoryStrategy = inMemoryStrategy;
	this.inDatabaseStrategy = inDatabaseStrategy;
}
 
Example #2
Source File: PropertyBinder.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private ValueGeneration determineValueGenerationStrategy(XProperty property) {
	ValueGeneration valueGeneration = getValueGenerationFromAnnotations( property );

	if ( valueGeneration == null ) {
		return NoValueGeneration.INSTANCE;
	}

	final GenerationTiming when = valueGeneration.getGenerationTiming();

	if ( valueGeneration.getValueGenerator() == null ) {
		insertable = false;
		if ( when == GenerationTiming.ALWAYS ) {
			updatable = false;
		}
	}

	return valueGeneration;
}
 
Example #3
Source File: AbstractEntityPersister.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public void processUpdateGeneratedProperties(
		Serializable id,
		Object entity,
		Object[] state,
		SharedSessionContractImplementor session) {
	if ( !hasUpdateGeneratedProperties() ) {
		throw new AssertionFailure( "no update-generated properties" );
	}
	processGeneratedProperties(
			id,
			entity,
			state,
			session,
			sqlUpdateGeneratedValuesSelectString,
			GenerationTiming.ALWAYS
	);
}
 
Example #4
Source File: AbstractEntityPersister.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public void processInsertGeneratedProperties(
		Serializable id,
		Object entity,
		Object[] state,
		SharedSessionContractImplementor session) {
	if ( !hasInsertGeneratedProperties() ) {
		throw new AssertionFailure( "no insert-generated properties" );
	}
	processGeneratedProperties(
			id,
			entity,
			state,
			session,
			sqlInsertGeneratedValuesSelectString,
			GenerationTiming.INSERT
	);
}
 
Example #5
Source File: EntityMetamodel.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void add(InDatabaseValueGenerationStrategy inDatabaseStrategy) {
	if ( inDatabaseStrategies == null ) {
		inDatabaseStrategies = new ArrayList<>();
	}
	inDatabaseStrategies.add( inDatabaseStrategy );

	if ( inDatabaseStrategy.getGenerationTiming() != GenerationTiming.NEVER ) {
		hadInDatabaseGeneration = true;
	}
}
 
Example #6
Source File: EntityMetamodel.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void add(InMemoryValueGenerationStrategy inMemoryStrategy) {
	if ( inMemoryStrategies == null ) {
		inMemoryStrategies = new ArrayList<>();
	}
	inMemoryStrategies.add( inMemoryStrategy );

	if ( inMemoryStrategy.getGenerationTiming() != GenerationTiming.NEVER ) {
		hadInMemoryGeneration = true;
	}
}
 
Example #7
Source File: EntityMetamodel.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private static GenerationStrategyPair buildGenerationStrategyPair(
		final SessionFactoryImplementor sessionFactory,
		final Property mappingProperty) {
	final ValueGeneration valueGeneration = mappingProperty.getValueGenerationStrategy();
	if ( valueGeneration != null && valueGeneration.getGenerationTiming() != GenerationTiming.NEVER ) {
		// the property is generated in full. build the generation strategy pair.
		if ( valueGeneration.getValueGenerator() != null ) {
			// in-memory generation
			return new GenerationStrategyPair(
					FullInMemoryValueGenerationStrategy.create( valueGeneration )
			);
		}
		else {
			// in-db generation
			return new GenerationStrategyPair(
					create(
							sessionFactory,
							mappingProperty,
							valueGeneration
					)
			);
		}
	}
	else if ( mappingProperty.getValue() instanceof Component ) {
		final CompositeGenerationStrategyPairBuilder builder = new CompositeGenerationStrategyPairBuilder( mappingProperty );
		interpretPartialCompositeValueGeneration( sessionFactory, (Component) mappingProperty.getValue(), builder );
		return builder.buildPair();
	}

	return NO_GEN_PAIR;
}
 
Example #8
Source File: AbstractEntityPersister.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private boolean isValueGenerationRequired(NonIdentifierAttribute attribute, GenerationTiming matchTiming) {
	if ( attribute.getType() instanceof ComponentType ) {
		final ComponentType type = (ComponentType) attribute.getType();
		final ValueGeneration[] propertyValueGenerationStrategies = type.getPropertyValueGenerationStrategies();
		for ( int i = 0; i < propertyValueGenerationStrategies.length; i++ ) {
			if ( isReadRequired( propertyValueGenerationStrategies[i], matchTiming ) ) {
				return true;
			}
		}
		return false;
	}
	else {
		return isReadRequired( attribute.getValueGenerationStrategy(), matchTiming );
	}
}
 
Example #9
Source File: EntityMetamodel.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private InDatabaseValueGenerationStrategyImpl(
		GenerationTiming timing,
		boolean referenceColumnInSql,
		String[] referencedColumnValues) {
	this.timing = timing;
	this.referenceColumnInSql = referenceColumnInSql;
	this.referencedColumnValues = referencedColumnValues;
}
 
Example #10
Source File: PropertyBinder.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * In case the given annotation is a value generator annotation, the corresponding value generation strategy to be
 * applied to the given property is returned, {@code null} otherwise.
 */
private <A extends Annotation> AnnotationValueGeneration<A> getValueGenerationFromAnnotation(
		XProperty property,
		A annotation) {
	ValueGenerationType generatorAnnotation = annotation.annotationType()
			.getAnnotation( ValueGenerationType.class );

	if ( generatorAnnotation == null ) {
		return null;
	}

	Class<? extends AnnotationValueGeneration<?>> generationType = generatorAnnotation.generatedBy();
	AnnotationValueGeneration<A> valueGeneration = instantiateAndInitializeValueGeneration(
			annotation, generationType, property
	);

	if ( annotation.annotationType() == Generated.class &&
			property.isAnnotationPresent( javax.persistence.Version.class ) &&
			valueGeneration.getGenerationTiming() == GenerationTiming.INSERT ) {

		throw new AnnotationException(
				"@Generated(INSERT) on a @Version property not allowed, use ALWAYS (or NEVER): "
						+ StringHelper.qualify( holder.getPath(), name )
		);
	}

	return valueGeneration;
}
 
Example #11
Source File: EntityMetamodel.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public boolean isNaturalIdentifierInsertGenerated() {
	// the intention is for this call to replace the usage of the old ValueInclusion stuff (as exposed from
	// persister) in SelectGenerator to determine if it is safe to use the natural identifier to find the
	// insert-generated identifier.  That wont work if the natural-id is also insert-generated.
	//
	// Assumptions:
	//		* That code checks that there is a natural identifier before making this call, so we assume the same here
	// 		* That code assumes a non-composite natural-id, so we assume the same here
	final InDatabaseValueGenerationStrategy strategy = inDatabaseValueGenerationStrategies[ naturalIdPropertyNumbers[0] ];
	return strategy != null && strategy.getGenerationTiming() != GenerationTiming.NEVER;
}
 
Example #12
Source File: BasicAttributePropertySource.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public GenerationTiming getGenerationTiming() {
	return basicAttributeMapping.getGenerated();
}
 
Example #13
Source File: SingularAttributeSourceAnyImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public GenerationTiming getGenerationTiming() {
	return GenerationTiming.NEVER;
}
 
Example #14
Source File: ManyToOnePropertySource.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public GenerationTiming getGenerationTiming() {
	return null;
}
 
Example #15
Source File: SingularIdentifierAttributeSourceImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public GenerationTiming getGenerationTiming() {
	return null;
}
 
Example #16
Source File: CompositeIdentifierSingularAttributeSourceBasicImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public GenerationTiming getGenerationTiming() {
	return GenerationTiming.NEVER;
}
 
Example #17
Source File: TimestampAttributeSourceImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public GenerationTiming getGenerationTiming() {
	return timestampElement.getGenerated();
}
 
Example #18
Source File: SingularAttributeSourceBasicImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public GenerationTiming getGenerationTiming() {
	return propertyElement.getGenerated();
}
 
Example #19
Source File: ModelBinder.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void bindEntityVersion(
		MappingDocument sourceDocument,
		EntityHierarchySourceImpl hierarchySource,
		RootClass rootEntityDescriptor) {
	final VersionAttributeSource versionAttributeSource = hierarchySource.getVersionAttributeSource();

	final SimpleValue versionValue = new SimpleValue(
			sourceDocument,
			rootEntityDescriptor.getTable()
	);

	versionValue.makeVersion();

	bindSimpleValueType(
			sourceDocument,
			versionAttributeSource.getTypeInformation(),
			versionValue
	);

	relationalObjectBinder.bindColumnsAndFormulas(
			sourceDocument,
			versionAttributeSource.getRelationalValueSources(),
			versionValue,
			false,
			new RelationalObjectBinder.ColumnNamingDelegate() {
				@Override
				public Identifier determineImplicitName(LocalMetadataBuildingContext context) {
					return implicitNamingStrategy.determineBasicColumnName( versionAttributeSource );
				}
			}
	);

	Property prop = new Property();
	prop.setValue( versionValue );
	bindProperty(
			sourceDocument,
			versionAttributeSource,
			prop
	);

	// for version properties marked as being generated, make sure they are "always"
	// generated; aka, "insert" is invalid; this is dis-allowed by the DTD,
	// but just to make sure...
	if ( prop.getValueGenerationStrategy() != null ) {
		if ( prop.getValueGenerationStrategy().getGenerationTiming() == GenerationTiming.INSERT ) {
			throw new MappingException(
					"'generated' attribute cannot be 'insert' for version/timestamp property",
					sourceDocument.getOrigin()
			);
		}
	}

	if ( versionAttributeSource.getUnsavedValue() != null ) {
		versionValue.setNullValue( versionAttributeSource.getUnsavedValue() );
	}
	else {
		versionValue.setNullValue( "undefined" );
	}

	rootEntityDescriptor.setVersion( prop );
	rootEntityDescriptor.setDeclaredVersion( prop );
	rootEntityDescriptor.addProperty( prop );
}
 
Example #20
Source File: EntityMetamodel.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public GenerationTiming getGenerationTiming() {
	return GenerationTiming.NEVER;
}
 
Example #21
Source File: ModelBinder.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void bindProperty(
		MappingDocument mappingDocument,
		AttributeSource propertySource,
		Property property) {
	property.setName( propertySource.getName() );

	if ( StringHelper.isNotEmpty( propertySource.getXmlNodeName() ) ) {
		DeprecationLogger.DEPRECATION_LOGGER.logDeprecationOfDomEntityModeSupport();
	}

	property.setPropertyAccessorName(
			StringHelper.isNotEmpty( propertySource.getPropertyAccessorName() )
					? propertySource.getPropertyAccessorName()
					: mappingDocument.getMappingDefaults().getImplicitPropertyAccessorName()
	);

	if ( propertySource instanceof CascadeStyleSource ) {
		final CascadeStyleSource cascadeStyleSource = (CascadeStyleSource) propertySource;

		property.setCascade(
				StringHelper.isNotEmpty( cascadeStyleSource.getCascadeStyleName() )
						? cascadeStyleSource.getCascadeStyleName()
						: mappingDocument.getMappingDefaults().getImplicitCascadeStyleName()
		);
	}

	property.setOptimisticLocked( propertySource.isIncludedInOptimisticLocking() );

	if ( propertySource.isSingular() ) {
		final SingularAttributeSource singularAttributeSource = (SingularAttributeSource) propertySource;

		property.setInsertable( singularAttributeSource.isInsertable() );
		property.setUpdateable( singularAttributeSource.isUpdatable() );

		// NOTE : Property#is refers to whether a property is lazy via bytecode enhancement (not proxies)
		property.setLazy( singularAttributeSource.isBytecodeLazy() );

		final GenerationTiming generationTiming = singularAttributeSource.getGenerationTiming();
		if ( generationTiming == GenerationTiming.ALWAYS || generationTiming == GenerationTiming.INSERT ) {
			// we had generation specified...
			//   	HBM only supports "database generated values"
			property.setValueGenerationStrategy( new GeneratedValueGeneration( generationTiming ) );

			// generated properties can *never* be insertable...
			if ( property.isInsertable() ) {
				log.debugf(
						"Property [%s] specified %s generation, setting insertable to false : %s",
						propertySource.getName(),
						generationTiming.name(),
						mappingDocument.getOrigin()
				);
				property.setInsertable( false );
			}

			// properties generated on update can never be updatable...
			if ( property.isUpdateable() && generationTiming == GenerationTiming.ALWAYS ) {
				log.debugf(
						"Property [%s] specified ALWAYS generation, setting updateable to false : %s",
						propertySource.getName(),
						mappingDocument.getOrigin()
				);
				property.setUpdateable( false );
			}
		}
	}

	property.setMetaAttributes( propertySource.getToolingHintContext().getMetaAttributeMap() );

	if ( log.isDebugEnabled() ) {
		final StringBuilder message = new StringBuilder()
				.append( "Mapped property: " )
				.append( propertySource.getName() )
				.append( " -> [" );
		final Iterator itr = property.getValue().getColumnIterator();
		while ( itr.hasNext() ) {
			message.append( ( (Selectable) itr.next() ).getText() );
			if ( itr.hasNext() ) {
				message.append( ", " );
			}
		}
		message.append( "]" );
		log.debug( message.toString() );
	}
}
 
Example #22
Source File: AbstractToOneAttributeSourceImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public GenerationTiming getGenerationTiming() {
	return GenerationTiming.NEVER;
}
 
Example #23
Source File: AbstractSingularAttributeSourceEmbeddedImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public GenerationTiming getGenerationTiming() {
	// todo : is this correct here?
	return null;
}
 
Example #24
Source File: VersionAttributeSourceImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public GenerationTiming getGenerationTiming() {
	return versionElement.getGenerated();
}
 
Example #25
Source File: GenerationTimingConverter.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public static GenerationTiming fromXml(String name) {
	return GenerationTiming.parseFromName( name );
}
 
Example #26
Source File: GenerationTimingConverter.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public static String toXml(GenerationTiming generationTiming) {
	return ( null == generationTiming ) ?
			null :
			generationTiming.name().toLowerCase( Locale.ENGLISH );
}
 
Example #27
Source File: ModifiedByValueGeneration.java    From HibernateDemos with The Unlicense 4 votes vote down vote up
public GenerationTiming getGenerationTiming() {
	return GenerationTiming.ALWAYS;
}
 
Example #28
Source File: ModifiedByValueGeneration.java    From hibernate-demos with Apache License 2.0 4 votes vote down vote up
public GenerationTiming getGenerationTiming() {
	return GenerationTiming.ALWAYS;
}
 
Example #29
Source File: EntityMetamodel.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public boolean isVersionGenerated() {
	final InDatabaseValueGenerationStrategy strategy = inDatabaseValueGenerationStrategies[ versionPropertyIndex ];
	return strategy != null && strategy.getGenerationTiming() != GenerationTiming.NEVER;
}
 
Example #30
Source File: PropertyBinder.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public GenerationTiming getGenerationTiming() {
	return GenerationTiming.NEVER;
}