HHH-6171 Mapping simple attribute (non id)

This commit is contained in:
Hardy Ferentschik 2011-05-03 18:07:14 +02:00
parent 46f5022b60
commit 658a06e5c3
4 changed files with 59 additions and 12 deletions

View File

@ -45,14 +45,14 @@ public final class ColumnValues {
private int precision = 0; private int precision = 0;
private int scale = 0; private int scale = 0;
public ColumnValues(AnnotationInstance columnAnnotation) { public ColumnValues(AnnotationInstance columnAnnotation, boolean isId) {
if ( columnAnnotation != null && !JPADotNames.COLUMN.equals( columnAnnotation.name() ) ) { if ( columnAnnotation != null && !JPADotNames.COLUMN.equals( columnAnnotation.name() ) ) {
throw new AssertionFailure( "A @Column annotation needs to be passed to the constructor" ); throw new AssertionFailure( "A @Column annotation needs to be passed to the constructor" );
} }
applyColumnValues( columnAnnotation ); applyColumnValues( columnAnnotation, isId );
} }
private void applyColumnValues(AnnotationInstance columnAnnotation) { private void applyColumnValues(AnnotationInstance columnAnnotation, boolean isId) {
if ( columnAnnotation == null ) { if ( columnAnnotation == null ) {
return; return;
} }
@ -62,14 +62,26 @@ public final class ColumnValues {
this.name = nameValue.asString(); this.name = nameValue.asString();
} }
AnnotationValue uniqueValue = columnAnnotation.value( "unique" ); // id attribute must be unique
if ( uniqueValue != null ) { if ( isId ) {
this.unique = nameValue.asBoolean(); this.unique = true;
}
else {
AnnotationValue uniqueValue = columnAnnotation.value( "unique" );
if ( uniqueValue != null ) {
this.unique = nameValue.asBoolean();
}
} }
AnnotationValue nullableValue = columnAnnotation.value( "nullable" ); // id attribute cannot be nullable
if ( nullableValue != null ) { if ( isId ) {
this.nullable = nullableValue.asBoolean(); this.nullable = false;
}
else {
AnnotationValue nullableValue = columnAnnotation.value( "nullable" );
if ( nullableValue != null ) {
this.nullable = nullableValue.asBoolean();
}
} }
AnnotationValue insertableValue = columnAnnotation.value( "insertable" ); AnnotationValue insertableValue = columnAnnotation.value( "insertable" );

View File

@ -68,6 +68,8 @@ public class EntityBinder {
if ( configuredClass.isRoot() ) { if ( configuredClass.isRoot() ) {
bindId( entityBinding ); bindId( entityBinding );
} }
bindAttributes( entityBinding );
meta.addEntity( entityBinding ); meta.addEntity( entityBinding );
} }
@ -164,7 +166,6 @@ public class EntityBinder {
typeDescriptor.setTypeName( idAttribute.getType().getName() ); typeDescriptor.setTypeName( idAttribute.getType().getName() );
domainState.typeDescriptor = typeDescriptor; domainState.typeDescriptor = typeDescriptor;
domainState.attribute = entityBinding.getEntity().getOrCreateSingularAttribute( idAttribute.getName() ); domainState.attribute = entityBinding.getEntity().getOrCreateSingularAttribute( idAttribute.getName() );
idBinding.initialize( domainState ); idBinding.initialize( domainState );
AttributeColumnRelationalState columnRelationsState = new AttributeColumnRelationalState( idAttribute, meta ); AttributeColumnRelationalState columnRelationsState = new AttributeColumnRelationalState( idAttribute, meta );
@ -173,6 +174,32 @@ public class EntityBinder {
idBinding.initializeSimpleTupleValue( relationalState ); idBinding.initializeSimpleTupleValue( relationalState );
} }
private void bindAttributes(EntityBinding entityBinding) {
for ( MappedAttribute mappedAttribute : configuredClass.getMappedAttributes() ) {
if ( mappedAttribute.isId() ) {
continue;
}
String attributeName = mappedAttribute.getName();
entityBinding.getEntity().getOrCreateSingularAttribute( attributeName );
SimpleAttributeBinding simpleBinding = entityBinding.makeSimpleAttributeBinding( attributeName );
AnnotationSimpleAttributeDomainState domainState = new AnnotationSimpleAttributeDomainState();
HibernateTypeDescriptor typeDescriptor = new HibernateTypeDescriptor();
typeDescriptor.setTypeName( mappedAttribute.getType().getName() );
domainState.typeDescriptor = typeDescriptor;
domainState.attribute = entityBinding.getEntity().getOrCreateSingularAttribute( attributeName );
simpleBinding.initialize( domainState );
AttributeColumnRelationalState columnRelationsState = new AttributeColumnRelationalState(
mappedAttribute, meta
);
AnnotationSimpleAttributeRelationalState relationalState = new AnnotationSimpleAttributeRelationalState();
relationalState.valueStates.add( columnRelationsState );
simpleBinding.initializeSimpleTupleValue( relationalState );
}
}
private void bindHibernateEntityAnnotation(EntityBinding entityBinding) { private void bindHibernateEntityAnnotation(EntityBinding entityBinding) {
AnnotationInstance hibernateEntityAnnotation = JandexHelper.getSingleAnnotation( AnnotationInstance hibernateEntityAnnotation = JandexHelper.getSingleAnnotation(
configuredClass.getClassInfo(), HibernateDotNames.ENTITY configuredClass.getClassInfo(), HibernateDotNames.ENTITY

View File

@ -42,18 +42,22 @@ public class MappedAttribute implements Comparable<MappedAttribute> {
private final Class<?> type; private final Class<?> type;
private final Map<DotName, List<AnnotationInstance>> annotations; private final Map<DotName, List<AnnotationInstance>> annotations;
private final ColumnValues columnValues; private final ColumnValues columnValues;
private final boolean isId;
MappedAttribute(String name, Class<?> type, Map<DotName, List<AnnotationInstance>> annotations) { MappedAttribute(String name, Class<?> type, Map<DotName, List<AnnotationInstance>> annotations) {
this.name = name; this.name = name;
this.type = type; this.type = type;
this.annotations = annotations; this.annotations = annotations;
List<AnnotationInstance> idAnnotations = annotations.get( JPADotNames.ID );
isId = idAnnotations != null && !idAnnotations.isEmpty();
List<AnnotationInstance> columnAnnotations = annotations.get( JPADotNames.COLUMN ); List<AnnotationInstance> columnAnnotations = annotations.get( JPADotNames.COLUMN );
if ( columnAnnotations != null && columnAnnotations.size() > 1 ) { if ( columnAnnotations != null && columnAnnotations.size() > 1 ) {
throw new AssertionFailure( "There can only be one @Column annotation per mapped attribute" ); throw new AssertionFailure( "There can only be one @Column annotation per mapped attribute" );
} }
AnnotationInstance columnAnnotation = columnAnnotations == null ? null : columnAnnotations.get( 0 ); AnnotationInstance columnAnnotation = columnAnnotations == null ? null : columnAnnotations.get( 0 );
columnValues = new ColumnValues( columnAnnotation ); columnValues = new ColumnValues( columnAnnotation, isId );
} }
public final String getName() { public final String getName() {
@ -68,6 +72,10 @@ public class MappedAttribute implements Comparable<MappedAttribute> {
return columnValues; return columnValues;
} }
public boolean isId() {
return isId;
}
public final List<AnnotationInstance> annotations(DotName annotationDotName) { public final List<AnnotationInstance> annotations(DotName annotationDotName) {
if ( annotations.containsKey( annotationDotName ) ) { if ( annotations.containsKey( annotationDotName ) ) {
return annotations.get( annotationDotName ); return annotations.get( annotationDotName );

View File

@ -38,7 +38,7 @@ import org.hibernate.testing.FailureExpected;
*/ */
public class BasicAnnotationBindingTests extends AbstractBasicBindingTests { public class BasicAnnotationBindingTests extends AbstractBasicBindingTests {
@FailureExpected(jiraKey = "HHH-5672", message = "Work in progress") //@FailureExpected(jiraKey = "HHH-5672", message = "Work in progress")
@Test @Test
public void testSimpleEntityMapping() { public void testSimpleEntityMapping() {
super.testSimpleEntityMapping(); super.testSimpleEntityMapping();