I have started to learn JPA and I have searched on many sites and I couldn't 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 can't understand why the author is selecting an Object(emp)..why doesn't he use something like this SELECT emp FROM Employee emp WHERE emp.firstName = 'John'
Is there a difference ? Do I miss something ?
SELECT OBJECT(emp) FROM Employee emp
is the exact same as
SELECT emp FROM Employee emp
See the JPA spec. [69] Note that the keyword OBJECT is not required. It is preferred that it be omitted for new queries.
The origin of "OBJECT" is in EJBs (now outdated).
The difference is that even though path expressions can resolve to an entity type, the syntax of the OBJECT keyword is limited to identification variables.
You can safely remove OBJECT from the query.
To make an example, if Employee has a relationship with a Department entity, this query would be illegal using object:
SELECT OBJECT(emp.department) FROM Employee emp WHERE emp.firstName = 'John' // Not valid
SELECT emp.department FROM Employee emp WHERE emp.firstName = 'John' // Valid
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With