| <html><head> |
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
| <title xmlns:d="http://docbook.org/ns/docbook">Chapter 5. Getting started with ObjectContext</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="getting-started-part3.html" title="Part III. Learning Cayenne API"><link rel="next" href="ch06.html" title="Chapter 6. Getting started with persistent 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 5. Getting started with ObjectContext</th><th></th></tr><tr><td width="20%" align="left"><a accesskey="p" href="getting-started-part3.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="ch06.html">Next</a></td></tr></table><hr></div><div class="chapter" title="Chapter 5. Getting started with ObjectContext"><div class="titlepage"><div><div><h2 class="title"><a name="d0e318"></a>Chapter 5. Getting started with ObjectContext</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="ch05.html#creating-main-class">Creating the Main Class</a></span></dt><dt><span class="section"><a href="ch05.html#runnning-app">Running Application</a></span></dt></dl></div><p>In this section we'll write a simple main class to run our application, and get a brief |
| introduction to Cayenne ObjectContext.</p><div class="section" title="Creating the Main Class"><div class="titlepage"><div><div><h2 class="title"><a name="creating-main-class"></a>Creating the Main Class</h2></div></div></div><div class="itemizedlist"><ul class="itemizedlist"><li class="listitem"><p>In Eclipse create a new class called "Main" in the "org.example.cayenne" |
| package.</p></li><li class="listitem"><p>Create a standard "main" method to make it a runnable |
| class:</p><pre class="programlisting"><span xmlns="http://www.w3.org/1999/xhtml" class="hl-keyword">package</span> org.example.cayenne; |
| |
| <span xmlns="http://www.w3.org/1999/xhtml" class="hl-keyword">public</span> <span xmlns="http://www.w3.org/1999/xhtml" class="hl-keyword">class</span> Main { |
| |
| <span xmlns="http://www.w3.org/1999/xhtml" class="hl-keyword">public</span> <span xmlns="http://www.w3.org/1999/xhtml" class="hl-keyword">static</span> <span xmlns="http://www.w3.org/1999/xhtml" class="hl-keyword">void</span> main(String[] args) { |
| |
| } |
| }</pre></li><li class="listitem"><p>The first thing you need to be able to access the database is to create a |
| ServerRuntime object (which is essentially a wrapper around Cayenne stack) and |
| use it to obtain an instance of an |
| ObjectContext.</p><pre class="programlisting"><span xmlns="http://www.w3.org/1999/xhtml" class="hl-keyword">package</span> org.example.cayenne; |
| |
| <span xmlns="http://www.w3.org/1999/xhtml" class="hl-keyword">import</span> org.apache.cayenne.ObjectContext; |
| <span xmlns="http://www.w3.org/1999/xhtml" class="hl-keyword">import</span> org.apache.cayenne.configuration.server.ServerRuntime; |
| |
| <span xmlns="http://www.w3.org/1999/xhtml" class="hl-keyword">public</span> <span xmlns="http://www.w3.org/1999/xhtml" class="hl-keyword">class</span> Main { |
| |
| <span xmlns="http://www.w3.org/1999/xhtml" class="hl-keyword">public</span> <span xmlns="http://www.w3.org/1999/xhtml" class="hl-keyword">static</span> <span xmlns="http://www.w3.org/1999/xhtml" class="hl-keyword">void</span> main(String[] args) { |
| ServerRuntime cayenneRuntime = <span xmlns="http://www.w3.org/1999/xhtml" class="hl-keyword">new</span> ServerRuntime( |
| <span xmlns="http://www.w3.org/1999/xhtml" class="hl-string">"cayenne-project.xml"</span>); |
| ObjectContext context = cayenneRuntime.getContext(); |
| } |
| }</pre><p>ObjectContext is an isolated "session" in Cayenne that provides all needed API |
| to work with data. ObjectContext has methods to execute queries and manage |
| persistent objects. We'll discuss them in the following sections. When the first |
| ObjectContext is created in the application, Cayenne loads XML mapping files and |
| creates a shared access stack that is later reused by other ObjectContexts. |
| </p></li></ul></div></div><div class="section" title="Running Application"><div class="titlepage"><div><div><h2 class="title"><a name="runnning-app"></a>Running Application</h2></div></div></div><p>Let's check what happens when you run the application. But before we do that we need |
| to add another dependency to the pom.xml - Apache Derby, our embedded database engine. |
| The following piece of XML needs to be added to the |
| <dependencies>...</dependencies> section, where we already have Cayenne |
| jars:</p><pre class="programlisting"><dependency> |
| <groupId>org.apache.derby</groupId> |
| <artifactId>derby</artifactId> |
| <version>10.8.1.2</version> |
| </dependency></pre><p>Now |
| we are ready to run. Right click the "Main" class in Eclipse and select "Run As > |
| Java Application". In the console you'll see output similar to this, indicating that |
| Cayenne stack has been |
| started:</p><pre class="programlisting">INFO: Loading XML configuration resource from file:cayenne-project.xml |
| INFO: loading user name and password. |
| INFO: Created connection pool: jdbc:derby:memory:testdb;create=true |
| Driver class: org.apache.derby.jdbc.EmbeddedDriver |
| Min. connections in the pool: 1 |
| Max. connections in the pool: 1</pre><p> |
| |
| </p><div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p><span class="bold"><strong>How to Configure Cayenne Logging.</strong></span> Follow the instructions in |
| the logging chapter to tweak verbosity of the logging output.</p></div><p> |
| </p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="getting-started-part3.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="ch06.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Part III. Learning Cayenne API </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 6. Getting started with persistent objects</td></tr></table></div></body></html> |