Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion documentation/manual/jpa.textile
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>==[Property][Comparator]And?==</code> where Comparator can be the following:
Simple queries follow the following syntax <code>==[Property][Comparator]And?[Orderdirective]?==</code> where Comparator can be the following:

* @LessThan@ - less than the given value
* @LessThanEquals@ - less than or equal a give value
Expand All @@ -133,6 +135,15 @@ Simple queries follow the following syntax <code>==[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:
Expand Down
18 changes: 17 additions & 1 deletion framework/src/play/db/jpa/JPQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public Query bindParameters(Query q, Map<String,Object> params) {
}
return q;
}

public String findByToJPQL(String findBy) {
return findByToJPQL(JPA.DEFAULT, findBy);
}
Expand Down Expand Up @@ -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();
}

Expand Down
58 changes: 58 additions & 0 deletions framework/test-src/play/db/jpa/JPQLTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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 testFindBy() {
String query = "ByName";
String result = jpql.findByToJPQL(query);
assertTrue(result.equals("name = ?1"));
}

@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"));

}

}