3
votes

I recently got objectify working with app engine, but I'm having trouble with registering a class for objectify multiple times. While developing in Eclipse, which recompiles and runs again every time the localhost is refreshed, the script that registers the student is run multiple times, and crashes the program after just one refresh.

<%
//In my main.jsp file, which is the main interactive html page
ObjectifyService.register(Object.class);
%>

How can I ensure that this script is only run once? Is there a way to check if a class is registered with objectify? I followed a suggestion on another stackoverflow thread to do the following:

public class Object {
    ...
    static {
        ObjectifyService.register(Object.class);
    }
    ...
}

This gave me a different error. How can I solve this?

2
"Crashes"? "A different error"? Be specific! Include stacktraces! - Nick Johnson

2 Answers

1
votes

Put it on your ServletContextListener, specifically, on the contextInitialized() hook. This will ensure that the Objectify register code is only executed once when the server is warming up.

1
votes

by looking on a reliable example like this:

Objectify in JSP

you can find similar attempt to register the class, BUT... Read the comments from the example:

// BE CAREFUL with this line! This a example, but in a real world project, you should look a better
// place for register an entity, at the very beginning of your application is recommended.

So... You probably have a java class in which you implemented several methods like: get/put/delete etc... and in that java class you should place the code to register the class

    static {
        ObjectifyService.register(Object.class);
    }

OR, look for some other place that is being called once upon the application starts