您何时在GAE的Objectify中注册课程? [英] When do you register classes in Objectify for GAE?
问题描述
ObjectifyService.register(User。类);
目前,我正在使用一个类似界面的类的构造函数其他类可以简化特定于我的应用程序的数据存储的使用。但是,我收到此错误:
试图注册种类'用户'两次
所以,我想我的问题是您在Objectify注册课程的频率和具体时间是多少?
谢谢! p>
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();
$ b public void regUser(String email,String password,String firstName,String lastName){
// TODO:检查语法如果电子邮件
// TODO:存储加密的密码
}
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;
$ b $ public User getUser(String email,String password)throws InvalidKeyException,IllegalBlockSizeException,BadPaddingException,NoSuchAlgorithmException,NoSuchPaddingException {
String pass_enc = MyUtils.getEncrypted密码);
Iterable< User> users = ojy.query(User.class).filter(email,email).filter(password,pass_enc);
迭代器<用户> iter = users.iterator();
if(iter.hasNext()){
return iter.next();
} else {
return null;
$ b
更新
以下是最佳实践解决方案:
使用您自己的服务,
这可以确保您的实体在您使用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(); //在v.4.0之前使用.begin(),
//自v.4.0使用ObjectifyService.ofy();
public static ObjectifyFactory factory(){
return ObjectifyService.factory();
}
}
然后像这样使用它:
public User createUser(User pUser){
Objectify objectify = OfyService.ofy();
objectify.put(pUser);
返回pUser;
}
Original Answer(better use上面的代码):
你应该在你的类中这样做,只需要这样一个静态块:
static {
ObjectifyService.register(User.class);
}
ps,您也可以看看客观化的最佳实践
http://code.google .com / p / objectify-appengine / wiki / BestPractices
So this might be kind of a dumb question but when do you register classes with:
ObjectifyService.register( User.class );
Currently, I'm doing this in the constructor of an interface-like class that I use in my other classes to simplify usage of the Data store specifically to my application. However, I'm getting this error:
Attempted to register kind 'User' twice
So, I guess my question is how often and specifically when do you register classes in Objectify?
Thanks!
P.S. Here's my entire class:
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;
}
}
}
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
这篇关于您何时在GAE的Objectify中注册课程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!