| <html><head> |
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
| <title xmlns:d="http://docbook.org/ns/docbook">Chapter 7. Selecting Objects</title><link rel="stylesheet" type="text/css" href="css/cayenne-doc.css"><meta xmlns:d="http://docbook.org/ns/docbook" name="keywords" content="Cayenne 3.1 documentation"><meta xmlns:d="http://docbook.org/ns/docbook" name="description" content="User documentation for Apache Cayenne version 3.1"><link rel="home" href="index.html" title="Getting Started with Cayenne"><link rel="up" href="getting-started-part3.html" title="Part III. Learning Cayenne API"><link rel="prev" href="ch06.html" title="Chapter 6. Getting started with persistent objects"><link rel="next" href="ch08.html" title="Chapter 8. Deleting Objects"><script xmlns:d="http://docbook.org/ns/docbook" type="text/javascript"> |
| var _gaq = _gaq || []; |
| _gaq.push(['_setAccount', 'UA-7036673-1']); |
| _gaq.push(['_trackPageview']); |
| (function() { |
| var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; |
| ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; |
| var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); |
| })(); |
| </script></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div xmlns:d="http://docbook.org/ns/docbook" class="navheader"><table width="100%" summary="Navigation header"><tr><th class="versioninfo">v.3.1 (3.1)</th><th align="center">Chapter 7. Selecting Objects</th><th></th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch06.html">Prev</a> </td><th width="60%" align="center"><a accesskey="u" href="getting-started-part3.html">Part III. Learning Cayenne API</a></th><td width="20%" align="right"> <a accesskey="n" href="ch08.html">Next</a></td></tr></table><hr></div><div class="chapter" title="Chapter 7. Selecting Objects"><div class="titlepage"><div><div><h2 class="title"><a name="d0e405"></a>Chapter 7. Selecting Objects</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="ch07.html#introducing-select-query">Introducing SelectQuery</a></span></dt></dl></div><p>This chapter shows how to select objects from the database using SelectQuery. </p><div class="section" title="Introducing SelectQuery"><div class="titlepage"><div><div><h2 class="title"><a name="introducing-select-query"></a>Introducing SelectQuery</h2></div></div></div><p>It was shown before how to persist new objects. Cayenne queries are used to access |
| already saved objects. The primary query type used for selecting objects is <span class="italic">SelectQuery</span>. It can be mapped in CayenneModeler or created |
| via the API. We'll use the later approach in this section. We don't have too much data |
| in the database yet, but we can still demonstrate the main principles below.</p><div class="itemizedlist"><ul class="itemizedlist"><li class="listitem"><p>Select all paintings (the code, and the log output it generates):</p></li></ul></div><pre class="programlisting">SelectQuery select1 = <span xmlns="http://www.w3.org/1999/xhtml" class="hl-keyword">new</span> SelectQuery(Painting.<span xmlns="http://www.w3.org/1999/xhtml" class="hl-keyword">class</span>); |
| List paintings1 = context.performQuery(select1);</pre><pre class="programlisting">INFO: SELECT t0.GALLERY_ID, t0.ARTIST_ID, t0.NAME, t0.ID FROM PAINTING t0 |
| INFO: === returned 2 rows. - took 18 ms.</pre><div class="itemizedlist"><ul class="itemizedlist"><li class="listitem"><p>Select paintings that start with "gi", ignoring case:</p></li></ul></div><pre class="programlisting">Expression qualifier2 = ExpressionFactory.likeIgnoreCaseExp( |
| Painting.NAME_PROPERTY, |
| <span xmlns="http://www.w3.org/1999/xhtml" class="hl-string">"gi%"</span>); |
| SelectQuery select2 = <span xmlns="http://www.w3.org/1999/xhtml" class="hl-keyword">new</span> SelectQuery(Painting.<span xmlns="http://www.w3.org/1999/xhtml" class="hl-keyword">class</span>, qualifier2); |
| List paintings2 = context.performQuery(select2);</pre><pre class="programlisting">INFO: SELECT t0.GALLERY_ID, t0.NAME, t0.ARTIST_ID, t0.ID FROM PAINTING t0 WHERE UPPER(t0.NAME) LIKE UPPER(?) |
| [bind: 1->NAME:'gi%'] - prepared in 6 ms. |
| INFO: === returned 1 row. - took 18 ms.</pre><div class="itemizedlist"><ul class="itemizedlist"><li class="listitem"><p>Select all paintings done by artists who were born more than a 100 years ago |
| (demonstrating using Expression.fromString(..) instead of |
| ExpressionFactory):</p></li></ul></div><pre class="programlisting">Calendar c = <span xmlns="http://www.w3.org/1999/xhtml" class="hl-keyword">new</span> GregorianCalendar(); |
| c.set(c.get(Calendar.YEAR) - <span xmlns="http://www.w3.org/1999/xhtml" class="hl-number">100</span>, <span xmlns="http://www.w3.org/1999/xhtml" class="hl-number">0</span>, <span xmlns="http://www.w3.org/1999/xhtml" class="hl-number">1</span>, <span xmlns="http://www.w3.org/1999/xhtml" class="hl-number">0</span>, <span xmlns="http://www.w3.org/1999/xhtml" class="hl-number">0</span>, <span xmlns="http://www.w3.org/1999/xhtml" class="hl-number">0</span>); |
| |
| Expression qualifier3 = Expression.fromString(<span xmlns="http://www.w3.org/1999/xhtml" class="hl-string">"artist.dateOfBirth < $date"</span>); |
| qualifier3 = qualifier3.expWithParameters(Collections.singletonMap(<span xmlns="http://www.w3.org/1999/xhtml" class="hl-string">"date"</span>, c.getTime())); |
| SelectQuery select3 = <span xmlns="http://www.w3.org/1999/xhtml" class="hl-keyword">new</span> SelectQuery(Painting.<span xmlns="http://www.w3.org/1999/xhtml" class="hl-keyword">class</span>, qualifier3); |
| List paintings3 = context.performQuery(select3);</pre><pre class="programlisting">INFO: SELECT t0.GALLERY_ID, t0.NAME, t0.ARTIST_ID, t0.ID FROM PAINTING t0 JOIN ARTIST t1 ON (t0.ARTIST_ID = t1.ID) |
| WHERE t1.DATE_OF_BIRTH < ? [bind: 1->DATE_OF_BIRTH:'1911-01-01 00:00:00.493'] - prepared in 7 ms. |
| INFO: === returned 2 rows. - took 25 ms.</pre></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch06.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="getting-started-part3.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ch08.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 6. Getting started with persistent objects </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 8. Deleting Objects</td></tr></table></div></body></html> |