Implementing a query using @NamedQuery with "SELECT OBJECt (var) FROM EntityName var

I started learning JPA, and I searched on many sites, and I could not find an explanation regarding this example:

Implementing a query using @NamedQuery:

   @Entity
   @NamedQuery(
    name="findAllEmployeesByFirstName",
    queryString="SELECT OBJECT(emp) FROM Employee emp WHERE emp.firstName = 'John'"
)
public class Employee implements Serializable {
...
}

I just donโ€™t understand why the author selects the object (emp) .. why he doesnโ€™t use something like this SELECT emp FROM Employee emp WHERE emp.firstName = 'John'

Is there any difference? Did I miss something?

+4
source share
3 answers

The difference is that although path expressions can be resolved for an entity type, the syntax of the OBJECT keyword is limited to identification variables.

You can safely remove OBJECT from the query.

, Employee , :

SELECT OBJECT(emp.department) FROM Employee emp WHERE emp.firstName = 'John'  // Not valid
SELECT emp.department FROM Employee emp WHERE emp.firstName = 'John' // Valid
+2
SELECT OBJECT(emp) FROM Employee emp

,

SELECT emp FROM Employee emp

. JPA. [69] , OBJECT . , .

"OBJECT" EJB ( ).

+3

. , Java interface, abstract interface, , .

+2

Source: https://habr.com/ru/post/1648024/


All Articles