Skip to content

Commit

Permalink
HHH-7037 pushing attribute nature into MappedAttribute and creating e…
Browse files Browse the repository at this point in the history
…mpty source impls for *toMany sources
  • Loading branch information
hferentschik committed Mar 5, 2012
1 parent 1657f9f commit 3aaed02
Show file tree
Hide file tree
Showing 10 changed files with 354 additions and 75 deletions.
Expand Up @@ -31,7 +31,6 @@
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.Index;
import org.jboss.jandex.Indexer;
import org.jboss.logging.Logger;

import org.hibernate.AssertionFailure;
import org.hibernate.HibernateException;
Expand Down Expand Up @@ -60,8 +59,6 @@
* @author Steve Ebersole
*/
public class AnnotationMetadataSourceProcessorImpl implements MetadataSourceProcessor {
private static final Logger LOG = Logger.getLogger( AnnotationMetadataSourceProcessorImpl.class );

private final MetadataImplementor metadata;

private AnnotationBindingContext bindingContext;
Expand Down
Expand Up @@ -54,7 +54,6 @@
* @author Hardy Ferentschik
*/
public class AssociationAttribute extends MappedAttribute {
private final AttributeNature associationNature;
private final boolean ignoreNotFound;
private final String referencedEntityType;
private final String mappedBy;
Expand Down Expand Up @@ -90,17 +89,16 @@ public static AssociationAttribute createAssociationAttribute(String name,

AssociationAttribute(String name,
Class<?> javaType,
AttributeNature associationType,
AttributeNature attributeNature,
String accessType,
Map<DotName, List<AnnotationInstance>> annotations,
EntityBindingContext context) {
super( name, javaType, accessType, annotations, context );
this.associationNature = associationType;
super( name, javaType, attributeNature, accessType, annotations, context );
this.ignoreNotFound = ignoreNotFound();

AnnotationInstance associationAnnotation = JandexHelper.getSingleAnnotation(
annotations,
associationType.getAnnotationDotName()
attributeNature.getAnnotationDotName()
);

// using jandex we don't really care which exact type of annotation we are dealing with
Expand Down Expand Up @@ -129,10 +127,6 @@ public String getMappedBy() {
return mappedBy;
}

public AttributeNature getAssociationNature() {
return associationNature;
}

public Set<CascadeType> getCascadeTypes() {
return cascadeTypes;
}
Expand Down Expand Up @@ -324,8 +318,8 @@ private String determineMapsId() {
return null;
}

if ( !( AttributeNature.MANY_TO_ONE.equals( getAssociationNature() ) || AttributeNature.MANY_TO_ONE
.equals( getAssociationNature() ) ) ) {
if ( !( AttributeNature.MANY_TO_ONE.equals( getAttributeNature() ) || AttributeNature.MANY_TO_ONE
.equals( getAttributeNature() ) ) ) {
throw new MappingException(
"@MapsId can only be specified on a many-to-one or one-to-one associations",
getContext().getOrigin()
Expand Down
Expand Up @@ -25,6 +25,7 @@

import org.jboss.jandex.DotName;

import org.hibernate.metamodel.internal.source.annotations.HibernateDotNames;
import org.hibernate.metamodel.internal.source.annotations.JPADotNames;

/**
Expand All @@ -38,6 +39,7 @@ public enum AttributeNature {
ONE_TO_MANY( JPADotNames.ONE_TO_MANY ),
MANY_TO_ONE( JPADotNames.MANY_TO_ONE ),
MANY_TO_MANY( JPADotNames.MANY_TO_MANY ),
MANY_TO_ANY( HibernateDotNames.MANY_TO_ANY ),
ELEMENT_COLLECTION( JPADotNames.ELEMENT_COLLECTION ),
EMBEDDED_ID( JPADotNames.EMBEDDED_ID ),
EMBEDDED( JPADotNames.EMBEDDED );
Expand Down
Expand Up @@ -93,23 +93,24 @@ public class BasicAttribute extends MappedAttribute {

private final String customWriteFragment;
private final String customReadFragment;
private final String checkCondition;
private AttributeTypeResolver resolver;

public static BasicAttribute createSimpleAttribute(String name,
Class<?> attributeType,
AttributeNature attributeNature,
Map<DotName, List<AnnotationInstance>> annotations,
String accessType,
EntityBindingContext context) {
return new BasicAttribute( name, attributeType, accessType, annotations, context );
return new BasicAttribute( name, attributeType, attributeNature, accessType, annotations, context );
}

BasicAttribute(String name,
Class<?> attributeType,
AttributeNature attributeNature,
String accessType,
Map<DotName, List<AnnotationInstance>> annotations,
EntityBindingContext context) {
super( name, attributeType, accessType, annotations, context );
super( name, attributeType, attributeNature, accessType, annotations, context );

AnnotationInstance versionAnnotation = JandexHelper.getSingleAnnotation( annotations, JPADotNames.VERSION );
isVersioned = versionAnnotation != null;
Expand Down Expand Up @@ -149,7 +150,7 @@ public static BasicAttribute createSimpleAttribute(String name,
String[] readWrite = createCustomReadWrite( columnTransformerAnnotations );
this.customReadFragment = readWrite[0];
this.customWriteFragment = readWrite[1];
this.checkCondition = parseCheckAnnotation();

}

public boolean isVersioned() {
Expand Down Expand Up @@ -184,10 +185,6 @@ public String getCustomReadFragment() {
return customReadFragment;
}

public String getCheckCondition() {
return checkCondition;
}

public IdGenerator getIdGenerator() {
return idGenerator;
}
Expand Down Expand Up @@ -291,15 +288,6 @@ private String[] createCustomReadWrite(List<AnnotationInstance> columnTransforme
return readWrite;
}

private String parseCheckAnnotation() {
String checkCondition = null;
AnnotationInstance checkAnnotation = JandexHelper.getSingleAnnotation( annotations(), HibernateDotNames.CHECK );
if ( checkAnnotation != null ) {
checkCondition = checkAnnotation.value( "constraints" ).toString();
}
return checkCondition;
}

private IdGenerator checkGeneratedValueAnnotation() {
IdGenerator generator = null;
AnnotationInstance generatedValueAnnotation = JandexHelper.getSingleAnnotation(
Expand Down
Expand Up @@ -25,10 +25,9 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

import org.hibernate.FetchMode;
import org.hibernate.annotations.common.reflection.java.JavaXMember;
import org.hibernate.engine.spi.CascadeStyle;
import org.hibernate.metamodel.spi.binding.CascadeType;
import org.hibernate.metamodel.spi.source.ManyToManyPluralAttributeElementSource;
Expand All @@ -38,11 +37,10 @@
/**
* @author Hardy Ferentschik
*/
public class ManyToManyPluralAttributeElementSourceImpl
implements ManyToManyPluralAttributeElementSource {
private final CollectionAssociationAttribute associationAttribute;
public class ManyToManyPluralAttributeElementSourceImpl implements ManyToManyPluralAttributeElementSource {
private final PluralAssociationAttribute associationAttribute;

public ManyToManyPluralAttributeElementSourceImpl(CollectionAssociationAttribute associationAttribute) {
public ManyToManyPluralAttributeElementSourceImpl(PluralAssociationAttribute associationAttribute) {
this.associationAttribute = associationAttribute;
}

Expand All @@ -59,7 +57,13 @@ public String getReferencedEntityAttributeName() {

@Override
public Collection<String> getReferencedColumnNames() {
return null; //To change body of implemented methods use File | Settings | File Templates.
HashSet<String> referencedColumnNames = new HashSet<String>();
for ( Column column : associationAttribute.getColumnValues() ) {
if ( column.getReferencedColumnName() != null ) {
referencedColumnNames.add( column.getReferencedColumnName() );
}
}
return referencedColumnNames;
}

@Override
Expand Down
Expand Up @@ -59,6 +59,11 @@ public abstract class MappedAttribute implements Comparable<MappedAttribute> {
*/
private final Class<?> attributeType;

/**
* The nature of the attribute
*/
AttributeNature attributeNature;

/**
* The access type for this property. At the moment this is either 'field' or 'property', but Hibernate
* also allows custom named accessors (see {@link org.hibernate.property.PropertyAccessorFactory}).
Expand All @@ -82,16 +87,23 @@ public abstract class MappedAttribute implements Comparable<MappedAttribute> {
*/
private final boolean isOptimisticLockable;

/**
* Contains the SQL check condition specified via {@link org.hibernate.annotations.Check} or null if no annotation
* is specified.
*/
private final String checkCondition;

/**
* The binding context
*/
private final EntityBindingContext context;

MappedAttribute(String name, Class<?> attributeType, String accessType, Map<DotName, List<AnnotationInstance>> annotations, EntityBindingContext context) {
MappedAttribute(String name, Class<?> attributeType, AttributeNature attributeNature, String accessType, Map<DotName, List<AnnotationInstance>> annotations, EntityBindingContext context) {
this.context = context;
this.annotations = annotations;
this.name = name;
this.attributeType = attributeType;
this.attributeNature = attributeNature;
this.accessType = accessType;

//if this attribute has either @Id or @EmbeddedId, then it is an id attribute
Expand All @@ -101,7 +113,9 @@ public abstract class MappedAttribute implements Comparable<MappedAttribute> {
JPADotNames.EMBEDDED_ID
);
this.isId = ( idAnnotation != null || embeddedIdAnnotation != null );

this.isOptimisticLockable = checkOptimisticLockAnnotation();
this.checkCondition = checkCheckAnnotation();
checkColumnAnnotations( annotations );
}

Expand Down Expand Up @@ -137,6 +151,14 @@ public boolean isOptimisticLockable() {
return isOptimisticLockable;
}

public AttributeNature getAttributeNature() {
return attributeNature;
}

public String getCheckCondition() {
return checkCondition;
}

@Override
public int compareTo(MappedAttribute mappedProperty) {
return name.compareTo( mappedProperty.getName() );
Expand Down Expand Up @@ -224,7 +246,15 @@ private void checkColumnAnnotations(Map<DotName, List<AnnotationInstance>> annot
columnValues.add( new Column( annotation ) );
}
}
}

private String checkCheckAnnotation() {
String checkCondition = null;
AnnotationInstance checkAnnotation = JandexHelper.getSingleAnnotation( annotations(), HibernateDotNames.CHECK );
if ( checkAnnotation != null ) {
checkCondition = checkAnnotation.value( "constraints" ).toString();
}
return checkCondition;
}
}

Expand Down
Expand Up @@ -37,23 +37,24 @@
import org.hibernate.metamodel.internal.source.annotations.entity.EntityBindingContext;

/**
* Represents an collection association attribute.
* Represents an collection (collection, list, set, map) association attribute.
*
* @author Hardy Ferentschik
*/
public class CollectionAssociationAttribute extends AssociationAttribute {
public class PluralAssociationAttribute extends AssociationAttribute {
private final String whereClause;
private final String orderBy;

// Used for the non-owning side of a ManyToMany relationship
private final String inverseForeignKeyName;

public static CollectionAssociationAttribute createPluralAssociationAttribute(String name,
Class<?> attributeType,
AttributeNature attributeNature,
String accessType,
Map<DotName, List<AnnotationInstance>> annotations,
EntityBindingContext context) {
return new CollectionAssociationAttribute(
public static PluralAssociationAttribute createPluralAssociationAttribute(String name,
Class<?> attributeType,
AttributeNature attributeNature,
String accessType,
Map<DotName, List<AnnotationInstance>> annotations,
EntityBindingContext context) {
return new PluralAssociationAttribute(
name,
attributeType,
attributeNature,
Expand All @@ -63,17 +64,28 @@ public static CollectionAssociationAttribute createPluralAssociationAttribute(St
);
}

private CollectionAssociationAttribute(String name,
Class<?> javaType,
AttributeNature associationType,
String accessType,
Map<DotName, List<AnnotationInstance>> annotations,
EntityBindingContext context) {
public String getWhereClause() {
return whereClause;
}

public String getOrderBy() {
return orderBy;
}

public String getInverseForeignKeyName() {
return inverseForeignKeyName;
}

private PluralAssociationAttribute(String name,
Class<?> javaType,
AttributeNature associationType,
String accessType,
Map<DotName, List<AnnotationInstance>> annotations,
EntityBindingContext context) {
super( name, javaType, associationType, accessType, annotations, context );
this.whereClause = determineWereClause();
this.orderBy = determineOrderBy();
this.inverseForeignKeyName = determineInverseForeignKeyName();

}

private String determineInverseForeignKeyName() {
Expand Down Expand Up @@ -137,18 +149,6 @@ private String determineOrderBy() {

return orderBy;
}

public String getWhereClause() {
return whereClause;
}

public String getOrderBy() {
return orderBy;
}

public String getInverseForeignKeyName() {
return inverseForeignKeyName;
}
}


0 comments on commit 3aaed02

Please sign in to comment.