When do you register classes in Objectify for GAE?

后端 未结 3 1382
无人共我
无人共我 2021-02-05 12:08

So this might be kind of a dumb question but when do you register classes with:

ObjectifyService.register( User.class );

Currently, I\'m doing

3条回答
  •  庸人自扰
    2021-02-05 12:33

    Update

    Here is the Best Practice Solution:

    Use Your Own Service , This guarantees that your entities are registered before you use Objectify, but doesn't necessarily impact application startup for requests which do not access the datastore.

    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();
        }
    
    }
    

    Then use it like this:

    public User createUser(User pUser) {
    
        Objectify objectify = OfyService.ofy();
        objectify.put(pUser);
    
        return pUser;
    }
    

    Original Answer (better use the code above):

    you should do it this way in your class, just put a static block like this:

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

    p.s , you take a look at the best practice of objectify too

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

提交回复
热议问题