Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why "SELECT c" instead of "SELECT * " in JPQL?

Example query in JPQL looks like this:

SELECT c FROM Customer c;

I read JPA specification, books and I can't find what this c means. In SQL we simply write:

SELECT * FROM customer;

I know we can use this c as alias i.e. in WHERE clause like this:

SELECT c FROM Customer c WHERE c.name = ...

but I still don't understand what this c actually is, how to call it (alias? object?) and why it has to be just after SELECT instead of *.

like image 372
swch Avatar asked Jan 11 '16 15:01

swch


People also ask

Can we use subquery in JPQL?

A subselect is a query embedded into another query. It's a powerful feature you probably know from SQL. Unfortunately, JPQL supports it only in the WHERE clause and not in the SELECT or FROM clause. Subqueries can return one or multiple records and can use the aliases defined in the outer query.

Why JPQL is better than SQL in Web application?

JPA allows you to avoid writing DML in the database specific dialect of SQL. JPA allows you to load and save Java objects and graphs without any DML language at all. When you do need to perform queries JPQL allows you to express the queries in terms of the Java entities rather than the (native) SQL tables and columns.

Which methods should be used for pagination with JPQL?

For all JPA query objects (except for native SQL queries), you would use pagination through the setMaxResults(int) and setFirstResult(int) methods.

What is the difference between JPQL and Hql?

JPQL is a heavily-inspired-by subset of HQL. A JPQL query is always a valid HQL query, the reverse is not true however. Both HQL and JPQL are non-type-safe ways to perform query operations. Criteria queries offer a type-safe approach to querying. See Criteria for more information.


1 Answers

SQL returns rows, and rows contain columns.

JPQL is a bit more complex: it can return rows of columns, but also entity instances.

So, suppose you have an (invalid) JPQL query like

select * from School school
join school.students student
where ...

What should the query return? Instances of School? Instances of Student? columns? Quite hard to know. Suppose it returns all the fields of school and students, what would be the order of the fields? How could you use the results?

Whereas if you do

select school from School school
join school.students student
where ...

you tell JPQL that you want to get instances of the School entity.

If you do

select student from School school
join school.students student
where ...

you tell JPQL that you want to get instances of the Student entity.

If you do

select school.name, student.firstName, student.age 
from School school
join school.students student
where ...

you tell JPQL that you want to get rows, containing three columns: the school name, the student first name, and the student age.

Note that, even in SQL, it's considered bad practice to use select * from a program as well: the query probably returns more columns than really needed, you don't now the order of the columns, and you rely on the name the columns have in the result set rather than specifying the desired names in the query.

like image 125
JB Nizet Avatar answered Nov 03 '22 01:11

JB Nizet