3

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?

4

2 に答える 2

1

ServletContextListener 具体的には contextInitialized() フックに配置します。これにより、サーバーがウォームアップしているときに Objectify 登録コードが 1 回だけ実行されるようになります。

于 2012-01-27T10:03:29.803 に答える
1

次のような信頼できる例を見ると:

JSPでオブジェクト化

クラスを登録する同様の試みを見つけることができますが、... 例のコメントを読んでください。

// この行には注意してください!
これは一例ですが、実際のプロジェクトでは、エンティティを登録する // 場所をアプリケーションの最初のほうに置くことをお勧めします。

だから...おそらく、get/put/deleteなどのいくつかのメソッドを実装したJavaクラスがあり、そのJavaクラスにクラスを登録するコードを配置する必要があります

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

または、アプリケーションの起動時に一度呼び出されている他の場所を探します

于 2012-01-27T06:13:09.767 に答える