HHH-6171 Implementing lazy, optional, property generation, insertable and updatable ofSimpleAttributeBindingState

This commit is contained in:
Hardy Ferentschik 2011-05-18 17:10:26 +02:00
parent d6b36fca5a
commit fc456e3756
4 changed files with 107 additions and 50 deletions

View File

@ -23,8 +23,12 @@
*/
package org.hibernate.metamodel.source.annotations.entity;
import java.util.List;
import java.util.Map;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationValue;
import org.jboss.jandex.DotName;
import org.hibernate.AnnotationException;
import org.hibernate.AssertionFailure;
@ -116,9 +120,10 @@ public class EntityBinder {
}
private void bindDiscriminatorColumn(EntityBinding entityBinding) {
MappedAttribute discriminatorAttribute = MappedAttribute.createDiscriminatorAttribute(
configuredClass.getClassInfo().annotations()
final Map<DotName, List<AnnotationInstance>> typeAnnotations = JandexHelper.getTypeAnnotations(
configuredClass.getClassInfo()
);
MappedAttribute discriminatorAttribute = MappedAttribute.createDiscriminatorAttribute( typeAnnotations );
bindSingleMappedAttribute( entityBinding, discriminatorAttribute );

View File

@ -27,6 +27,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.DiscriminatorType;
import javax.persistence.FetchType;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationValue;
@ -34,12 +35,14 @@ import org.jboss.jandex.DotName;
import org.hibernate.AnnotationException;
import org.hibernate.AssertionFailure;
import org.hibernate.annotations.GenerationTime;
import org.hibernate.mapping.PropertyGeneration;
import org.hibernate.metamodel.source.annotations.HibernateDotNames;
import org.hibernate.metamodel.source.annotations.JPADotNames;
import org.hibernate.metamodel.source.annotations.util.JandexHelper;
/**
* Represent a mapped attribute (explicitly or implicitly mapped). Also used for synthetic attributes likes a
* Represent a mapped attribute (explicitly or implicitly mapped). Also used for synthetic attributes like a
* discriminator column.
*
* @author Hardy Ferentschik
@ -52,25 +55,24 @@ public class MappedAttribute implements Comparable<MappedAttribute> {
private final String type;
private final Map<String, String> typeParameters;
private final ColumnValues columnValues;
private final boolean isId;
private final boolean isVersioned;
private final boolean isDiscriminator;
private boolean isLazy = false;
private boolean isOptional = true;
private PropertyGeneration propertyGeneration;
private boolean isInsertable = true;
private boolean isUpdatable = true;
private final ColumnValues columnValues;
static MappedAttribute createMappedAttribute(String name, String type, Map<DotName, List<AnnotationInstance>> annotations) {
return new MappedAttribute( name, type, annotations, false );
}
static MappedAttribute createDiscriminatorAttribute(Map<DotName, List<AnnotationInstance>> annotations) {
Map<DotName, List<AnnotationInstance>> discriminatorAnnotations = JandexHelper.filterAnnotations(
annotations,
JPADotNames.DISCRIMINATOR_COLUMN,
JPADotNames.DISCRIMINATOR_VALUE,
HibernateDotNames.DISCRIMINATOR_FORMULA,
HibernateDotNames.DISCRIMINATOR_OPTIONS
);
AnnotationInstance discriminatorOptionsAnnotation = JandexHelper.getSingleAnnotation(
annotations, JPADotNames.DISCRIMINATOR_COLUMN
);
@ -100,7 +102,7 @@ public class MappedAttribute implements Comparable<MappedAttribute> {
}
}
}
return new MappedAttribute( name, type, discriminatorAnnotations, true );
return new MappedAttribute( name, type, annotations, true );
}
private MappedAttribute(String name, String type, Map<DotName, List<AnnotationInstance>> annotations, boolean isDiscriminator) {
@ -130,6 +132,9 @@ public class MappedAttribute implements Comparable<MappedAttribute> {
columnValues.setUnique( true );
columnValues.setNullable( false );
}
checkBasicAnnotation();
checkGeneratedAnnotation();
}
public final String getName() {
@ -160,6 +165,26 @@ public class MappedAttribute implements Comparable<MappedAttribute> {
return isDiscriminator;
}
public boolean isLazy() {
return isLazy;
}
public boolean isOptional() {
return isOptional;
}
public boolean isInsertable() {
return isInsertable;
}
public boolean isUpdatable() {
return isUpdatable;
}
public PropertyGeneration getPropertyGeneration() {
return propertyGeneration;
}
/**
* Returns the annotation with the specified name or {@code null}
*
@ -227,6 +252,40 @@ public class MappedAttribute implements Comparable<MappedAttribute> {
return typeAnnotation.value( "type" ).asString();
}
private void checkBasicAnnotation() {
AnnotationInstance basicAnnotation = getIfExists( JPADotNames.BASIC );
if ( basicAnnotation != null ) {
FetchType fetchType = FetchType.LAZY;
AnnotationValue fetchValue = basicAnnotation.value( "fetch" );
if ( fetchValue != null ) {
fetchType = Enum.valueOf( FetchType.class, fetchValue.asEnum() );
}
this.isLazy = fetchType == FetchType.LAZY;
AnnotationValue optionalValue = basicAnnotation.value( "optional" );
if ( optionalValue != null ) {
this.isOptional = optionalValue.asBoolean();
}
}
}
// TODO - there is more todo for updatable and insertable. Checking the @Generated annotation is only one part (HF)
private void checkGeneratedAnnotation() {
AnnotationInstance generatedAnnotation = getIfExists( HibernateDotNames.GENERATED );
if ( generatedAnnotation != null ) {
this.isInsertable = false;
AnnotationValue generationTimeValue = generatedAnnotation.value();
if ( generationTimeValue != null ) {
GenerationTime genTime = Enum.valueOf( GenerationTime.class, generationTimeValue.asEnum() );
if ( GenerationTime.ALWAYS.equals( genTime ) ) {
this.isUpdatable = false;
this.propertyGeneration = PropertyGeneration.parse( genTime.toString().toLowerCase() );
}
}
}
}
}

View File

@ -37,7 +37,6 @@ import org.hibernate.metamodel.source.annotations.entity.MappedAttribute;
*/
public class AttributeBindingStateImpl implements SimpleAttributeBindingState {
private final MappedAttribute mappedAttribute;
private final PropertyGeneration propertyGeneration = null;
private final String typeName;
private final Map<String, String> typeParameters;
@ -54,35 +53,17 @@ public class AttributeBindingStateImpl implements SimpleAttributeBindingState {
@Override
public PropertyGeneration getPropertyGeneration() {
// GeneratedValue generatedValue = property.getAnnotation( GeneratedValue.class );
// String generatorType = generatedValue != null ?
// generatorType( generatedValue.strategy(), mappings ) :
// "assigned";
// String generatorName = generatedValue != null ?
// generatedValue.generator() :
// BinderHelper.ANNOTATION_STRING_DEFAULT;
return propertyGeneration;
return mappedAttribute.getPropertyGeneration();
}
@Override
public boolean isInsertable() {
return false; //To change body of implemented methods use File | Settings | File Templates.
return mappedAttribute.isInsertable();
}
@Override
public boolean isUpdatable() {
return false; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public boolean isKeyCascadeDeleteEnabled() {
return false; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public String getUnsavedValue() {
return null; //To change body of implemented methods use File | Settings | File Templates.
return mappedAttribute.isUpdatable();
}
@Override
@ -97,9 +78,19 @@ public class AttributeBindingStateImpl implements SimpleAttributeBindingState {
@Override
public boolean isLazy() {
return mappedAttribute.isLazy();
}
@Override
public boolean isKeyCascadeDeleteEnabled() {
return false; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public String getUnsavedValue() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public String getPropertyAccessorName() {
return null; //To change body of implemented methods use File | Settings | File Templates.

View File

@ -126,20 +126,6 @@ public class JandexHelper {
}
}
/**
* @param annotations List of annotation instances keyed against their dot name.
* @param annotationNames the annotation names to filter
*
* @return a new map of annotation instances only containing annotations of the specified dot names.
*/
public static Map<DotName, List<AnnotationInstance>> filterAnnotations(Map<DotName, List<AnnotationInstance>> annotations, DotName... annotationNames) {
Map<DotName, List<AnnotationInstance>> filteredAnnotations = new HashMap<DotName, List<AnnotationInstance>>();
for ( DotName name : annotationNames ) {
filteredAnnotations.put( name, annotations.get( name ) );
}
return filteredAnnotations;
}
/**
* Creates a jandex index for the specified classes
*
@ -202,6 +188,22 @@ public class JandexHelper {
return annotations;
}
public static Map<DotName, List<AnnotationInstance>> getTypeAnnotations(ClassInfo classInfo) {
if ( classInfo == null ) {
throw new IllegalArgumentException( "classInfo cannot be null" );
}
Map<DotName, List<AnnotationInstance>> annotations = new HashMap<DotName, List<AnnotationInstance>>();
for ( List<AnnotationInstance> annotationList : classInfo.annotations().values() ) {
for ( AnnotationInstance instance : annotationList ) {
if ( instance.target() instanceof ClassInfo ) {
addAnnotationToMap( instance, annotations );
}
}
}
return annotations;
}
private static void addAnnotationToMap(AnnotationInstance instance, Map<DotName, List<AnnotationInstance>> annotations) {
DotName dotName = instance.name();
List<AnnotationInstance> list;