-
Notifications
You must be signed in to change notification settings - Fork 160
Setup
<dependencies>
<dependency>
<groupId>com.googlecode.objectify</groupId>
<artifactId>objectify</artifactId>
<version>check for latest version</version>
</dependency>
</dependencies>ObjectifyFactory is the abstraction for a single datastore. Most applications only interact with a single datastore, so Objectify provides a convenient singleton - the ObjectifyService.
Most of this documentation assumes you will use ObjectifyService, but if you wish to manage ObjectifyFactory instances yourself, see MultipleDatastores
Call ObjectifyService.init() in the bootstrap of your application. The default init() will connect to the default DatastoreService configured for your environment, using Google's default discovery rules. Those are:
- If you're running in App Engine, the project id will be obtained from the environment
- If you aren't running in App Engine, the project id will be drawn from the environment variable
GOOGLE_CLOUD_PROJECT - The default database id
(default)will be used
If you need custom connection logic (or wish to enable the memcache service), construct an ObjectifyFactory and pass it to init(). See the constructor javadoc for ObjectifyFactory for more information.
After you have initialized the ObjectifyService, register your entity classes.
In a web application, you can use a servlet context listener:
<listener>
<listener-class>com.example.YourBootstrapper</listener-class>
</listener>public class YourBootstrapper implements ServletContextListener {
public void contextInitialized(ServletContextEvent event) {
ObjectifyService.init();
ObjectifyService.register(YourEntity.class);
// etc...
}
}Before you can use the ofy() method, you must start a session. The simplest way to start a session is manually:
import static com.googlecode.objectify.ObjectifyService.ofy;
ObjectifyService.run(() -> {
// Do some work with Objectify
Foo foo = ofy().load().type(Foo.class).id(123).now();
...
});Alternatively:
import static com.googlecode.objectify.ObjectifyService.ofy;
try (Closeable session = ObjectifyService.begin()) {
// Do some work with Objectify
Foo foo = ofy().load().type(Foo.class).id(123).now();
...
}Sessions are thread-local state. ObjectifyService.ofy() will return an Objectify command instance appropriate to the current transaction context.
If you're building a web application, Objectify provides a servlet filter that creates a session for each request. Add this to your WEB-INF/web.xml:
<filter>
<filter-name>ObjectifyFilter</filter-name>
<filter-class>com.googlecode.objectify.ObjectifyService$Filter</filter-class>
</filter>
<filter-mapping>
<filter-name>ObjectifyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>There's also a ObjectifyService$FilterJavax that works with the old javax.servlet.* API instead of jakarta.servlet.*.
Alternatively, you can provide a WebFilter with the following code:
import javax.servlet.annotation.WebFilter; // or jakarta.servlet.annnotation.WebFilter
import com.googlecode.objectify.ObjectifyService;
@WebFilter(urlPatterns = {"/*"})
public class ObjectifyFilter extends ObjectifyService.Filter {}If you would like to build Objectify from source, see ContributingToObjectify.
- Home
- Concepts
- User's Guide
- Examples
- API Javadoc
- Release Notes
- Best Practices
- Frequently Asked Questions
- Extensions
- Contributing To Objectify
- User's Guide (Objectify v4)
- User's Guide (Objectify v3)