In this section we'll write a simple main class to run our application, and get a brief introduction to Cayenne ObjectContext.
- In Eclipse create a new class called "Main" in the "org.example.cayenne" package.
- Create a standard "main" method to make it a runnable class:
- The first thing you need to be able to access the database is an instance of an ObjectContext. In this simple case of a standalone command-line application, it can be obtained by calling a static method on a DataContext class (DataContext is the default implementor of the ObjectContext interface) :
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.
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 <dependencies>...</dependencies> section, where we already have Cayenne jars:
Now we are ready to run. Right click the "Main" class in Eclipse and select "Run As > Java Application". In the console you'll see output similar to this, indicating that Cayenne stack has been started:
|How to Configure Cayenne Logging|
You can tweak more or less detailed output by following the instructions in the logging chapter.
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.
Next Step: Tutorial Persistent Objects