Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "EclipseLink/UserGuide/JPA/Basic JPA Development/Querying/JPQL"

m
Line 7: Line 7:
  
 
=JPQL=
 
=JPQL=
You may define queries in metadata annotations or the XML descriptor.
+
The Java Persistence Query Language (JPQL) is the query language defined by JPA.  JPQL is similar to SQL, but operates on objects, attributes and relationships instead of tables and columns.  JPQL can be used for reading (<tt>SELECT</tt>), as well as bulk updates (<tt>UPDATE</tt>) and deletes (<tt>DELETE</tt>).  JPQL can be used in a <tt>NamedQuery</tt> (through annotations or XML) or in dynamic queries using the <tt>EntityManager</tt> <tt>createQuery()</tt> API.
  
You can use update and delete queries to persist your changes with the Java Persistence Language (JPQL).
+
==SELECT==
  
You can perform bulk update of entities with the <tt>UPDATE</tt> statement. This statement operates on a single entity type and sets one or more single-valued properties of the entity subject to the condition in the <tt>WHERE</tt> clause. Update queries provide an equivalent to the <tt>SQL UPDATE</tt> statement, but with JP QL conditional expressions.
+
 
 +
==UPDATE==
 +
You can perform bulk update of entities with the <tt>UPDATE</tt> statement. This statement operates on a single entity type and sets one or more single-valued properties of the entity subject to the condition in the <tt>WHERE</tt> clause. Update queries provide an equivalent to the <tt>SQL UPDATE</tt> statement, but with JPQL conditional expressions.  Update queries should only be used for bulk updates, regular updates to objects should be made by using the object' set methods within a transaction and committing the changes.
  
 
This example demonstrates how to use an update query to give employees a raise. The <tt>WHERE</tt> clause contains the conditional expression.
 
This example demonstrates how to use an update query to give employees a raise. The <tt>WHERE</tt> clause contains the conditional expression.
Line 23: Line 25:
 
</source>
 
</source>
  
You can perform bulk removal of entities with the <tt>DELETE</tt> statement. Delete queries provide an equivalent to the <tt>SQL DELETE</tt> statement, but with JP QL conditional expressions.
+
The persistence context is not updated to reflect results of update operations. If you use a transaction-scoped persistence context, you should either execute the bulk operation in a transaction all by itself, or be the first operation in the transaction. That is because any entity actively managed by the persistence context will remain unaware of the actual changes occurring at the database level.
 +
 
 +
==DELETE==
 +
You can perform bulk removal of entities with the <tt>DELETE</tt> statement. Delete queries provide an equivalent to the <tt>SQL DELETE</tt> statement, but with JPQL conditional expressions.  Delete queries should only be used for bulk deletes, regular deletes to objects should be performed through calling the <tt>EntityManager</tt> <tt>remove()</tt> API.
  
 
This example demonstrates how to use a delete query to remove all employees who are not assigned to a department. The <tt>WHERE</tt> clause contains the conditional expression.
 
This example demonstrates how to use a delete query to remove all employees who are not assigned to a department. The <tt>WHERE</tt> clause contains the conditional expression.
Line 35: Line 40:
  
 
{{EclipseLink_Note
 
{{EclipseLink_Note
|note=Delete queries are polymorphic: any entity subclass instances that meet the criteria of the delete query will be deleted. However, delete queries do not honor cascade rules: no entities other than the type referenced in the query and its subclasses will be removed, even if the entity has relationships to other entities with cascade removes enabled.}}
+
|note=Delete queries are polymorphic: any entity subclass instances that meet the criteria of the delete query will be deleted. However, delete queries do not honor cascade rules: no entities other than the type referenced in the query and its subclasses will be removed, even if the entity has relationships to other entities with cascade removes enabled.  Delete queries will delete the rows from join and collection tables.}}
  
  
The persistence context is not updated to reflect results of update and delete operations. If you use a transaction-scoped persistence context, you should either execute the bulk operation in a transaction all by itself, or be the first operation in the transaction. That is because any entity actively managed by the persistence context will remain unaware of the actual changes occurring at the database level.
+
The persistence context is not updated to reflect results of delete operations. If you use a transaction-scoped persistence context, you should either execute the bulk operation in a transaction all by itself, or be the first operation in the transaction. That is because any entity actively managed by the persistence context will remain unaware of the actual changes occurring at the database level.
  
 
{{EclipseLink_Spec
 
{{EclipseLink_Spec

Revision as of 14:39, 12 April 2012

EclipseLink JPA

Eclipselink-logo.gif
EclipseLink
Website
Download
Community
Mailing ListForumsIRCmattermost
Issues
OpenHelp WantedBug Day
Contribute
Browse Source


JPQL

The Java Persistence Query Language (JPQL) is the query language defined by JPA. JPQL is similar to SQL, but operates on objects, attributes and relationships instead of tables and columns. JPQL can be used for reading (SELECT), as well as bulk updates (UPDATE) and deletes (DELETE). JPQL can be used in a NamedQuery (through annotations or XML) or in dynamic queries using the EntityManager createQuery() API.

SELECT

UPDATE

You can perform bulk update of entities with the UPDATE statement. This statement operates on a single entity type and sets one or more single-valued properties of the entity subject to the condition in the WHERE clause. Update queries provide an equivalent to the SQL UPDATE statement, but with JPQL conditional expressions. Update queries should only be used for bulk updates, regular updates to objects should be made by using the object' set methods within a transaction and committing the changes.

This example demonstrates how to use an update query to give employees a raise. The WHERE clause contains the conditional expression.

Update Query

 UPDATE Employee e
 SET e.salary = 60000
 WHERE e.salary = 50000

The persistence context is not updated to reflect results of update operations. If you use a transaction-scoped persistence context, you should either execute the bulk operation in a transaction all by itself, or be the first operation in the transaction. That is because any entity actively managed by the persistence context will remain unaware of the actual changes occurring at the database level.

DELETE

You can perform bulk removal of entities with the DELETE statement. Delete queries provide an equivalent to the SQL DELETE statement, but with JPQL conditional expressions. Delete queries should only be used for bulk deletes, regular deletes to objects should be performed through calling the EntityManager remove() API.

This example demonstrates how to use a delete query to remove all employees who are not assigned to a department. The WHERE clause contains the conditional expression.

Delete Query

 DELETE FROM Employee e
 WHERE e.department IS NULL

Elug note icon.png

Note: Delete queries are polymorphic: any entity subclass instances that meet the criteria of the delete query will be deleted. However, delete queries do not honor cascade rules: no entities other than the type referenced in the query and its subclasses will be removed, even if the entity has relationships to other entities with cascade removes enabled. Delete queries will delete the rows from join and collection tables.


The persistence context is not updated to reflect results of delete operations. If you use a transaction-scoped persistence context, you should either execute the bulk operation in a transaction all by itself, or be the first operation in the transaction. That is because any entity actively managed by the persistence context will remain unaware of the actual changes occurring at the database level.

Elug javaspec icon.gif

For more information, see Chapter 4 "Query Language" in the JPA Specification.

For more information and examples, see What You May Need to Know About Querying with Java Persistence Query Language.

Eclipselink-logo.gif
Version: 2.2.0 DRAFT
Other versions...

Back to the top