| <?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. |
| --> |
| <section 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 IDEA create a new class called "<code>Main</code>" in the "<code>org.example.cayenne</code>" |
| 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 |
| <code>ServerRuntime</code> object (which is essentially a wrapper around Cayenne stack) and |
| use it to obtain an instance of an |
| <code>ObjectContext</code>.<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><code>ObjectContext</code> 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 <code>pom.xml</code> - Apache Derby, our embedded database engine. |
| The following piece of XML needs to be added to the |
| <code><dependencies>...</dependencies></code> section, where we already have Cayenne |
| jars:<programlisting language="xml"><dependency> |
| <groupId>org.apache.derby</groupId> |
| <artifactId>derby</artifactId> |
| <version>10.13.1.1</version> |
| </dependency></programlisting>Now |
| we are ready to run. Right click the "Main" class in IDEA and select "Run 'Main.main()'". |
| </para> |
| <para> |
| <inlinemediaobject> |
| <imageobject> |
| <imagedata fileref="images/idea-file-run-menu.png"/> |
| </imageobject> |
| </inlinemediaobject> |
| </para> |
| <para> |
| In the console you'll see output similar to this, indicating that Cayenne stack has been started: |
| <screen>INFO: Loading XML configuration resource from file:/.../cayenne-project.xml |
| INFO: Loading XML DataMap resource from file:/.../datamap.map.xml |
| INFO: loading user name and password. |
| INFO: Connecting to 'jdbc:derby:memory:testdb;create=true' as 'null' |
| INFO: +++ Connecting: SUCCESS. |
| INFO: setting DataNode 'datanode' as default, used by all unlinked DataMaps</screen> |
| |
| <note> |
| <title>How to Configure Cayenne Logging</title> |
| <para>Follow the instructions in the logging chapter to tweak verbosity of the logging output.</para> |
| </note> |
| </para> |
| </section> |
| </section> |