16

したがって、これはちょっとばかげた質問かもしれませんが、いつクラスを登録しますか:

ObjectifyService.register( User.class );

現在、これは、他のクラスで使用しているインターフェイスのようなクラスのコンストラクターで行っており、アプリケーション専用のデータストアの使用を簡素化しています。ただし、次のエラーが発生します。

種類「ユーザー」を2回登録しようとしました

ですから、私の質問は、Objectifyにクラスを登録する頻度と具体的な時期についてだと思います。

ありがとう!

PSこれが私のクラス全体です:

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Iterator;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.persistence.Id;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.annotation.Indexed;
import com.googlecode.objectify.annotation.Unindexed;

public class UsersService {

    Objectify ojy;

    public UsersService(){
        ObjectifyService.register( User.class );
        ojy = ObjectifyService.begin();
    }

    public void regUser(String email, String password, String firstName, String lastName){
        //TODO: Check syntax if email
        //TODO: store encrypted password
    }

    public void regUser(String email, String password, String firstName){
        regUser(email, password, firstName, null);
    }

    public void regUser(String email, String password){
        regUser(email, password, "", "");
    }

    public boolean checkFor(Long acc_id){
        User checked_user = ojy.find(User.class, acc_id);
        if(checked_user == null){
            return false;
        }else{
            return true;
        }
    }

    public User getUser(String email, String password) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException{
        String pass_enc = MyUtils.getEncrypted(password);
        Iterable<User> users = ojy.query(User.class).filter("email", email).filter("password", pass_enc);
        Iterator<User> iter = users.iterator();
        if(iter.hasNext()){
            return iter.next();
        }else{
            return null;
        }
    }

}
4

3 に答える 3

23

アップデート

ベスト プラクティス ソリューションは次のとおりです。

Use Your Own Service 。これにより、Objectify を使用する前にエンティティが登録されることが保証されますが、データストアにアクセスしない要求のアプリケーションの起動には必ずしも影響しません。

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyFactory;
import com.googlecode.objectify.ObjectifyService;


public class OfyService {
    static {
        ObjectifyService.register(User.class);
    }

    public static Objectify ofy() {
        return ObjectifyService.begin();//prior to v.4.0 use .begin() , 
                                        //since v.4.0  use ObjectifyService.ofy();
    }

    public static ObjectifyFactory factory() {
        return ObjectifyService.factory();
    }

}

次に、次のように使用します。

public User createUser(User pUser) {

    Objectify objectify = OfyService.ofy();
    objectify.put(pUser);

    return pUser;
}

元の回答(上記のコードを使用することをお勧めします):

クラスでこのようにする必要があります。次のように静的ブロックを配置するだけです。

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

ps 、オブジェクト化のベストプラクティスもご覧ください

http://code.google.com/p/objectify-appengine/wiki/BestPractices

于 2011-11-05T00:11:46.227 に答える
1

すべての情報がコンパイル/ビルド時に収集されるため、アプリケーションの起動時間に大きな影響を与えることなく、@Entity注釈、Reflectionsライブラリ、およびランタイム登録を使用します。

ObjectifyLoaderContextListener.java

package com.vertigrated.servlet;
 
import com.google.appengine.api.ThreadManager;
import com.googlecode.objectify.ObjectifyFactory;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.annotation.Entity;
import org.reflections.Reflections;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import javax.annotation.Nonnull;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
/**
 * This class processes the classpath for classes with the @Entity or @Subclass annotations from Objectify
 * and registers them with the ObjectifyFactory, it is multi-threaded uses a prebuilt list of classes to process
 * created by the Reflections library at compile time and works very fast!
 */
public class ObjectifyLoaderContextListener implements ServletContextListener
{
    private static final Logger L = LoggerFactory.getLogger(ObjectifyLoaderContextListener.class);
 
    private final Set<Class<?>> entities;
 
    public ObjectifyLoaderContextListener()
    {
        this.entities = new HashSet<>();
    }
 
    @Override
    public void contextInitialized(@Nonnull final ServletContextEvent sce)
    {
        final ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setUrls(ClasspathHelper.forPackage(""));
        final ExecutorService es = Executors.newCachedThreadPool(ThreadManager.currentRequestThreadFactory());
        cb.setExecutorService(es);
        final Reflections r = new Reflections(cb);
        this.entities.addAll(r.getTypesAnnotatedWith(Entity.class));
        es.shutdown();
        final ObjectifyFactory of = ObjectifyService.factory();
        for (final Class<?> cls : this.entities)
        {
            of.register(cls);
            L.debug("Registered {} with Objectify", cls.getName());
        }
    }
 
    @Override
    public void contextDestroyed(@Nonnull final ServletContextEvent sce)
    {
        /* this is intentionally empty */
    }
}
于 2015-04-12T10:39:19.100 に答える