diff --git a/documentation/manual/jpa.textile b/documentation/manual/jpa.textile
index 6c2c08b8c0..2aa58f7d1a 100644
--- a/documentation/manual/jpa.textile
+++ b/documentation/manual/jpa.textile
@@ -118,8 +118,10 @@ bc. Post.find("byTitle", "My first post").fetch();
Post.find("byTitleLike", "%hello%").fetch();
Post.find("byAuthorIsNull").fetch();
Post.find("byTitleLikeAndAuthor", "%hello%", connectedUser).fetch();
+Post.find("byTitleOrderByTitle", "A nice post").fetch();
+Post.find("byTitleOrderByNbCommentsDesc", "A nice post").fetch();
-Simple queries follow the following syntax ==[Property][Comparator]And?== where Comparator can be the following:
+Simple queries follow the following syntax ==[Property][Comparator]And?[Orderdirective]?== where Comparator can be the following:
* @LessThan@ - less than the given value
* @LessThanEquals@ - less than or equal a give value
@@ -133,6 +135,15 @@ Simple queries follow the following syntax ==[Property][Comparator]And?==<
* @IsNotNull@ - Not a null value (doesn't require an argument)
* @IsNull@ - Is a null value (doesn't require an argument)
+and Orderdirective can be (e.g for a property named @name@) :
+
+* @OrderByName@ - default ascending order
+* @OrderByNameDesc@ - descending order
+
+you can also use several attributes in Orderdirective :
+* @OrderByNameAndAge@
+* @OrderByNameAndAgeDesc@
+
h3. Find using a JPQL query
You can use a JPQL query:
diff --git a/framework/src/play/db/jpa/JPQL.java b/framework/src/play/db/jpa/JPQL.java
index 7f61b9ec30..ea4293c682 100644
--- a/framework/src/play/db/jpa/JPQL.java
+++ b/framework/src/play/db/jpa/JPQL.java
@@ -250,7 +250,7 @@ public String findByToJPQL(String dbName, String findBy) {
StringBuilder jpql = new StringBuilder();
String subRequest;
if (findBy.contains("OrderBy"))
- subRequest = findBy.split("OrderBy")[0];
+ subRequest = findBy.split("OrderBy")[0];
else subRequest = findBy;
String[] parts = subRequest.split("And");
int index = 1;
@@ -309,6 +309,22 @@ public String findByToJPQL(String dbName, String findBy) {
jpql.append(" AND ");
}
}
+ // ORDER BY clause
+ if (findBy.contains("OrderBy")) {
+ jpql.append(" ORDER BY ");
+ String orderQuery = findBy.split("OrderBy")[1];
+ parts = orderQuery.split("And");
+ for (int i = 0; i < parts.length; i++) {
+ String part = parts[i];
+ String orderProp;
+ if (part.endsWith("Desc"))
+ orderProp = extractProp(part, "Desc") + " DESC";
+ else orderProp = part.toLowerCase();
+ if (i > 0)
+ jpql.append(", ");
+ jpql.append(orderProp);
+ }
+ }
return jpql.toString();
}
diff --git a/framework/test-src/play/db/jpa/JPQLTest.java b/framework/test-src/play/db/jpa/JPQLTest.java
new file mode 100644
index 0000000000..a6f037c7e3
--- /dev/null
+++ b/framework/test-src/play/db/jpa/JPQLTest.java
@@ -0,0 +1,52 @@
+package play.db.jpa;
+
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.util.Properties;
+import java.util.Set;
+
+import static play.db.jpa.JPQL.extractProp;
+
+import static org.junit.Assert.*;
+
+public class JPQLTest {
+
+ static JPQL jpql;
+
+ @BeforeClass
+ public static void setup(){
+ jpql = new JPQL();
+ }
+
+ @Test
+ public void testOrder() {
+
+ String query = "ByNameOrderByName";
+ String result = jpql.findByToJPQL(query);
+ assertTrue(result.endsWith(" ORDER BY name"));
+
+ query = "ByNameOrderByNameAndAge";
+ result = jpql.findByToJPQL(query);
+ assertTrue(result.endsWith(" ORDER BY name, age"));
+
+ query = "ByNameOrderByNameDesc";
+ result = jpql.findByToJPQL(query);
+ assertTrue(result.endsWith(" ORDER BY name DESC"));
+
+ query = "ByNameOrderByNameDescAndAge";
+ result = jpql.findByToJPQL(query);
+ assertTrue(result.endsWith(" ORDER BY name DESC, age"));
+
+ query = "ByNameOrderByNameAndAgeDesc";
+ result = jpql.findByToJPQL(query);
+ assertTrue(result.endsWith(" ORDER BY name, age DESC"));
+
+ query = "ByNameOrderByNameDescAndAgeDesc";
+ result = jpql.findByToJPQL(query);
+ assertTrue(result.endsWith(" ORDER BY name DESC, age DESC"));
+
+ }
+
+}
\ No newline at end of file