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 * Boston, MA 02110-1301 USA
*/ */
package org.hibernate.annotations; package org.hibernate.annotations;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
/** /**
* Whether or not update entity's version on property's change * 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 srategy (default) * If the annotation is not present, the property is involved in the optimistic lock strategy (default).
* *
* @author Logi Ragnarsson * @author Logi Ragnarsson
*/ */
@Target( { ElementType.METHOD, ElementType.FIELD }) @Target( { ElementType.METHOD, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
public @interface OptimisticLock { 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(); boolean excluded();
} }

View File

@ -27,6 +27,8 @@ import org.hibernate.mapping.PropertyGeneration;
/** /**
* @author Gail Badner * @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 { public interface SimpleAttributeBindingState extends AttributeBindingState {
boolean isInsertable(); boolean isInsertable();

View File

@ -48,24 +48,64 @@ import org.hibernate.metamodel.source.annotations.util.JandexHelper;
* @author Hardy Ferentschik * @author Hardy Ferentschik
*/ */
public class MappedAttribute implements Comparable<MappedAttribute> { public class MappedAttribute implements Comparable<MappedAttribute> {
/**
* Annotations defined on the attribute, keyed against the annotation dot name.
*/
private final Map<DotName, List<AnnotationInstance>> annotations; private final Map<DotName, List<AnnotationInstance>> annotations;
/**
* The property name.
*/
private final String name; private final String name;
/**
* The property type as string.
*/
private final String type; private final String type;
/**
* Optional type parameters for custom types.
*/
private final Map<String, String> typeParameters; private final Map<String, String> typeParameters;
/**
* Is this property an id property (or part thereof).
*/
private final boolean isId; private final boolean isId;
/**
* Is this a versioned property (annotated w/ {@code @Version}.
*/
private final boolean isVersioned; private final boolean isVersioned;
/**
* Is this property a discriminator property.
*/
private final boolean isDiscriminator; 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; private boolean isLazy = false;
/**
* Is this property optional (see {@link javax.persistence.Basic}).
*/
private boolean isOptional = true; private boolean isOptional = true;
private PropertyGeneration propertyGeneration; private PropertyGeneration propertyGeneration;
private boolean isInsertable = true; private boolean isInsertable = true;
private boolean isUpdatable = true; private boolean isUpdatable = true;
/**
* Defines the column values (relational values) for this property.
*/
private final ColumnValues columnValues; private final ColumnValues columnValues;
static MappedAttribute createMappedAttribute(String name, String type, Map<DotName, List<AnnotationInstance>> annotations) { 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 ); columnValues.setNullable( false );
} }
this.isOptimisticLockable = checkOptimisticLockAnnotation();
checkBasicAnnotation(); checkBasicAnnotation();
checkGeneratedAnnotation(); checkGeneratedAnnotation();
} }
@ -185,6 +227,10 @@ public class MappedAttribute implements Comparable<MappedAttribute> {
return propertyGeneration; return propertyGeneration;
} }
public boolean isOptimisticLockable() {
return isOptimisticLockable;
}
/** /**
* Returns the annotation with the specified name or {@code null} * 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(); 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() { private void checkBasicAnnotation() {
AnnotationInstance basicAnnotation = getIfExists( JPADotNames.BASIC ); AnnotationInstance basicAnnotation = getIfExists( JPADotNames.BASIC );
if ( basicAnnotation != null ) { 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. * Implementation of the attribute binding state via annotation configuration.
* *
* @author Hardy Ferentschik * @author Hardy Ferentschik
* @todo in the end we can maybe just let MappedAttribute implement SimpleAttributeBindingState. (HF)
*/ */
public class AttributeBindingStateImpl implements SimpleAttributeBindingState { public class AttributeBindingStateImpl implements SimpleAttributeBindingState {
private final MappedAttribute mappedAttribute; private final MappedAttribute mappedAttribute;
private final String typeName;
private final Map<String, String> typeParameters;
public AttributeBindingStateImpl(MappedAttribute mappedAttribute) { public AttributeBindingStateImpl(MappedAttribute mappedAttribute) {
this.mappedAttribute = mappedAttribute; this.mappedAttribute = mappedAttribute;
typeName = mappedAttribute.getType();
typeParameters = mappedAttribute.getTypeParameters();
} }
@Override @Override
@ -68,12 +65,12 @@ public class AttributeBindingStateImpl implements SimpleAttributeBindingState {
@Override @Override
public String getTypeName() { public String getTypeName() {
return typeName; return mappedAttribute.getType();
} }
@Override @Override
public Map<String, String> getTypeParameters() { public Map<String, String> getTypeParameters() {
return typeParameters; return mappedAttribute.getTypeParameters();
} }
@Override @Override
@ -81,6 +78,11 @@ public class AttributeBindingStateImpl implements SimpleAttributeBindingState {
return mappedAttribute.isLazy(); return mappedAttribute.isLazy();
} }
@Override
public boolean isOptimisticLockable() {
return mappedAttribute.isOptimisticLockable();
}
@Override @Override
public boolean isKeyCascadeDeleteEnabled() { public boolean isKeyCascadeDeleteEnabled() {
return false; //To change body of implemented methods use File | Settings | File Templates. 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. 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 @Override
public String getNodeName() { public String getNodeName() {
return null; //To change body of implemented methods use File | Settings | File Templates. return null; //To change body of implemented methods use File | Settings | File Templates.