The JPA-SQL editor is a powerful tool that facilitates the formulation of your database queries in RapidClipse. JPA-SQL is a domain-specific, database-independent language (DSL). The syntax is deliberately similar to SQL so that JPA-SQL is easy to learn. However, JPA-SQL is not to be confused with native SQL. Queries formulated in JPA-SQL are never sent to a database. Instead, they are converted to Java code based on the JPA Criteria API. Thus, you can access all the benefits of the JPA Criteria API without having to write the JPA Criteria code.
At runtime, Hibernate generates the native SQL statements from the generated JPA Criteria code to match the connected database, and these are sent to the database. Thus JPA-SQL combines the simplicity of SQL with the numerous advantages of JPA Criteria API.
Benefits of JPA-SQL compared to the use of SQL strings:
- Clearer code structure
- Type safe
- Any order of statements
- IDE support
- Code Completion - detection of keywords, operators, and entities
- Syntax highlighting
- Code folding
Inline refactoring and refactoring participants for JDT member renames and moves
Error/warning markers with quick fixes
Integration in Eclipse build process
- Debuggable (JPA Criteria Code is generated)
- Query method is automatically generated in DAO
- Database independent
Benefits of JPA-SQL in comparison to the JPA Criteria API:
- SQL-like syntax and code structure
- Significantly lower complexity
- Cleaner code
- Easy to learn
- Under Project Management > Data Access click the DAO with which you want to perform a database query, e.g. CustomerDAO.java.
- Press Strg + Space and select query: create new query in the following autocomplete window.
Enter an appropriate method name for the generated method, e.g. findAllCustomer.
- After the keyword from, enter C, press Strg + Space and select Customer.
- Click Save.
Where condition with parameter
It is also possible to pass objects as parameters:
Returns specific columns
Saves the query result in a different class
MAX function - Returns the highest unit price
MIN function - Returns the lowest unit price
AVG function - Grouped by product name. Calculates the average order price of the orders within a group of products
ORDER BY function
Alternative to Subselects:
- Complex queries
JPA-SQL Language Specification