JPA Named Query "o"?
I am just curious what the o stands for when I am trying to query.
By default my query is coming out as @NamedQuery (name="newName" query=SELECT o FROM tableView o)
I am just curious what the o stands for. I have seen several instances of using the letter x and c as well. Just wondering what the difference, if any. Also, why can't I just use a * to select and entire row?
It is called identification variable in JQPL terminology. When used in SELECT statements, identification variable is declared in FROM clause. It is mandatory to use it in SELECT statement if some persistent attribute (of entity), entity itself, or some value deduced from persistent attribute is designed as a result of query.
SELECT a.someAttribute FROM EntityA a SELECT a FROM EntityA a SELECT SUM(a.someAttribute) FROM EntityA a
Same queries without using a. are not valid JQPL queries. They do work in Hibernate, because HQL as a query language does have quite a bunch of extensions.
Need for identification variable comes obvious when query contains more than one entity with identical names of attributes:
SELECT a.someAttribute, b.someAttribute FROM EntityA a, EntityB WHERE a.id = b.id
As you see from the query above, identification variable are also used in WHERE clause. Following quote from JPA 2.0 specification sums up also other uses and declaration of them quite tightly:
All identification variables used in the SELECT, WHERE, ORDER BY, GROUP BY, or HAVING clause of a SELECT or DELETE statement must be declared in the FROM clause. The identification variables used in the WHERE clause of an UPDATE statement must be declared in the UPDATE clause.
Identification variables are existentially quantified in these clauses. This means that an identification variable represents a member of a collection or an instance of an entity’s abstract schema type. An identification variable never designates a collection in its entirety.
An identification variable is scoped to the query (or subquery) in which it is defined and is also visible to any subqueries within that query scope that do not define an identification variable of the same name.
Naming of identification variable is quite free, but it should not be any reserved identifier or name of the entity.
You cannot use 'SELECT *', because JPQL does not have such a construct. It is also often considered as an anti pattern in SQL not to specify explicitly list of values to be selected. It would for example need to redefine what is selected when join is added to query.
It's just an alias - you can use whatever you like. It's not really necessary in your example, but for a more complex example joining multiple tables it lets you refer to a specific table's properties. eg:
SELECT A from tableA A, tableB B where B.someId=A.id;