Jump to: navigation, search

Difference between revisions of "CDOQuery OCL"

(Model)
(Queries)
Line 9: Line 9:
  
 
=== Queries ===
 
=== Queries ===
 +
This section documents some queries based on the aforementioned model. It is work in progress.
 +
 +
==== Finding object references ====
 +
The following OCL query finds all <tt>Konsultation</tt> entries in the entire repository, that belong to a certain <tt>Patient p</tt>, "injected" by the context. The element provided by the context is refered as '''self''' within the query.
 +
 +
<source lang="java">
 +
CDOTransaction cdoTransaction = Activator.openSession("demo")
 +
.openTransaction();
 +
 +
CDOQuery cqo = cdoTransaction.createQuery("ocl",
 +
"Konsultation.allInstances()->select(k:Konsultation|k.belongsTo=self)",
 +
((Patient)p).cdoID(), false);
 +
 +
List<Konsultation> lre = cqo.getResult(Konsultation.class);
 +
</source>
 +
 +
The most important part in this query is the fact that we are comparing real objects, as <tt>k.belongsTo</tt> refers to an element of type <tt>Patient</tt>. For OCL CDO to evaluate this query correct, we need to pass the context objects <tt>CDOID</tt> object as otherwise we will receive an error. A direct comparison by simply passing <tt>(Patient)p</tt> as context is hence not possible.

Revision as of 01:55, 18 April 2012

Based on CDO 4.0-SR2

Model

We have the following, deliberately trivial, model:

TrivialModel.png

We have the entities Patient and Konsultation (Consultation), which hold some generic attributes resp. features. The enumeration Geschlecht (sex) is self explaining. Each Konsultation belongsTo exactly one Patient, it is however not contained by the patient but exists as independent entity.

Queries

This section documents some queries based on the aforementioned model. It is work in progress.

Finding object references

The following OCL query finds all Konsultation entries in the entire repository, that belong to a certain Patient p, "injected" by the context. The element provided by the context is refered as self within the query.

CDOTransaction cdoTransaction = Activator.openSession("demo")
	.openTransaction();
 
CDOQuery cqo = cdoTransaction.createQuery("ocl",
		"Konsultation.allInstances()->select(k:Konsultation|k.belongsTo=self)",
		((Patient)p).cdoID(), false);
 
List<Konsultation> lre = cqo.getResult(Konsultation.class);

The most important part in this query is the fact that we are comparing real objects, as k.belongsTo refers to an element of type Patient. For OCL CDO to evaluate this query correct, we need to pass the context objects CDOID object as otherwise we will receive an error. A direct comparison by simply passing (Patient)p as context is hence not possible.