|
From: <hib...@li...> - 2006-06-19 03:32:12
|
Author: epbernard
Date: 2006-06-18 23:32:09 -0400 (Sun, 18 Jun 2006)
New Revision: 10025
Modified:
trunk/HibernateExt/metadata/src/java/org/hibernate/cfg/annotations/CollectionBinder.java
Log:
ANN-336
Modified: trunk/HibernateExt/metadata/src/java/org/hibernate/cfg/annotations/CollectionBinder.java
===================================================================
--- trunk/HibernateExt/metadata/src/java/org/hibernate/cfg/annotations/CollectionBinder.java 2006-06-16 02:49:06 UTC (rev 10024)
+++ trunk/HibernateExt/metadata/src/java/org/hibernate/cfg/annotations/CollectionBinder.java 2006-06-19 03:32:09 UTC (rev 10025)
@@ -710,6 +710,83 @@
return orderByString;
}
+ private static String buildOrderByClauseFromHql(String hqlOrderBy, Component component, String role) {
+ String orderByString = null;
+ if ( hqlOrderBy != null ) {
+ List<String> properties = new ArrayList<String>();
+ List<String> ordering = new ArrayList<String>();
+ StringBuilder orderByBuffer = new StringBuilder();
+ if ( "".equals( hqlOrderBy ) ) {
+ //TODO : Check that. Maybe order by key for maps
+ }
+ else {
+ StringTokenizer st = new StringTokenizer( hqlOrderBy, " ,", false );
+ String currentOrdering = null;
+ //FIXME make this code decent
+ while ( st.hasMoreTokens() ) {
+ String token = st.nextToken();
+ if ( isNonPropertyToken( token ) ) {
+ if ( currentOrdering != null ) {
+ throw new AnnotationException(
+ "Error while parsing HQL orderBy clause: " + hqlOrderBy
+ + " (" + role + ")"
+ );
+ }
+ currentOrdering = token;
+ }
+ else {
+ //Add ordering of the previous
+ if ( currentOrdering == null ) {
+ //default ordering
+ ordering.add( "asc" );
+ }
+ else {
+ ordering.add( currentOrdering );
+ currentOrdering = null;
+ }
+ properties.add( token );
+ }
+ }
+ ordering.remove( 0 ); //first one is the algorithm starter
+ // add last one ordering
+ if ( currentOrdering == null ) {
+ //default ordering
+ ordering.add( "asc" );
+ }
+ else {
+ ordering.add( currentOrdering );
+ currentOrdering = null;
+ }
+ int index = 0;
+
+ for ( String property : properties ) {
+ Property p = component.getProperty( property );
+ if ( p == null ) {
+ throw new AnnotationException(
+ "property from @OrderBy clause not found: "
+ + role + "." + property
+ );
+ }
+
+ Iterator propertyColumns = p.getColumnIterator();
+ while ( propertyColumns.hasNext() ) {
+ Column column = (Column) propertyColumns.next();
+ orderByBuffer.append( column.getName() )
+ .append( " " )
+ .append( ordering.get( index ) )
+ .append( ", " );
+ }
+ index++;
+ }
+
+ if ( orderByBuffer.length() >= 2 ) {
+ orderByString = orderByBuffer.substring( 0, orderByBuffer.length() - 2 );
+ }
+ }
+ }
+ return orderByString;
+ }
+
private static boolean isNonPropertyToken(String token) {
if ( " ".equals( token ) ) return true;
if ( ",".equals( token ) ) return true;
@@ -803,16 +880,7 @@
}
}
}
- if ( StringHelper.isNotEmpty( hqlOrderBy ) ) {
- if ( ! isCollectionOfEntities ) {
- collValue.setOrderBy( hqlOrderBy );
- }
- else {
- collValue.setManyToManyOrdering(
- buildOrderByClauseFromHql( hqlOrderBy, collectionEntity, collValue.getRole() )
- );
- }
- }
+
boolean mappedBy = ! AnnotationBinder.isDefault( joinColumns[0].getMappedBy() );
if ( mappedBy ) {
if ( ! isCollectionOfEntities ) {
@@ -897,6 +965,11 @@
element.setFetchMode( FetchMode.JOIN );
element.setLazy( false );
element.setIgnoreNotFound( ignoreNotFound );
+ if ( StringHelper.isNotEmpty( hqlOrderBy ) ) {
+ collValue.setManyToManyOrdering(
+ buildOrderByClauseFromHql( hqlOrderBy, collectionEntity, collValue.getRole() )
+ );
+ }
}
else {
XClass elementClass;
@@ -960,7 +1033,16 @@
entityBinder, false, false,
mappings
);
+
collValue.setElement( component );
+
+ if ( StringHelper.isNotEmpty( hqlOrderBy ) ) {
+ String path = collValue.getOwnerEntityName() + "." + joinColumns[0].getPropertyName();
+ String orderBy = buildOrderByClauseFromHql( hqlOrderBy, component, path );
+ if ( orderBy != null ) {
+ collValue.setOrderBy( orderBy );
+ }
+ }
}
else {
SimpleValueBinder elementBinder = new SimpleValueBinder();
|