| Title: DataObjects Example |
| |
| |
| <P>As discussed in <A href="dataobjects.html" title="DataObjects">DataContext</A> chapter, any changes made to the DataObjects via "set*" methods are synchronized with the database by calling <TT>DataContext.commitChanges</TT>. Here is an example showing how the <TT>Painting</TT> object is fetched, modified and saved back. The following modifications are performed: the price of the first retrieved painting is doubled, a new painting is added that belongs to the same artist as the painting fetched before:</P> |
| <DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent"> |
| <PRE class="code-java"><SPAN class="code-keyword">import</SPAN> java.math.BigDecimal; |
| <SPAN class="code-keyword">import</SPAN> java.util.List; |
| <SPAN class="code-keyword">import</SPAN> org.apache.cayenne.access.DataContext; |
| <SPAN class="code-keyword">import</SPAN> org.apache.cayenne.query.SelectQuery; |
| <SPAN class="code-keyword">import</SPAN> org.apache.cayenne.exp.Expression; |
| <SPAN class="code-keyword">import</SPAN> org.apache.cayenne.exp.ExpressionFactory; |
| <SPAN class="code-keyword">import</SPAN> org.apache.art.Painting; |
| <SPAN class="code-keyword">import</SPAN> org.apache.art.Artist; |
| ... |
| |
| <SPAN class="code-comment">// assume <SPAN class="code-keyword">this</SPAN> exists and initialized |
| </SPAN>DataContext ctxt; |
| ... |
| Expression e = |
| ExpressionFactory.greaterExp(<SPAN class="code-quote">"estimatedPrice"</SPAN>, |
| <SPAN class="code-keyword">new</SPAN> BigDecimal(100000.0)); |
| SelectQuery q = <SPAN class="code-keyword">new</SPAN> SelectQuery(Painting.class, e); |
| List realExpensiveArt = ctxt.performQuery(q); |
| |
| <SPAN class="code-keyword">if</SPAN>(realExpensiveArt.size() > 0) { |
| Painting firstPainting = (Painting)realExpensiveArt.get(0); |
| |
| <SPAN class="code-comment">// <SPAN class="code-object">double</SPAN> the price |
| </SPAN> <SPAN class="code-object">double</SPAN> oldPrice = firstPainting.getEstimatedPrice().doubleValue(); |
| firstPainting.setEstimatedPrice(<SPAN class="code-keyword">new</SPAN> BigDecimal(oldPrice * 2.00)); |
| |
| <SPAN class="code-comment">// create <SPAN class="code-keyword">new</SPAN> painting and register it with DataContext |
| </SPAN> <SPAN class="code-comment">// <SPAN class="code-quote">"Painting"</SPAN> string is a name of ObjEntity in the DataMap |
| </SPAN> Painting newPainting = (Painting)ctxt.newObject(Painting.class); |
| newPainting.setPaintingTitle(<SPAN class="code-quote">"Sunset as it is"</SPAN>); |
| |
| <SPAN class="code-comment">// price it as unreasonable as other paintings |
| </SPAN> newPainting.setEstimatedPrice(<SPAN class="code-keyword">new</SPAN> BigDecimal(5000000.0)); |
| |
| <SPAN class="code-comment">// get artist via relationship |
| </SPAN> Artist artist = firstPainting.getToArtist(); |
| |
| <SPAN class="code-comment">// assign <SPAN class="code-keyword">new</SPAN> painting to an artist |
| </SPAN> artist.addToPaintingArray(newPainting); |
| |
| <SPAN class="code-comment">// save all the changes we've made so far |
| </SPAN> ctxt.commitChanges(); |
| } |
| ... |
| </PRE> |
| </DIV></DIV> |
| <P>If SQL tracing is turned on, and depending on the preexisting data in the database, the following SQL statements might be printed to console during the commit phase:</P> |
| <DIV class="preformatted panel" style="border-width: 1px;"><DIV class="preformattedContent panelContent"> |
| <PRE>[main 12-22 15:50:19] QueryLogger: --- will run 2 queries. |
| [main 12-22 15:50:19] QueryLogger: INSERT INTO PAINTING (PAINTING_ID, PAINTING_TITLE, ESTIMATED_PRICE, |
| ARTIST_ID) VALUES (?, ?, ?, ?) [params: 200, 'Sunset as it is', 5000000, 10] |
| [main 12-22 15:50:19] QueryLogger: === updated 1 row. |
| [main 12-22 15:50:19] QueryLogger: UPDATE PAINTING SET ESTIMATED_PRICE = ? WHERE PAINTING_ID = ? |
| [params: 2000000, 34] |
| [main 12-22 15:50:19] QueryLogger: === updated 1 row. |
| [main 12-22 15:50:19] QueryLogger: +++ transaction committed. |
| </PRE> |
| </DIV></DIV> |