Native queries are queries expressed through Structured Query Language (SQL) and are concise as well as type-safe methods of C++ and Java query expression. The use of native queries is beneficial in utilization of specific features in databases, including query hints and certain database-specific features. They are also useful in providing clean paths of migration directly from applications using SQL or Java Database Connectivity (JDBC™). Native queries are used to provide results consisting of scalar values, entities or both. These types of queries support the use of native SQL on a target database.
These types of queries can overcome a string-based application programming interface’s (API’s) shortcomings. Queries of this kind are used in an object database, where object persistence is a common task for programmers and a common feature of the database. Non-native queries, however, appear foreign in programs that are object-oriented due to their expressions being simple strings and object graphs containing interspersed strings.
Native queries help alleviate the problems faced in databases by expressing a query within Java and C++. Queries can be written without an API or a customized query language. In addition, an Integrated Development Environment (IDE) is able to aid in the reduction of typos. The native queries would then be type-safe to the fullest and able to be accessed by an IDE’s refactoring features. Finally, the queries can be run, tested and prototyped against plain memory collections without the back end of a database.
The queries, and their accompanying SQL, are not necessarily transferrable across other databases. Upon the return of many entities by a particular native query, these same entities should be specified, as well as mapped, to column results in SQL statements in a metadata definition mapping software program. As a result, these may be used to map JDBC results by persistent runtime into expected objects. Name parameter use is never defined for these types of queries and portable applications can only use binding of positional parameters for native SQL queries. Support of joins is limited to relationships of single values.
To use native queries, a programmer must describe a SQL result set. Describing the result set helps a program such as Entitymanager map columns onto properties of entities. Scalar results may also be defined and mixed with other entity results, resulting in a return of an alias column. Once result sets are described, execution of native queries is possible.