blob: 8ad535c260d210931b6ebd3ce16d5d6531e31d30 [file] [log] [blame]
Title: Tutorial DataContext
<UL>
<LI>In Eclipse create a new class called &quot;Main&quot; in the <TT>&quot;cayenne.tutorial&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="codeHeader panelHeader" style="border-bottom-width: 1px;"><B>Main.java</B></DIV><DIV class="codeContent panelContent">
<PRE class="code-java"><SPAN class="code-keyword">package</SPAN> cayenne.tutorial;
<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 a <TT><A href="datacontext.html" title="DataContext">DataContext</A></TT> instance. In this simple case of a standalone command line application, it can be obtained by calling a static method:</LI>
</UL>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeHeader panelHeader" style="border-bottom-width: 1px;"><B>Main.java</B></DIV><DIV class="codeContent panelContent">
<PRE class="code-java"><SPAN class="code-keyword">package</SPAN> cayenne.tutorial;
<SPAN class="code-keyword">import</SPAN> org.objectstyle.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) {
DataContext context = DataContext.createDataContext();
}
}</PRE>
</DIV></DIV>
<P>DataContext is a single session a user needs to work with Cayenne. DataContext has methods to execute queries and manage persistent objects. We'll discuss them in the following chapters. When the first DataContext is created in the application, Cayenne loads XML mapping files and creates an access stack that can later be reused for other DataContexts. </P>
<H3><A name="TutorialDataContext-RunningApplication"></A>Running Application</H3>
<P>Let's check what happens when you run the application. First you need to close CayenneModeler (as Derby does not allow multiple applications accessing the same database in the embedded mode). After that 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 QueryLogger: Created connection pool: jdbc:derby:testdb;create=true
Driver class: org.apache.derby.jdbc.EmbeddedDriver
Min. connections in the pool: 1
Max. connections in the pool: 1</PRE>
</DIV></DIV>
<P>The log omits most stack loading details. You can configure a more detailed output by following the instructions in the <A href="configuring-logging.html" title="Configuring Logging">logging chapter</A>.</P>
<HR>
<P><B>Next Step: <A href="tutorial-dataobjects.html" title="Tutorial DataObjects">Tutorial DataObjects</A></B></P>
<HR>