The Java Persistence API provides facilities to retrieve entities from the database by using the Java Persistence Query Language. The Java Persistence Query Language is very similar to SQL. Therefore, if you are familiar with SQL, you should find that the JPA QL is not too different.
Note
In this chapter, we are just going to cover the Java Persistence Query Language briefly. Full details of the query language can be found at:
To retrieve a list of entities using JPA, we use the entity manager, just as we did when persisting an entity. To retrieve a list of customers, we would use a method similar to the following one.
private List<Customer>customers; public void getCustomers() { customers = em.createQuery("select customer from Customer customer order by customer.secondName").getResultList(); }
Here, we can see that we use the createQuery()
method of the entity manager to perform a query...