HHH-7037 Dealing with @OrderBy

This commit is contained in:
Hardy Ferentschik 2012-02-09 12:16:57 -06:00
parent 3fc75c8f4d
commit f475dc096b
3 changed files with 46 additions and 23 deletions

View File

@ -29,26 +29,28 @@
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.DotName;
import org.hibernate.AnnotationException;
import org.hibernate.metamodel.internal.source.annotations.HibernateDotNames;
import org.hibernate.metamodel.internal.source.annotations.JPADotNames;
import org.hibernate.metamodel.internal.source.annotations.JandexHelper;
import org.hibernate.metamodel.internal.source.annotations.entity.EntityBindingContext;
/**
* Represents an association attribute.
* Represents an collection association attribute.
*
* @author Hardy Ferentschik
*/
public class PluralAssociationAttribute extends AssociationAttribute {
public class CollectionAssociationAttribute extends AssociationAttribute {
private final String whereClause;
private final String orderBy;
public static PluralAssociationAttribute createPluralAssociationAttribute(String name,
Class<?> attributeType,
AttributeNature attributeNature,
String accessType,
Map<DotName, List<AnnotationInstance>> annotations,
EntityBindingContext context) {
return new PluralAssociationAttribute(
public static CollectionAssociationAttribute createPluralAssociationAttribute(String name,
Class<?> attributeType,
AttributeNature attributeNature,
String accessType,
Map<DotName, List<AnnotationInstance>> annotations,
EntityBindingContext context) {
return new CollectionAssociationAttribute(
name,
attributeType,
attributeNature,
@ -58,12 +60,12 @@ public static PluralAssociationAttribute createPluralAssociationAttribute(String
);
}
private PluralAssociationAttribute(String name,
Class<?> javaType,
AttributeNature associationType,
String accessType,
Map<DotName, List<AnnotationInstance>> annotations,
EntityBindingContext context) {
private CollectionAssociationAttribute(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();
@ -83,12 +85,34 @@ private String determineWereClause() {
private String determineOrderBy() {
String orderBy = null;
AnnotationInstance whereAnnotation = JandexHelper.getSingleAnnotation(
AnnotationInstance hibernateWhereAnnotation = JandexHelper.getSingleAnnotation(
annotations(),
HibernateDotNames.ORDER_BY
);
if ( whereAnnotation != null ) {
orderBy = JandexHelper.getValue( whereAnnotation, "clause", String.class );
AnnotationInstance jpaWhereAnnotation = JandexHelper.getSingleAnnotation(
annotations(),
JPADotNames.ORDER_BY
);
if ( jpaWhereAnnotation != null && hibernateWhereAnnotation != null ) {
throw new AnnotationException(
"Cannot use sql order by clause (@org.hibernate.annotations.OrderBy) " +
"in conjunction with JPA order by clause (@java.persistence.OrderBy) on " + getName()
);
}
if ( hibernateWhereAnnotation != null ) {
orderBy = JandexHelper.getValue( hibernateWhereAnnotation, "clause", String.class );
}
if ( jpaWhereAnnotation != null ) {
// todo
// this could be an empty string according to JPA spec 11.1.38 -
// If the ordering element is not specified for an entity association, ordering by the primary key of the
// associated entity is assumed
// The binder will need to take this into account and generate the right property names
orderBy = JandexHelper.getValue( jpaWhereAnnotation, "value", String.class );
}
return orderBy;

View File

@ -35,9 +35,9 @@
* @author Hardy Ferentschik
*/
public class ManyToManyPluralAttributeElementSourceImpl implements ManyToManyPluralAttributeElementSource {
private final PluralAssociationAttribute associationAttribute;
private final CollectionAssociationAttribute associationAttribute;
public ManyToManyPluralAttributeElementSourceImpl(PluralAssociationAttribute associationAttribute) {
public ManyToManyPluralAttributeElementSourceImpl(CollectionAssociationAttribute associationAttribute) {
this.associationAttribute = associationAttribute;
}

View File

@ -54,7 +54,7 @@
import org.hibernate.EntityMode;
import org.hibernate.HibernateException;
import org.hibernate.cfg.NotYetImplementedException;
import org.hibernate.metamodel.internal.source.annotations.attribute.PluralAssociationAttribute;
import org.hibernate.metamodel.internal.source.annotations.attribute.CollectionAssociationAttribute;
import org.hibernate.metamodel.spi.source.MappingException;
import org.hibernate.metamodel.internal.source.annotations.AnnotationBindingContext;
import org.hibernate.metamodel.internal.source.annotations.HibernateDotNames;
@ -143,7 +143,6 @@ public ConfiguredClass(
this.parent = parent;
this.classInfo = classInfo;
this.clazz = context.locateClassByName( classInfo.toString() );
// this.configuredClassType = determineType();
this.classAccessType = determineClassAccessType( defaultAccessType );
this.customTuplizer = determineCustomTuplizer();
@ -480,7 +479,7 @@ else if ( attribute.isVersioned() ) {
}
case ONE_TO_MANY:
case MANY_TO_MANY: {
AssociationAttribute attribute = PluralAssociationAttribute.createPluralAssociationAttribute(
AssociationAttribute attribute = CollectionAssociationAttribute.createPluralAssociationAttribute(
attributeName,
attributeType,
attributeNature,