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 "CDO/Hibernate Store/HQL"

(New page: __TOC__ The CDO Hibernate Store supports HQL as the query language. For a detailed description the HQL syntax visit the documentation [http://docs.jboss.org/hiber...)
 
(A simple query)
Line 16: Line 16:
 
The [[CDO_Hibernate_Store_Download_and_Install#Example_Projects|download & install]] page describes how/where to find these example projects.
 
The [[CDO_Hibernate_Store_Download_and_Install#Example_Projects|download & install]] page describes how/where to find these example projects.
  
== A simple query ==
+
== Some simple queries ==
  
Let's start with a simple query to also introduce the client side query api. The following code snippet shows how to retrieve all products from the backend:
+
Let's start with a simple query to also introduce the client side query api. The following code snippet shows how to retrieve all instances of the Product from the backend:
 
<source lang="java">
 
<source lang="java">
    CDOSession session = openSession();
+
CDOSession session = openSession();
    CDOTransaction transaction = session.openTransaction();
+
CDOTransaction transaction = session.openTransaction();
  
    {
+
CDOQuery cdoQuery = transaction.createQuery("hql", "from Product"); //$NON-NLS-1$  //$NON-NLS-2$
      CDOQuery cdoQuery = transaction.createQuery("hql", "from Product"); //$NON-NLS-1$  //$NON-NLS-2$
+
final List<Product> products = cdoQuery.getResult(Product.class);
      final List<Product> products = cdoQuery.getResult(Product.class);
+
transaction.commit();
 
</source>
 
</source>
 +
 +
This snippet shows a number of things:
 +
* querying is done through a transaction and its createQuery method.
 +
* the HQL query uses the name: Product to query for products. This is the entity name. The entity name is normally the same as the EClass. You can control the entity name through an annotation or through a so-called entity naming strategy.
 +
* the createQuery method expects two parameters: 1) the query language (for the hibernate store always "hql" has to be used) and the query itself.
 +
* the createQuery method returns a CDOQuery object
 +
* the CDOQuery object has a getResult method which always returns a List, it is a generic parameterized method so the result is in the correct type (by passing the Product.class)
 +
 +
This was the first step, let's now introduce the usage of parameters (now without the open session and open transaction code).
 +
  
 
----
 
----
 
Wikis: [[CDO]] | [[Net4j]] | [[EMF]] | [[Eclipse]]
 
Wikis: [[CDO]] | [[Net4j]] | [[EMF]] | [[Eclipse]]

Revision as of 12:13, 21 January 2010


The CDO Hibernate Store supports HQL as the query language. For a detailed description the HQL syntax visit the documentation here.

This page shows examples of different HQL queries to give a feel for what the possibilities are to use HQL directly from the client. As far as is known there are no limits in using HQL on the client.

Note that HQL is supported not the Hibernate Criteria api.

Example projects

The HQL queries discussed here are available in the example project: org.eclipse.emf.cdo.examples.hibernate.client, in the HibernateQueryTest class.

The model which is used here is available as an example project: org.eclipse.emf.cdo.examples.company.

The download & install page describes how/where to find these example projects.

Some simple queries

Let's start with a simple query to also introduce the client side query api. The following code snippet shows how to retrieve all instances of the Product from the backend:

CDOSession session = openSession();
CDOTransaction transaction = session.openTransaction();
 
CDOQuery cdoQuery = transaction.createQuery("hql", "from Product"); //$NON-NLS-1$  //$NON-NLS-2$
final List<Product> products = cdoQuery.getResult(Product.class);
transaction.commit();

This snippet shows a number of things:

  • querying is done through a transaction and its createQuery method.
  • the HQL query uses the name: Product to query for products. This is the entity name. The entity name is normally the same as the EClass. You can control the entity name through an annotation or through a so-called entity naming strategy.
  • the createQuery method expects two parameters: 1) the query language (for the hibernate store always "hql" has to be used) and the query itself.
  • the createQuery method returns a CDOQuery object
  • the CDOQuery object has a getResult method which always returns a List, it is a generic parameterized method so the result is in the correct type (by passing the Product.class)

This was the first step, let's now introduce the usage of parameters (now without the open session and open transaction code).



Wikis: CDO | Net4j | EMF | Eclipse

Back to the top