下面列出了org.hibernate.tuple.GenerationTiming#ALWAYS 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
private boolean timingsMatch(GenerationTiming timing, GenerationTiming matchTiming) {
return
( matchTiming == GenerationTiming.INSERT && timing.includesInsert() ) ||
( matchTiming == GenerationTiming.ALWAYS && timing.includesUpdate() );
}
public GenerationStrategyPair buildPair() {
if ( hadInMemoryGeneration && hadInDatabaseGeneration ) {
throw new ValueGenerationStrategyException(
"Composite attribute [" + mappingProperty.getName() + "] contained both in-memory"
+ " and in-database value generation"
);
}
else if ( hadInMemoryGeneration ) {
throw new NotYetImplementedException( "Still need to wire in composite in-memory value generation" );
}
else if ( hadInDatabaseGeneration ) {
final Component composite = (Component) mappingProperty.getValue();
// we need the numbers to match up so we can properly handle 'referenced sql column values'
if ( inDatabaseStrategies.size() != composite.getPropertySpan() ) {
throw new ValueGenerationStrategyException(
"Internal error : mismatch between number of collected in-db generation strategies" +
" and number of attributes for composite attribute : " + mappingProperty.getName()
);
}
// the base-line values for the aggregated InDatabaseValueGenerationStrategy we will build here.
GenerationTiming timing = GenerationTiming.INSERT;
boolean referenceColumns = false;
String[] columnValues = new String[ composite.getColumnSpan() ];
// start building the aggregate values
int propertyIndex = -1;
int columnIndex = 0;
Iterator subProperties = composite.getPropertyIterator();
while ( subProperties.hasNext() ) {
propertyIndex++;
final Property subProperty = (Property) subProperties.next();
final InDatabaseValueGenerationStrategy subStrategy = inDatabaseStrategies.get( propertyIndex );
if ( subStrategy.getGenerationTiming() == GenerationTiming.ALWAYS ) {
// override the base-line to the more often "ALWAYS"...
timing = GenerationTiming.ALWAYS;
}
if ( subStrategy.referenceColumnsInSql() ) {
// override base-line value
referenceColumns = true;
}
if ( subStrategy.getReferencedColumnValues() != null ) {
if ( subStrategy.getReferencedColumnValues().length != subProperty.getColumnSpan() ) {
throw new ValueGenerationStrategyException(
"Internal error : mismatch between number of collected 'referenced column values'" +
" and number of columns for composite attribute : " + mappingProperty.getName() +
'.' + subProperty.getName()
);
}
System.arraycopy(
subStrategy.getReferencedColumnValues(),
0,
columnValues,
columnIndex,
subProperty.getColumnSpan()
);
}
}
// then use the aggregated values to build the InDatabaseValueGenerationStrategy
return new GenerationStrategyPair(
new InDatabaseValueGenerationStrategyImpl( timing, referenceColumns, columnValues )
);
}
else {
return NO_GEN_PAIR;
}
}
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() );
}
}
public GenerationTiming getGenerationTiming() {
return GenerationTiming.ALWAYS;
}
public GenerationTiming getGenerationTiming() {
return GenerationTiming.ALWAYS;
}