首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GAE+Objectify -不支持参数化类型com.googlecode.objectify.Ref

GAE+Objectify -不支持参数化类型com.googlecode.objectify.Ref
EN

Stack Overflow用户
提问于 2014-07-05 20:59:49
回答 2查看 2.1K关注 0票数 2

我正在使用GoogleAppEngine1.9.3,Eclipse,Objectify5.03。我的班级如下:

代码语言:javascript
复制
import com.googlecode.objectify.Ref;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Load;

@Entity
public class User {

@Id private Long userId;
private String userName;
@Load private Ref<UserDetails> userDetails;
@Load private Ref<UserPassword> userPassword;

//getters & setters 

}

当我尝试通过Eclipse为该类创建google端点时,会得到以下错误:java.lang.IllegalArgumentException:参数化类型com.googlecode.objectify.Ref不支持

这是我第一次尝试物化。

我做错了什么。从我到目前为止所读到的,GAE端点和对象化应该能工作,对吗?

EN

回答 2

Stack Overflow用户

发布于 2014-07-07 19:28:49

Google无法序列化Ref对象,因为它是由objectify定义的任意对象,因此不支持错误指示的对象。

这是已知的具有云端点的限制,因为它不允许使用自定义对象。如果您感兴趣的话,这里有一个关于这一点的讨论主题:Cloud endpoints .api generation exception when using objectify (4.0b1) parameterized key

您必须用@ApiResourceProperty注释您的方法,并将其忽略的属性设置为true,如下面的代码所示:

代码语言:javascript
复制
import com.googlecode.objectify.Ref;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Load;
import com.google.api.server.spi.config.AnnotationBoolean;
import com.google.api.server.spi.config.ApiResourceProperty;

@Entity
public class User 
{
    @Id private Long userId;
    private String userName;
    @Load private Ref<UserDetails> userDetails;
    @Load private Ref<UserPassword> userPassword;

    //getters & setters
    @ApiResourceProperty(ignored = AnnotationBoolean.TRUE) 
    public UserDetail getUserDetails(){
    }

    @ApiResourceProperty(ignored = AnnotationBoolean.TRUE) 
    public UserPassword getUserPassword(){
    }
}

如果仍然希望使用这些对象中保存的数据,请考虑向类中添加一些字段以保存数据,并在您的User类完成这样的加载之后初始化它们:

代码语言:javascript
复制
@Ignore String firstName;
@OnLoad
void trackUserDetails() 
{ 
    this.firstName = getUserDetails().getFirstName(); 
    // add more code here to set other fields, you get the gist
}

但在我看来,一个更好的方法是重新考虑你的班级的设计,或者更确切地说,重新考虑你想要做的事情。

票数 10
EN

Stack Overflow用户

发布于 2014-07-24 14:30:30

ApiResourceProperty注释不适用于Google Emdpoints+Objectify组合,因为Ref或Key是对象化的特定类,而Google不识别它们,在尝试生成客户端库时会出现错误。我更改了用户类,如下所示。

代码语言:javascript
复制
 @Id private Long userId;
 @Index private String userName;
 @Load private UserDetails userDetails;
 @Load private UserPassword userPassword;
 @Load private ArrayList<Account> userAccounts;

 //getters and setters

当我按用户名检索用户时,我通过getter获得用户、UserDetails、UserPassword以及用户帐户列表(一次)

代码语言:javascript
复制
@ApiMethod(name = "getUserByName", path = "get_user_by_name")
public User getUserByName(@Named("userName") String userName) {

    User user = null;
    try {
         user = ofy().load().type(User.class).filter("userName", userName).first().now();
         if(user != null)
             log.info("UserEndpoint.getUserByName...user retrieved="+user.getUserId());
         else
             log.info("UserEndpoint.getUserByName...user is null");
    } catch(Exception e) {
        log.info("UserEndpoint.getUserByName...exception="+e.getMessage());
    }
    return user;
}

当我使用Google上的Datastore Viewer查看数据时,我会在User表中的userDetails、userPassword和Accounts列中看到一些条目。我假设这些都是对各自表中的实际数据的引用,而不是数据本身的副本。希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24590518

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档