HHH-6171 Binding OptimisticLock annotation

This commit is contained in:
Hardy Ferentschik 2011-05-19 15:01:55 +02:00
parent fc456e3756
commit 6aee9365ee
4 changed files with 72 additions and 18 deletions

View File

@ -22,24 +22,23 @@
* Boston, MA 02110-1301 USA
*/
package org.hibernate.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Whether or not update entity's version on property's change
* If the annotation is not present, the property is involved in the optimistic lock srategy (default)
* Whether or not a change of the annotated property will trigger a entity version increment.
* If the annotation is not present, the property is involved in the optimistic lock strategy (default).
*
* @author Logi Ragnarsson
*/
@Target( {ElementType.METHOD, ElementType.FIELD} )
@Retention( RetentionPolicy.RUNTIME )
@Target( { ElementType.METHOD, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
public @interface OptimisticLock {
/**
* If true, the annotated property change will not trigger a version upgrade
* @return If {@code true}, the annotated property change will not trigger a version increment.
*/
boolean excluded();
}

View File

@ -27,6 +27,8 @@ import org.hibernate.mapping.PropertyGeneration;
/**
* @author Gail Badner
* @todo - We need to get a better split into the states. For example this SimpleAttributeBindingState contains
* state which is only relevant for primary/foreign keys. This should be in a different interface. (HF)
*/
public interface SimpleAttributeBindingState extends AttributeBindingState {
boolean isInsertable();

View File

@ -48,24 +48,64 @@ import org.hibernate.metamodel.source.annotations.util.JandexHelper;
* @author Hardy Ferentschik
*/
public class MappedAttribute implements Comparable<MappedAttribute> {
/**
* Annotations defined on the attribute, keyed against the annotation dot name.
*/
private final Map<DotName, List<AnnotationInstance>> annotations;
/**
* The property name.
*/
private final String name;
/**
* The property type as string.
*/
private final String type;
/**
* Optional type parameters for custom types.
*/
private final Map<String, String> typeParameters;
/**
* Is this property an id property (or part thereof).
*/
private final boolean isId;
/**
* Is this a versioned property (annotated w/ {@code @Version}.
*/
private final boolean isVersioned;
/**
* Is this property a discriminator property.
*/
private final boolean isDiscriminator;
/**
* Whether a change of the property's value triggers a version increment of the entity (in case of optimistic
* locking).
*/
private final boolean isOptimisticLockable;
/**
* Is this property lazy loaded (see {@link javax.persistence.Basic}).
*/
private boolean isLazy = false;
/**
* Is this property optional (see {@link javax.persistence.Basic}).
*/
private boolean isOptional = true;
private PropertyGeneration propertyGeneration;
private boolean isInsertable = true;
private boolean isUpdatable = true;
/**
* Defines the column values (relational values) for this property.
*/
private final ColumnValues columnValues;
static MappedAttribute createMappedAttribute(String name, String type, Map<DotName, List<AnnotationInstance>> annotations) {
@ -133,6 +173,8 @@ public class MappedAttribute implements Comparable<MappedAttribute> {
columnValues.setNullable( false );
}
this.isOptimisticLockable = checkOptimisticLockAnnotation();
checkBasicAnnotation();
checkGeneratedAnnotation();
}
@ -185,6 +227,10 @@ public class MappedAttribute implements Comparable<MappedAttribute> {
return propertyGeneration;
}
public boolean isOptimisticLockable() {
return isOptimisticLockable;
}
/**
* Returns the annotation with the specified name or {@code null}
*
@ -253,6 +299,16 @@ public class MappedAttribute implements Comparable<MappedAttribute> {
return typeAnnotation.value( "type" ).asString();
}
private boolean checkOptimisticLockAnnotation() {
boolean triggersVersionIncrement = true;
AnnotationInstance optimisticLockAnnotation = getIfExists( HibernateDotNames.OPTIMISTIC_LOCK );
if ( optimisticLockAnnotation != null ) {
boolean exclude = optimisticLockAnnotation.value( "excluded" ).asBoolean();
triggersVersionIncrement = !exclude;
}
return triggersVersionIncrement;
}
private void checkBasicAnnotation() {
AnnotationInstance basicAnnotation = getIfExists( JPADotNames.BASIC );
if ( basicAnnotation != null ) {

View File

@ -34,16 +34,13 @@ import org.hibernate.metamodel.source.annotations.entity.MappedAttribute;
* Implementation of the attribute binding state via annotation configuration.
*
* @author Hardy Ferentschik
* @todo in the end we can maybe just let MappedAttribute implement SimpleAttributeBindingState. (HF)
*/
public class AttributeBindingStateImpl implements SimpleAttributeBindingState {
private final MappedAttribute mappedAttribute;
private final String typeName;
private final Map<String, String> typeParameters;
public AttributeBindingStateImpl(MappedAttribute mappedAttribute) {
this.mappedAttribute = mappedAttribute;
typeName = mappedAttribute.getType();
typeParameters = mappedAttribute.getTypeParameters();
}
@Override
@ -68,12 +65,12 @@ public class AttributeBindingStateImpl implements SimpleAttributeBindingState {
@Override
public String getTypeName() {
return typeName;
return mappedAttribute.getType();
}
@Override
public Map<String, String> getTypeParameters() {
return typeParameters;
return mappedAttribute.getTypeParameters();
}
@Override
@ -81,6 +78,11 @@ public class AttributeBindingStateImpl implements SimpleAttributeBindingState {
return mappedAttribute.isLazy();
}
@Override
public boolean isOptimisticLockable() {
return mappedAttribute.isOptimisticLockable();
}
@Override
public boolean isKeyCascadeDeleteEnabled() {
return false; //To change body of implemented methods use File | Settings | File Templates.
@ -106,11 +108,6 @@ public class AttributeBindingStateImpl implements SimpleAttributeBindingState {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public boolean isOptimisticLockable() {
return false; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public String getNodeName() {
return null; //To change body of implemented methods use File | Settings | File Templates.