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/Advanced JPA Development/NoSQL/Querying"

m
 
(8 intermediate revisions by one other user not shown)
Line 1: Line 1:
 +
----
 +
 +
 +
'''[[Image:Elug_draft_icon.png|Warning]] This page is now obsolete. '''
 +
 +
For current information, please see "Using EclipseLink with NoSQL Databases in the ''EclipseLink Solutions Guide'': http://www.eclipse.org/eclipselink/documentation/latest/solutions/nonrelational_db.htm
 +
 +
 +
----
 +
 +
 
{{EclipseLink_UserGuide
 
{{EclipseLink_UserGuide
 
|info=y
 
|info=y
|toc=n
+
|toc=y
 
|eclipselink=y
 
|eclipselink=y
 
|eclipselinktype=JPA
 
|eclipselinktype=JPA
Line 12: Line 23:
  
 
= Querying =
 
= Querying =
Querying in NoSQL is dependent on the NoSQL platform.  Some NoSQL data-sources may support dynamic querying through their own query language, others may not support querying at all. EclipseLink does its best to support as much of JPQL and the Criteria API on a NoSQL platform as possible.  Almost all NoSQL platforms allow querying by Id, so <code>find()</code> can be used to look-up objects by Id.  Most NoSQL platforms also allow a find everything operation, this allows EclipseLink to support JPQL and Criteria queries that are by Id, of have no where clause.
+
Querying in NoSQL is dependent on the NoSQL platform.  Some NoSQL data-sources may support dynamic querying through their own query language, others may not support querying at all.
 +
 
 +
===JPQL and Criteria===
 +
EclipseLink does its best to support as much of JPQL and the Criteria API on a NoSQL platform as possible.  Almost all NoSQL platforms allow querying by Id, so <code>find()</code> can be used to look-up objects by Id.  Most NoSQL platforms also allow a find everything operation, this allows EclipseLink to support JPQL and Criteria queries that are by Id, or have no where clause.
  
 
For example:
 
For example:
Line 23: Line 37:
  
 
<blockquote>
 
<blockquote>
MongoDB - JPQL and Criteria are supported with some restrictions.  Joins, sub-selects and certain database functions are not supported.
+
MongoDB - JPQL and Criteria are supported with some restrictions.  Joins, sub-selects, group by and certain database functions are not supported.
 
</blockquote>
 
</blockquote>
  
Line 30: Line 44:
 
</blockquote>
 
</blockquote>
  
 +
======''MongoDB JPQL examples''======
 +
<source lang="java">
 +
Query query = em.createQuery("Select o from Order o where o.totalCost > 1000");
 +
List<Order> orders = query.getResultList();
 +
 +
Query query = em.createQuery("Select o from Order o where o.description like 'Pinball%'");
 +
List<Order> orders = query.getResultList();
 +
 +
Query query = em.createQuery("Select o from Order o join o.orderLines l where l.description = :desc");
 +
query.setParameter("desc", "shipping");
 +
List<Order> orders = query.getResultList();
 +
 +
Query query = em.createQuery("Select o.totalCost from Order o");
 +
List<BigDecimal> orders = query.getResultList();
 +
</source>
 +
 +
===Native Queries===
 +
Native SQL queries are not supported with NoSQL.  Some NoSQL platforms may offer their own native query language, EclipseLink will allow JPA native queries using this language.
 +
 +
<blockquote>
 +
MongoDB - JPA native queries use the MongoDB native command language.
 +
</blockquote>
 +
 +
======''MongoDB native query example''======
 +
<source lang="java">
 +
Query query = em.createNativeQuery("db.ORDER.findOne({\"_id\":\"" + oid + "\"})", Order.class);
 +
Order order = (Order)query.getSingleResult();
 +
</source>
 +
 +
===Interaction Queries===
 +
Internally EclipseLink uses instances of <code>EISInteraction</code> to execute queries.  There are two main <code>EISInteraction</code> subclasses, <code>MappedInteraction</code> and <code>XMLInteraction</code>.  Interactions are the NoSQL equivalent to <code>SQLCall</code> and <code>StoredProcedureCall</code> in the EclipseLink native API for relational databases.  Interactions can contain content and properties to pass to the NoSQL adapter to execute a specific operation.  Interactions can be used on EclipseLink native API <code>DatabaseQuery</code>, or in mapping or <code>DescriptorQueryManager</code> operations.
  
 +
The <code>DescriptorQueryManager</code> contains operations for <code>readObject</code>, <code>readAll</code>, <code>insert</code>, <code>update</code>, and <code>delete</code>.  An interaction can be defined for any of these operations to customize or define how the operation should execute.  This can be done using the EclipseLink native API through a <code>DescriptorCustomizer</code>.
  
 
<br>  
 
<br>  

Latest revision as of 07:16, 17 April 2013



Warning This page is now obsolete.

For current information, please see "Using EclipseLink with NoSQL Databases in the EclipseLink Solutions Guide: http://www.eclipse.org/eclipselink/documentation/latest/solutions/nonrelational_db.htm




EclipseLink JPA

Querying

Querying in NoSQL is dependent on the NoSQL platform. Some NoSQL data-sources may support dynamic querying through their own query language, others may not support querying at all.

JPQL and Criteria

EclipseLink does its best to support as much of JPQL and the Criteria API on a NoSQL platform as possible. Almost all NoSQL platforms allow querying by Id, so find() can be used to look-up objects by Id. Most NoSQL platforms also allow a find everything operation, this allows EclipseLink to support JPQL and Criteria queries that are by Id, or have no where clause.

For example:

SELECT o FROM ORDER o
SELECT o FROM ORDER o WHERE o.id = :id

MongoDB - JPQL and Criteria are supported with some restrictions. Joins, sub-selects, group by and certain database functions are not supported.

Oracle NoSQL - find() and JPQL and Criteria by Id or with no WHERE clause are supported.

MongoDB JPQL examples
Query query = em.createQuery("Select o from Order o where o.totalCost > 1000");
List<Order> orders = query.getResultList();
 
Query query = em.createQuery("Select o from Order o where o.description like 'Pinball%'");
List<Order> orders = query.getResultList();
 
Query query = em.createQuery("Select o from Order o join o.orderLines l where l.description = :desc");
query.setParameter("desc", "shipping");
List<Order> orders = query.getResultList();
 
Query query = em.createQuery("Select o.totalCost from Order o");
List<BigDecimal> orders = query.getResultList();

Native Queries

Native SQL queries are not supported with NoSQL. Some NoSQL platforms may offer their own native query language, EclipseLink will allow JPA native queries using this language.

MongoDB - JPA native queries use the MongoDB native command language.

MongoDB native query example
Query query = em.createNativeQuery("db.ORDER.findOne({\"_id\":\"" + oid + "\"})", Order.class);
Order order = (Order)query.getSingleResult();

Interaction Queries

Internally EclipseLink uses instances of EISInteraction to execute queries. There are two main EISInteraction subclasses, MappedInteraction and XMLInteraction. Interactions are the NoSQL equivalent to SQLCall and StoredProcedureCall in the EclipseLink native API for relational databases. Interactions can contain content and properties to pass to the NoSQL adapter to execute a specific operation. Interactions can be used on EclipseLink native API DatabaseQuery, or in mapping or DescriptorQueryManager operations.

The DescriptorQueryManager contains operations for readObject, readAll, insert, update, and delete. An interaction can be defined for any of these operations to customize or define how the operation should execute. This can be done using the EclipseLink native API through a DescriptorCustomizer.



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

Back to the top