JPQL (Java Persistence Query Language)

The simple JPQL query selects all the instances of single entiity type.

SELECT e FROM Employee e

if you want just the names of the Employee, the following query this bellow.

SELECT e.name FROM Employee e

filtering query

SELECT e FROM Employee e WHERE e.department.name = 'sl54' AND e.address.state IN ('JKT','SMG')

Join between entity

SELECT p.number
FROM Employee e, Phone p
WHERE e=p.employee AND 
e.department.name ='sa90' AND
p.type ='cell'

Query parameters

Positional parameter notation

SELECT e FROM Employee e
WHERE e.dapartment = ?2 AND e.salary > ?4

Named Parameter notation

SELECT e FROM Employee e
WHERE e.department = :dept AND
      e.salary > :base

using namedQuery

@NamedQuery(
name="findStudent"
value=SELECT s FROM Student s WHERE s:grade = "3"

Working with @NamedQueries

@NamedQueries({
  @NamedQuery(name="Employee.findAll",
  @query=SELECT e FROM EMPLOYEE e),
  @NamedQuery(name="Employee.findByPrimaryKey",
  @query="SELECT e FROM EMPLOYEE e WHERE e.id= :id)
             )}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s