simple-jpa

simple-jpa is a Griffon's plugin for developing JPA and Swing based desktop application.

Download .zip Download .tar.gz View on GitHub

Dynamic finders

You can use dynamic finders to retrieve JPA entities. By default, finder methods can be used inside Griffon's controller, but you can also configure simple-jpa to inject finders to Griffon's services.

The simplest form of simple-jpa's finders is findAllxxx() where xxx is JPA entity name to retrieve. This method will always returns a List which contains all xxx from database. If no records are found, this method will return an empty List.

List result = findAllStudent()
result.isEmpty() // return true if nothing found
result.size()    // number of students found
result[0].name   // name of the first student

If you want to retrieve an entity by its id, you can use findxxxById(). This method will return an instance or null if the id is not found.

Student student = findStudentById(1)

You can also find entities by their attribute by using findxxxByxxx(). This method will return a List which contains all xxx instances that have the specified attribute's value.

List result = findStudentByRegistrationNumber('12345')

The method above will find all students with registration number exactly equals to '12345'. By default, the method will use equals operator for searching. Sometimes you will need another operator, for example, in querying student's name, it is useful to use like operator. To specify which operator will be used, you need to pass an operator as String to the method.

List result1 = findStudentByName('like', '%jocki%')
List result2 = findStudentByAge('gt', 18)

The operator must be name of a method in CriteriaBuilder. You can find more information about CriteriaBuilder methods in CriteriaBuilder javadoc.

If you want to select by more than one attribute, you can use findXXXBy(). This method expect a Map whose keys are attributes and values are the expected values for each corresponding attributes.

List result1 = findStudentBy([age: 20, major: 'computer science'])
// will return all students whose ages are 20 
// and majors are 'computer science' 

For more complicated query, you can use simple-jpa query DSL. Every condition in simple-jpa query DSL must be separated by a line as shown below:

List result = findStudentByDsl {
    name like('%jocki%')
    or()
    name like('%hendry%')
    and()
    major eq('computer science')
}

simple-jpa query DSL uses some of CriteriaBuilder methods as operator for its condition. You can find more information about CriteriaBuilder methods in CriteriaBuilder javadoc.

You can also execute JP QL by calling executeQuery() method. This method will return a List that contains query's result.

List result = executeQuery("FROM Student s " + 
    "WHERE s.name LIKE '%jocki%'")

In some cases, you may want to directly issue SQL statement. You can call executeNativeQuery() for this purpose. This method will return a List that contains array of resulting fields.

List result = executeNativeQuery("SELECT s.name, s.age, s.major " +
    "FROM Student s WHERE s.name LIKE '%jocki%'")
    
result[0][0]  // first field (name) of the first row
result[0][1]  // second field (age) of the first row
result[0][2]  // third field (major) of the first row

result[1][0]  // first field (name) of the second row
result[1][1]  // second field (age) of the second row
result[1][2]  // third field (major) of the second row

Most finders will accept a Map as second argument. This Map can have the following keys:

findAllStudent([orderBy: 'name', orderDirection: 'desc'])
findAllStudent([orderBy: 'name,faculty', orderDirection: 'desc,asc'])
findStudentByFaculty('cs', [orderBy: 'name', page: 2, pageSize: 50])