11 November, 2016

JPA-SQL Code Editor introduction

Suitable for the 10 year jubilee of the JPA standard, RapidClipse 3 comes with a new JPA-SQL code editor for Eclipse. This tool enables you to write custom database queries in JPA-SQL, based on SQL. After that, the JPA-SQL code is turned into Javacode based on JPA Criteria API. If you compare the following code example, you can see most of the benefits at a look.



JPA Criteria Code:

public List<Customer> findAllCustomer() {
    EntityManager entityManager = em();

    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();

    CriteriaQuery<Customer> criteriaQuery = criteriaBuilder.createQuery(Customer.class);

    Root<Customer> root = criteriaQuery.from(Customer.class);

    TypedQuery<Customer> query = entityManager.createQuery(criteriaQuery);
    return query.getResultList();


What is JPA (Java Persistence API)?

If you want to develop database applications compliant with the Java standard, you have to use a Java persistence framework which implements the JPA specification, for example Open JPA, EclipseLink or Hibernate. Last one deemed as defacto standard and that's why we decided to use Hibernate in RapidClipse.

Database developers love SQL, but SQL is unsuited for Java

Even with JPA you can write custom queries by using plain SQL strings in any case, but actually you shouldn't do that. SQL strings have many disadvantages:
  • your query code is not type safety,
  • you completely give up your IDE support, for example code completion, syntax highlighting, error warnings, refactoring and so on,
  • you can not debug your query code,
  • the more statements, joins, quotes and string linkings you have, the more your code becomes confusing,
  • SQL strings are always a risk respectively SQL injection,
  • and your application will never be database-independent.

Java Query APIs are much more complex than SQL

For this reason, JPA provides us JPQL and the JPA Criteria API, which enable us to write our custom queries completely in Java. In addition to that, Hibernate provides it's own implementations HQL and the Hibernate Criteria API. Which API to use depends on intention and mostly it's a matter of taste, but we as Java tool makers had to make a decision which one we use for generting your query code.

JPQL Code Example: 

TypedQuery<Country> query =
    em.createQuery("SELECT c FROM Country c", Country.class);

List<Country> results = query.getResultList();

JPQL and thus HQL are similar to SQL, easy to read and write and that's why they are used frequently by developers. Unfortunately, JPQL and HQL using SQL strings as well, whereby we have disadvantages listed above. Until Hibernate 4 we personally were very pleased with the Hibernate Criteria API, but with the new Hibernate version 5 it's deprecated. JPA Criteria API is the only one remaining, if we want to avoid SQL strings. The JPA Criteria API is on the one hand perfectly. It's very powerful and flexible by supporting method chaining for example. But on the other hand it's considered as complicated, the code is difficult to read and write and does not look like SQL as you can see in the example above. Therefore, it's not much popular and mainly recommended for dynamic queries.

JPA-SQL combines the simplicity of SQL with the power of the JPA Criteria API 

To solve that problem, we decided to create a new query language called JPA-SQL, which combines the simplicity of SQL with all advantages of the powerful JPA Criteria API. The function principle is simple. JPA-SQL enables you to write custom queries in a SQL-based syntax, but behind the scenes the JPA-SQL Code Editor generates the respective JPA Criteria code in realtime.

Fully IDE support

JPA-SQL Code Editor provides you all advantages you know from Java coding:

  • type-safety 
  • code completion - detecting keywords, oprators and entities
  • syntax highlighting
  • code folding
  • code formatter
  • inline refactoring and refactoring participants for JDT member renames and moves
  • hovers
  • linking (Ctrl+Click)
  • outline view
  • error/warning marks with quick fixes
  • code templates
Additional advantages:
  • order of your statements can be chosen freely 
  • query-methods are generated automatically into your DAO
  • integration in Eclipse build process
  • debugging the generated JPA Criteria code is possible
  • safe from SQL injection
  • completely database-independent queries

Compatible with Hibernate, EclipseLink & Open JPA

The generated query code bases on the JPA Criteria API and thus can be used with any JPA provider.


JPA-SQL together with the JPA-SQL Code Editor is integrated in RapidClipse 3.0 available for free. A single JPA-SQL Code Editor plugin for Eclipse is planned already.

Download RapidClipse 3 with JPA-SQL for free

No comments:

Post a Comment