Cayenne User Documentation
Tutorial DataContext

- In Eclipse create a new class called "Main" in the `"cayenne.tutorial"` package.
- Create a standard "main" method to make it a runnable class:

Main.java

    package cayenne.tutorial;

    public class Main {

    	public static void main(String[] args) {

    	}
    }

- The first thing you need to be able to access the database is a `DataContext` instance. In this simple case of a standalone command line application, it can be obtained by calling a static method:

Main.java

    package cayenne.tutorial;

    import org.apache.cayenne.access.DataContext;

    public class Main {

    	public static void main(String[] args) {

    		DataContext context = DataContext.createDataContext();
    	}
    }

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.

### Running Application

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 "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:

    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

The log omits most stack loading details. You can configure a more detailed output by following the instructions in the logging chapter.

Next Step: Tutorial DataObjects

.
Copyright ©2001-2008 Apache Software Foundation