论坛首页 Java企业应用论坛

Play!跑在GAE上,小试GAE

浏览 27512 次
精华帖 (0) :: 良好帖 (6) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-07-11   最后修改:2009-07-11
哦?没有注意这个问题,呵呵~我也是刚接触这两个东西,那个Test应该是GAE提供的包吧?play可以引入别的数据类型的,理论上,如果View层没法迭代出来,最多在

postVo.setContent(postItem.content.toString());//假设有类似的方法

的时候转一下就好了,这些都不是问题,任何一个框架理论上都不会遇到适别数据类型的问题的,也就是说Play!不会说不能使用Test类型的,他大不了当是自定义的一个类处理。
0 请登录后投票
   发表时间:2009-07-11   最后修改:2009-07-11
在show方法我改成这样了:

public static void show(Long id) {
        Post post = Post.findById(id);
        notFoundIfNull(post);
        List<Post> sourcPosts = Post.findAll();

        List<Comment> comments = Comment.findBy("postId", post.id);
        post.comments = comments;

        List<Post> posts = new ArrayList<Post>();
        posts.addAll(sourcPosts);
        posts.remove(post);
        Collections.reverse(posts);
        render(post, posts);
    }

newComment方法改成这样:

public static void newComment(Long id, String author, String comment) {
        Post post = Post.findById(id);
        notFoundIfNull(post);
        Comment co = new Comment(author, comment, id);
        co.save();
        show(id);
    }

Post.java

@Transient
    public List<Comment> comments;

但是
public String content;
这个要大文本,GAE要用
public Text content;

但是play!处理不了这个。

Text的getValue()还没试好。

Post post = Post.findById(id);
这样出来的post,post的content为null,还待研究,看来以后真要不修改随处运行是个神话。
0 请登录后投票
   发表时间:2009-07-11  
pure 写道
在show方法我改成这样了:

public static void show(Long id) {
        Post post = Post.findById(id);
        notFoundIfNull(post);
        List<Post> sourcPosts = Post.findAll();

        List<Comment> comments = Comment.findBy("postId", post.id);
        post.comments = comments;

        List<Post> posts = new ArrayList<Post>();
        posts.addAll(sourcPosts);
        posts.remove(post);
        Collections.reverse(posts);
        render(post, posts);
    }

newComment方法改成这样:

public static void newComment(Long id, String author, String comment) {
        Post post = Post.findById(id);
        notFoundIfNull(post);
        Comment co = new Comment(author, comment, id);
        co.save();
        show(id);
    }

Post.java

@Transient
    public List<Comment> comments;

但是
public String content;
这个要大文本,GAE要用
public Text content;

但是play!处理不了这个。

Text的getValue()还没试好。

Post post = Post.findById(id);
这样出来的post,post的content为null,还待研究,看来以后真要不修改随处运行是个神话。


呵呵~恕我直说吧,你这个解决方法,其实不如使用传统的DTO模式,很明显GAE的存储层没法“到处调用”,那就应该把它隔离出来,原来J2EE的分层就是这个原因~

另外500字符问题已经解决(有点累,找了不少资料),Text不是Play!不能识别,原因如我前面所说~~
可以把GAE的appengine-api-1.0-sdk-1.2.1.jar拷过来,里面有com.google.appengine.api.datastore.Text这个类,在Post里面把content的类型设为他就好。。。

你会发现GAE这个字段没被存起来,这时候要坚定信念,因为前面已经分析过原因。去找GAE的问题,Google不到,只有JDO的,那就去Google Group去找,里面全是GAE的高手,包括开发者,搜一下就会发现有人遇到你的问题了。。。

在这个字段前面加上@Enumerated就可以解决。。。

后面的事情如同我前面说的,大不了复制为VO的时候转一下就好(这时候,你的解决方法就不行了,分层还是有好处的:)):
postVo.setContent(postItem.content.getValue());
这样就解决了,记得把View里面所有的调用改为调用postVo的,否则可能出现转型问题,检查清楚~~~

一切成功~

结论:方法和思路才是最重要的~~
0 请登录后投票
   发表时间:2009-07-11  
Laynepeng 写道
pure 写道
在show方法我改成这样了:

public static void show(Long id) {
        Post post = Post.findById(id);
        notFoundIfNull(post);
        List<Post> sourcPosts = Post.findAll();

        List<Comment> comments = Comment.findBy("postId", post.id);
        post.comments = comments;

        List<Post> posts = new ArrayList<Post>();
        posts.addAll(sourcPosts);
        posts.remove(post);
        Collections.reverse(posts);
        render(post, posts);
    }

newComment方法改成这样:

public static void newComment(Long id, String author, String comment) {
        Post post = Post.findById(id);
        notFoundIfNull(post);
        Comment co = new Comment(author, comment, id);
        co.save();
        show(id);
    }

Post.java

@Transient
    public List<Comment> comments;

但是
public String content;
这个要大文本,GAE要用
public Text content;

但是play!处理不了这个。

Text的getValue()还没试好。

Post post = Post.findById(id);
这样出来的post,post的content为null,还待研究,看来以后真要不修改随处运行是个神话。


呵呵~恕我直说吧,你这个解决方法,其实不如使用传统的DTO模式,很明显GAE的存储层没法“到处调用”,那就应该把它隔离出来,原来J2EE的分层就是这个原因~

另外500字符问题已经解决(有点累,找了不少资料),Text不是Play!不能识别,原因如我前面所说~~
可以把GAE的appengine-api-1.0-sdk-1.2.1.jar拷过来,里面有com.google.appengine.api.datastore.Text这个类,在Post里面把content的类型设为他就好。。。

你会发现GAE这个字段没被存起来,这时候要坚定信念,因为前面已经分析过原因。去找GAE的问题,Google不到,只有JDO的,那就去Google Group去找,里面全是GAE的高手,包括开发者,搜一下就会发现有人遇到你的问题了。。。

在这个字段前面加上@Enumerated就可以解决。。。

后面的事情如同我前面说的,大不了复制为VO的时候转一下就好(这时候,你的解决方法就不行了,分层还是有好处的:)):
postVo.setContent(postItem.content.getValue());
这样就解决了,记得把View里面所有的调用改为调用postVo的,否则可能出现转型问题,检查清楚~~~

一切成功~

结论:方法和思路才是最重要的~~

非常感谢,我也google了,也如你所说这个字段并没有存起来,学习了解决问题的方法和思路。
看来增加VO是必要的了?不管怎么再次表示感谢。
0 请登录后投票
   发表时间:2009-07-12  
今天下午没啥事做,自己把这两天的经历整合了一下,在GAE上用Play!做了个blog:

http://layneblog.appspot.com/

呵呵~~可以去指教下~

还很不完善,慢慢做吧反正~~
0 请登录后投票
   发表时间:2009-07-14  
pure 写道
Laynepeng 写道
pure 写道
Laynepeng 写道
文章全文在:http://blog.csdn.net/laynepeng/archive/2009/07/10/4338873.aspx

为了不被误会卖广告,把主要有用部份抽出来,如下,

其实GAE的局限主要是:

1. 底层是基于Big Table的,完全不是关系型的结构,让他完全支持JPA的复杂关联关系,太吃力了,所以GAE是不能支持Owned relationship和Many to many owned relationship的,也就是一对多,多对多关系不能支持,官方建议自己建立key,程序里面维护关联关系。Blog这个sample程序,因为存在这Post和Comment两个实体,两者之间是一对多的关系,所以GAE是不能支持的,解决方法很简单,把原来的JPA定义方法去掉,Comment里面加个postId的属性,添加comment时,把这个填上就okay了。
2. GAE限制了java.net.Inet.*这些class,所以Play!自带的ehcache1.5版本是用不了的;ehcache1.6声称支持GAE了,那就下个ehcache-1.6.0.jar替代掉原来Play!自带的那个。
3. GAE里面规定,选取出来的entity不能修改,所以Blog的那个sample里面那种:

posts.remove(post);
Collections.reverse(posts);

是不能用的,需要拿出来自己用其他collection里面做,或者在Query的时候排序和过滤。

经过测试,Blog那个例程已经可以跑起来,可以通过这个网址测试:http://laynezone.appspot.com

写的很不错,解决了我的相关疑问,前两个限制还算合理,但第三个限制就太不方便了,感觉完全失去了JPA的优势。期望google能有更好的解决办法吧,回头我也试一试,谢谢。


今天由在外面跑了一天,才看到帖子。第三个限制,其实也不麻烦,只是这种设计不适合xxx on rails思想而已,如果是比较早期接触J2EE的话,这个不会陌生。Model定义那几个东西,是直接操作数据库的,也就是我们说的PO,传统的J2EE上面,PO是不可以在Contrioller层操作的,在下面上来的数据需要转为VO才能操作,才能扔给View~这就是DTO模式。Apache的beanutil可以做这个事情,但是这只是个例子,直接setter,getter就可以了。。。

很简单,在Play!里面建个vo的包,建一个PostVo,然后:

        List<Post> posts = Post.findAll();
        Iterator<Post> postIt = posts.iterator();
        List<PostVo> otherPosts = new ArrayList<PostVo>();
        while (postIt.hasNext()) {
            Post postItem = postIt.next();
            if (postItem.id.longValue() != id.longValue()) {
                PostVo postVo = new PostVo();
                postVo.setId(postItem.id);
                postVo.setTitle(postItem.title);
                postVo.setContent(postItem.content);
                postVo.setDate(postItem.date);
                otherPosts.add(postVo);
            }
        }
        Collections.reverse(otherPosts);


当然在view里面要用otherPosts来替代posts,迭代otherPosts~~
我刚上传了个新版本,已经和原来的一样了~~

还有一个大文本的问题,GAE只能使用Text字段来存放大文本,这样play!会有问题。


GAE的TEXT类型根本无法使用,不管是直接使用JAVA,还是使用PLAY!框架。
0 请登录后投票
   发表时间:2009-07-14  
GAE的Text可以使用啊,你说的无法使用是指什么?
0 请登录后投票
   发表时间:2009-07-14  
pure 写道
GAE的Text可以使用啊,你说的无法使用是指什么?
你有试验成功的例子么?
0 请登录后投票
   发表时间:2009-07-14   最后修改:2009-07-14
Laynepeng试了,我也试了。

这两个都是Laynepeng的

http://laynezone.appspot.com/
http://layneblog.appspot.com/

要调 Text的getValue() 得到一个字符串对象来处理。持久化的时候要加@Enumerated注解。
0 请登录后投票
   发表时间:2009-07-14  
george.SPACE 写道
GAE的TEXT类型根本无法使用,不管是直接使用JAVA,还是使用PLAY!框架。


怎么不能使用,存不进去,还是什么?把报错的log帖出来。我的两个例子的正文用了Text,但是comment没有,限制了comment的长度。

如需测试,可以用http://laynezone.appspot.com/的发表文章来测试。。。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics