Wednesday, February 17, 2016

NetBeans Java EE Tip #2: JPQL Syntax Checking

When using JPA (Java Persistence API), one of the most common ways to query entity classes is to utilize JPQL (Java Persistence Query Language).  Although JPQL makes it easy to query entity classes, it is not type safe JPQL queries are typed using a plain String.  This will certainly cause an issue if the JPQL query is typed incorrectly...for instance if the entity class or one of the field names is incorrectly spelled.  If such is the case, the application will deploy without issues, but there will be a nasty error when the query is executed.

NetBeans helps cut down on the number of JPQL errors by providing real-time checking of JPQL query syntax.  If a JPQL query is typed incorrectly, a yellow light bulb will appear in the left hand margin of the NetBeans source editor and the erroneous createQuery() call will be underlined yellow, indicating the incorrect syntax (Figure 1).

Figure 1:  NetBeans flags errors in JPQL query strings

Once the error is corrected, the yellow lightbulb and underline will disappear, as seen in Figure 2.

Figure 2:  Errors have been repaired

This is a helpful feature because it saves the developer heart ache that would occur if an erroneous JPQL query were deployed.


1 comment:

  1. Is it possible to have JPQL Syntax Checking in spring-data @Query annotations?

    That would be awesome.

    ReplyDelete

Please leave a comment...