Detailed explanation of pure SQL statement method based on JPQL

Detailed explanation of pure SQL statement method based on JPQL

JPQL stands for Java Persistence Query Language.

Based on the EJB Query Language (EJB QL) first introduced in EJB 2.0, the Java Persistence Query Language (JPQL) is a portable query language designed to bind SQL syntax and simple query semantics together in the form of object-oriented expression language expressions. Queries written in this language are portable and can be compiled into SQL on all major database servers.

Its features are similar to native SQL statements and are fully object-oriented, accessed through class names and attributes rather than table names and table attributes.

To use JPQL, you need to modify the SQL statement to be similar to HQL statement. SQL queries the database, while JPQL queries objects and attributes, and the syntax is somewhat different. For some queries that cannot be written in JPQL, it is more convenient to write them in native SQL.

Here is an example, note the difference in syntax:

JPQL Query

@PersistenceContext
protected EntityManager em;

public List<Video> findVideoList1() {
  String hql = "from Video order by id desc";
  Query query = em.createQuery(hql);
  List<Video> result = query.getResultList();
  em.clear();
  return result;
}

SQL Query

Query the data for the last 7 days

public List<Video> findVideoList2() {
  List<Video> result = (List<Video>) em.createNativeQuery
    ("select * from db_video where date_sub(curdate(), interval 6 day) <= date(date) order by date desc", Video.class)
    .getResultList();
  return result;
}

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • JPA uses JPQL statements to add, delete, modify and query
  • Detailed installation tutorial of mysql5.7.19 decompressed version (with pure cracked Chinese version SQLYog)
  • Introduction and usage examples of CodernityDB, a nosql database developed in pure Python
  • Detailed explanation of how to import pure IP data into MySQL in 3 steps
  • Beautiful Flash slideshow and SQL tag tutorial written with pure CSS+DIV!
  • Differences between SQL delete statements DROP, TRUNCATE, and DELETE
  • Mybatis-plus configuration console prints complete SQL statement with parameters
  • Install the MyBatis Log Plugin plugin in IDEA and execute the mybatis sql statement (recommended)

<<:  In-depth analysis of Linux NFS mechanism through cases

>>:  Source code reveals why Vue2 this can directly obtain data and methods

Recommend

MySQL 8.0.11 installation tutorial with pictures and text

There are many tutorials on the Internet, and the...

Jenkins packaging microservices to build Docker images and run them

Table of contents Environment Preparation start 1...

The forgotten button tag

Note: This article has been translated by someone ...

...

Tutorial on installing MySQL 5.6 on CentOS 6.5

1. Download the RPM package corresponding to Linu...

MySQL uses events to complete scheduled tasks

Events can specify the execution of SQL code once...

Solution to the problem of mysql service starting but not connecting

The mysql service is started, but the connection ...

Implementation code of front-end HTML skin changing function

50 lines of code to change 5 skin colors, includi...

Docker image access to local elasticsearch port operation

Using the image service deployed by docker stack,...

HTML 5 Preview

<br />Original: http://www.alistapart.com/ar...

Ubuntu 15.04 opens mysql remote port 3306

Ubuntu 15.04 opens MySQL remote port 3306. All th...

MySQL database Shell import_table data import

Table of contents MySQL Shell import_table data i...

js realizes the dynamic loading of data by waterfall flow bottoming out

This article shares with you the specific code of...