<?xml version="1.0" encoding="UTF-8"?>
<?xml-model href="http://www.oasis-open.org/docbook/xml/5.0/rng/docbook.rng" schematypens="http://relaxng.org/ns/structure/1.0"?>
<!--
    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.
-->
<chapter xmlns="http://docbook.org/ns/docbook"
    xmlns:xlink="http://www.w3.org/1999/xlink" version="5.0">
    <title>Getting started with ObjectContext</title>
    <para>In this section we'll write a simple main class to run our application, and get a brief
    introduction to Cayenne ObjectContext.</para>
    <section xml:id="creating-main-class">
        <title>Creating the Main Class</title>
        <itemizedlist>
            <listitem>
                <para>In Eclipse create a new class called "Main" in the "org.example.cayenne"
                    package.</para>
            </listitem>
            <listitem>
                <para>Create a standard "main" method to make it a runnable
                    class:<programlisting language="java">package org.example.cayenne;

public class Main {

    public static void main(String[] args) {

    }
}</programlisting></para>
            </listitem>
            <listitem>
                <para>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.<programlisting language="java">package org.example.cayenne;

import org.apache.cayenne.ObjectContext;
import org.apache.cayenne.configuration.server.ServerRuntime;

public class Main {

    public static void main(String[] args) {
        ServerRuntime cayenneRuntime = new ServerRuntime(
                "cayenne-project.xml");
        ObjectContext context = cayenneRuntime.newContext();
    }
}</programlisting></para>
                <para>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.
                </para>
            </listitem>
        </itemizedlist>
    </section>
    <section xml:id="runnning-app">
        <title>Running Application</title>
        <para>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
            &lt;dependencies&gt;...&lt;/dependencies&gt; section, where we already have Cayenne
            jars:<programlisting>&lt;dependency&gt;
   &lt;groupId&gt;org.apache.derby&lt;/groupId&gt;
   &lt;artifactId&gt;derby&lt;/artifactId&gt;
   &lt;version&gt;10.8.1.2&lt;/version&gt;
&lt;/dependency&gt;</programlisting>Now
            we are ready to run. Right click the "Main" class in Eclipse and select "Run As &gt;
            Java Application". In the console you'll see output similar to this, indicating that
            Cayenne stack has been
                started:<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</programlisting>
            
            <note><para><emphasis role="bold">How to Configure Cayenne Logging.</emphasis>  Follow the instructions in
                    the logging chapter to tweak verbosity of the logging output.</para></note>
           </para>
    </section>
</chapter>
