blob: 2aea92f3f3d84d90a2f399cc01f620b917046298 [file] [log] [blame]
---
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
title: Tutorial DataContext
url: /docs/1.2/tutorial-datacontext.html
layout: docs_legacy
---
<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>