Slim3 Datastore‎ > ‎

Installation

If you want to use Slim3 as just a datastore framework, please continue to read this section. If you want to use Slim3 as a full-stack MVC framework, please try Getting Started first, and skip this page.


To use Slim3 as just a datastore framework, an application needs the following:

1.Download the latest version of Slim3 blank project(slim3-blank-xxx.zip) and unzip it

2.Copy war/WEB-INF/lib/slim3-xxx.jar to "your project"/war/WEB-INF/lib

3.Add slim3-xxx.jar to Java Build Path


4.Set Eclipse for testing

In the Window menu, select Preferences > Java > Code Style > Organize Imports. Set 1 to" Number of static imports needed for .*".


In the Window menu, select Preferences > Java > Editor > Content Assist > Favorites. Click "New Type" button and add org.hamcrest.CoreMatchers, org.junit.Assert, org.junit.matchers.JUnitMatchers.



In the Window menu, select Preferences > General > Workspace. Click "Refresh automatically" checkbox.


5.Enable annotation processing

In the Project Properties menu, select Java Compiler > Annotation Processing > Enable annotation processing, and input "src" in the "Generated source directory".


6.Add slim3-gen-xxx.jar in lib to the annotation processing factory path


In the Project Properties menu, select Java Compiler > Annotation Processing > Factory Path.



Add slim3-gen-xxx.jar to the path.


7.Define DatastoreFilter in war/WEB-INF/web.xml. This filter rolls back active transactions automatically when an exception occurred:
<filter>
<filter-name>datastoreFilter</filter-name>
<filter-class>org.slim3.datastore.DatastoreFilter</filter-class>
</filter>
<filter-mapping>
        <filter-name>datastoreFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
</filter-mapping>


Create a model and a test

Double-click build.xml under the project.

In the Outline view, right-click gen-model task and select Run as > Ant Build(first item)

In the ant input dialog, input "your favorite name", and click OK button.

If your project is not refreshed automatically, in the Window menu, select Preferences > General > Workspace. Click "Refresh automatically" checkbox. Or refresh your project manually.

"src/tutorial/model/Message.java" and "test/tutorial/model/MessageTest.java" will appear. The part of "Message" is "your favorite name".

Let's take a look at "src/tutorial.model/Message.java".

Message.java

package tutorial.model;

import java.io.Serializable;

import com.google.appengine.api.datastore.Key;

import org.slim3.datastore.Attribute;
import org.slim3.datastore.Model;

@Model(schemaVersion = 1)
public class Message implements Serializable {

    private static final long serialVersionUID = 1L;

    @Attribute(primaryKey = true)
    private Key key;

    @Attribute(version = true)
    private Long version;

    /**
     * Returns the key.
     *
     * @return the key
     */
    public Key getKey() {
        return key;
    }

    /**
     * Sets the key.
     *
     * @param key
     *            the key
     */
    public void setKey(Key key) {
        this.key = key;
    }

    /**
     * Returns the version.
     *
     * @return the version
     */
    public Long getVersion() {
        return version;
    }

    /**
     * Sets the version.
     *
     * @param version
     *            the version
     */
    public void setVersion(Long version) {
        this.version = version;
    }
}

Let's take a look at "test/tutorial.model/MessageTest.java".

MessageTest.java

package tutorial.model;

import org.junit.Test;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

public class MessageTest {

    private Message model = new Message();

    @Test
    public void test() throws Exception {
        assertThat(model, is(notNullValue()));
    }
}

Run the "MessageTest.java". The result will be green(OK).

Next...

Continue to Defining Data Classes.



Comments