blob: fd0a12110179a34faa7aab1fef65f7dab8875d6a [file] [log] [blame]
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title xmlns:d="http://docbook.org/ns/docbook">Chapter&nbsp;8.&nbsp;Deleting 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&nbsp;III.&nbsp;Learning Cayenne API"><link rel="prev" href="ch07.html" title="Chapter&nbsp;7.&nbsp;Selecting Objects"><link rel="next" href="getting-started-part4.html" title="Part&nbsp;IV.&nbsp;Converting to Web Application"><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&nbsp;8.&nbsp;Deleting Objects</th><th></th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch07.html">Prev</a>&nbsp;</td><th width="60%" align="center"><a accesskey="u" href="getting-started-part3.html">Part&nbsp;III.&nbsp;Learning Cayenne API</a></th><td width="20%" align="right">&nbsp;<a accesskey="n" href="getting-started-part4.html">Next</a></td></tr></table><hr></div><div class="chapter" title="Chapter&nbsp;8.&nbsp;Deleting Objects"><div class="titlepage"><div><div><h2 class="title"><a name="d0e444"></a>Chapter&nbsp;8.&nbsp;Deleting Objects</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="ch08.html#setup-delete-rules">Setting Up Delete Rules</a></span></dt><dt><span class="section"><a href="ch08.html#deleting-objects">Deleting Objects</a></span></dt></dl></div><p>This chapter explains how to model relationship delete rules and how to delete individual
objects as well as sets of objects. Also demonstrated the use of Cayenne class to run a
query.</p><div class="section" title="Setting Up Delete Rules"><div class="titlepage"><div><div><h2 class="title"><a name="setup-delete-rules"></a>Setting Up Delete Rules</h2></div></div></div><p>Before we discuss the API for object deletion, lets go back to CayenneModeler and set
up some delete rules. Doing this is optional but will simplify correct handling of the
objects related to deleted objects.</p><p>In the Modeler go to "Artist" ObjEntity, "Relationships" tab and select "Cascade" for
the "paintings" relationship delete rule:</p><p><span class="inlinemediaobject"><img src="images/modeler-deleterule.png"></span>
</p><p>Repeat this step for other relationships:</p><div class="itemizedlist"><ul class="itemizedlist"><li class="listitem"><p>For Gallery set "paintings" relationship to be "Nullify", as a painting can
exist without being displayed in a gallery.</p></li><li class="listitem"><p>For Painting et both relationships rules to "Nullify".</p></li></ul></div><p>Now save the mapping, and refresh the project in Eclispe.</p></div><div class="section" title="Deleting Objects"><div class="titlepage"><div><div><h2 class="title"><a name="deleting-objects"></a>Deleting Objects</h2></div></div></div><p>While deleting objects is possible via SQL, qualifying a delete on one or more IDs, a
more common way in Cayenne (or ORM in general) is to get a hold of the object first, and
then delete it via the context. Let's use utility class Cayenne to find an
artist:</p><pre class="programlisting">Expression qualifier = ExpressionFactory.matchExp(Artist.NAME_PROPERTY, <span xmlns="http://www.w3.org/1999/xhtml" class="hl-string">"Pablo Picasso"</span>);
SelectQuery select = <span xmlns="http://www.w3.org/1999/xhtml" class="hl-keyword">new</span> SelectQuery(Artist.<span xmlns="http://www.w3.org/1999/xhtml" class="hl-keyword">class</span>, qualifier);
Artist picasso = (Artist) Cayenne.objectForQuery(context, select);</pre><p>Now let's delete the artist:</p><pre class="programlisting"><span xmlns="http://www.w3.org/1999/xhtml" class="hl-keyword">if</span> (picasso != null) {
context.deleteObject(picasso);
context.commitChanges();
}</pre><p>Since we set up "Cascade" delete rule for the Artist.paintings relationships, Cayenne
will automatically delete all paintings of this artist. So when your run the app you'll
see this output:</p><pre class="programlisting">INFO: SELECT t0.DATE_OF_BIRTH, t0.NAME, t0.ID FROM ARTIST t0
WHERE t0.NAME = ? [bind: 1-&gt;NAME:'Pablo Picasso'] - prepared in 6 ms.
INFO: === returned 1 row. - took 18 ms.
INFO: +++ transaction committed.
INFO: --- transaction started.
INFO: DELETE FROM PAINTING WHERE ID = ?
INFO: [batch bind: 1-&gt;ID:200]
INFO: [batch bind: 1-&gt;ID:201]
INFO: === updated 2 rows.
INFO: DELETE FROM ARTIST WHERE ID = ?
INFO: [batch bind: 1-&gt;ID:200]
INFO: === updated 1 row.
INFO: +++ transaction committed.</pre></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch07.html">Prev</a>&nbsp;</td><td width="20%" align="center"><a accesskey="u" href="getting-started-part3.html">Up</a></td><td width="40%" align="right">&nbsp;<a accesskey="n" href="getting-started-part4.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter&nbsp;7.&nbsp;Selecting Objects&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&nbsp;Part&nbsp;IV.&nbsp;Converting to Web Application</td></tr></table></div></body></html>