blob: cde98201bc294d8c7eac5202b2fe0f5493b098a2 [file] [log] [blame]
Title: Tutorial ObjectContext
<h2>Tutorial ObjectContext</h2>
<P>In this section we'll write a simple main class to run our application, and get a brief introduction to Cayenne ObjectContext.</P>
<H3><A name="TutorialObjectContext-CreatingtheMainClass"></A>Creating the Main Class</H3>
<UL>
<LI>In Eclipse create a new class called &quot;Main&quot; in the <TT>&quot;org.example.cayenne&quot;</TT> package.</LI>
<LI>Create a standard &quot;main&quot; method to make it a runnable class:</LI>
</UL>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java"><SPAN class="code-keyword">package</SPAN> org.example.cayenne;
<SPAN class="code-keyword">public</SPAN> class Main {
<SPAN class="code-keyword">public</SPAN> <SPAN class="code-keyword">static</SPAN> void main(<SPAN class="code-object">String</SPAN>[] args) {
}
}</PRE>
</DIV></DIV>
<UL>
<LI>The first thing you need to be able to access the database is an instance of an <TT>ObjectContext</TT>. In this simple case of a standalone command-line application, it can be obtained by calling a static method on a <A href="datacontext.html" title="DataContext">DataContext</A> class (DataContext is the default implementor of the ObjectContext interface) :</LI>
</UL>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java"><SPAN class="code-keyword">package</SPAN> org.example.cayenne;
<SPAN class="code-keyword">import</SPAN> org.apache.cayenne.ObjectContext;
<SPAN class="code-keyword">import</SPAN> org.apache.cayenne.access.DataContext;
<SPAN class="code-keyword">public</SPAN> class Main {
<SPAN class="code-keyword">public</SPAN> <SPAN class="code-keyword">static</SPAN> void main(<SPAN class="code-object">String</SPAN>[] args) {
ObjectContext context = DataContext.createDataContext();
}
}</PRE>
</DIV></DIV>
<P>ObjectContext is an isolated &quot;session&quot; 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>
<H3><A name="TutorialObjectContext-RunningApplication"></A>Running Application</H3>
<P>Let's check what happens when you run the application. But before we do that we need to add another dependency to the <TT>pom.xml</TT> - Apache Derby, our embedded database engine. The following piece of XML needs to be added to the <TT>&lt;dependencies&gt;...&lt;/dependencies&gt;</TT> section, where we already have Cayenne jars:</P>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">&lt;dependency&gt;
&lt;groupId&gt;org.apache.derby&lt;/groupId&gt;
&lt;artifactId&gt;derby&lt;/artifactId&gt;
&lt;version&gt;10.5.3.0_1&lt;/version&gt;
&lt;/dependency&gt;</PRE>
</DIV></DIV>
<P>Now we are ready to run. Right click the &quot;Main&quot; class in Eclipse and select <TT>&quot;Run As &gt; Java Application&quot;</TT>. In the console you'll see output similar to this, indicating that Cayenne stack has been started:</P>
<DIV class="preformatted panel" style="border-width: 1px;"><DIV class="preformattedContent panelContent">
<PRE>INFO: started configuration loading.
INFO: loaded domain: UntitledDomain
INFO: loaded &lt;map name='UntitledDomainMap' location='UntitledDomainMap.map.xml'&gt;.
INFO: loading &lt;node name='UntitledDomainNode' datasource='UntitledDomainNode.driver.xml'
factory='org.apache.cayenne.conf.DriverDataSourceFactory'
schema-update-strategy='org.apache.cayenne.access.dbsync.CreateIfNoSchemaStrategy'&gt;.
INFO: using factory: org.apache.cayenne.conf.DriverDataSourceFactory
INFO: loading driver information from 'UntitledDomainNode.driver.xml'.
INFO: loading driver org.apache.derby.jdbc.EmbeddedDriver
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
INFO: loaded datasource.
INFO: no adapter set, using automatic adapter.
INFO: loaded map-ref: UntitledDomainMap.
INFO: finished configuration loading in 396 ms.</PRE>
</DIV></DIV>
<DIV class="panelMacro"><TABLE class="noteMacro"><COLGROUP><COL width="24"><COL></COLGROUP><TR><TD valign="top"><IMG src="http://cayenne.apache.org/docs/3.0/images/icons/emoticons/warning.gif" width="16" height="16" align="absmiddle" alt="" border="0"></TD><TD><B>How to Configure Cayenne Logging</B><BR>You can tweak more or less detailed output by following the instructions in the <A href="configuring-logging.html" title="Configuring Logging">logging chapter</A>.</TD></TR></TABLE></DIV>
<P>Nothing much happened here, but we've been able to create a working Cayenne stack. In the following chapters we'll use the ObjectContext for more interesting things.</P>
<HR>
<P><B>Next Step: <A href="tutorial-persistent-objects.html" title="Tutorial Persistent Objects">Tutorial Persistent Objects</A></B></P>
<HR>