Skip Headers
Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide
10g (10.1.3.5.0)

Part Number E13981-01
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Index
Index
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
View PDF

Implementing a JPA Dynamic Query

Using EntityManager methods createQuery or createNativeQuery, you can create a Query object dynamically at run time (see "Using Java"). Using the Query methods getResultList, getSingleResult, or executeUpdate you can execute the query (see "Executing a Query").

Optionally, you can configure your named query with query hints to use JPA persistence provider vendor extensions (see "Configuring TopLink Query Hints in a JPA Query").

For more information, see the following:

Using Java

Example 8-4 shows how to create a dynamic EJB QL query with parameters and how to execute the query. In this example, the query returns multiple results so Query method getResultList is used.

Example 8-4 Implementing and Executing a Dynamic Query

Query queryEmployeeByFirstName = entityManager.createQuery(
    "SELECT OBJECT(emp) FROM Employee emp WHERE emp.firstName = :firstname"
);

queryEmployeeByFirstName.setParameter("firstName", "Joan");

Collection employees = queryEmployeeByFirstName.getResultList();