【问题标题】:GAE & Siena - abstract classes and instantiation exceptionsGAE & Siena - 抽象类和实例化异常
【发布时间】:2011-10-23 14:44:20
【问题描述】:

我正在使用 Play 构建我的第一个 GAE 应用!框架,我遇到了锡耶纳和抽象类的问题。

应用程序中的一项功能是允许用户上传其他用户可以评论的帖子。但是,当我尝试创建多个从抽象 Post 类继承的帖子类型时遇到了问题。

Post 类和各自的Picture 子类定义如下:

发帖

public abstract class Post extends Model {

    //***EDIT*** - added default constructor
    public Post(){

    }

    @Id(Generator.AUTO_INCREMENT)
    public long id;

    @Filter("post")
    public Query<Comment> comments;

    public static Query<Post> all() {
        return Model.all(Post.class);
    }

    public static Post findById(long id) {
        return all().filter("id", id).get();
    }

    public Comment addComment( User user, String s_comment ){
        Comment comment = new Comment( this, s_comment );
        return comment;
    }

    public List<Comment> comments() {
        return comments.order().fetch();
    }
}

图片

public class Picture extends Post {

    //***EDIT*** - added default constructor
    public Post_Pic(){

    }

    public String path;
    public String description;

}


以及各自的Comment 类:

评论

public class Comment extends Model {

    @Id(Generator.AUTO_INCREMENT)
    public long id;

    @Index("post_index")
    public Post post;

    public String comment;

    public static Query<Comment> all() {
        return Model.all(Comment.class);
    }

    public static Comment findById(long id) {
        return all().filter("id", id).get();
    }

    public Comment( Post post, String comment ){
        this.post    = post;
        this.comment = comment;
    }
}


我遇到的问题是,当我尝试为 Picture 对象获取 cmets 时,会抛出一个 InstantiationException

Siena(或 Java)在检索 Comment 时是否尝试创建 Post 的新实例并因此抛出 InstantiationException?如果是这样,任何人都可以为我指明正确的方向来实现我想要做的事情吗?


*** 编辑 ***

我已经为我的模型添加了默认构造函数,但不幸的是这没有任何效果。

报错的测试代码如下:

@Test
public void commentPost_Pic(){
    User bob = new User( fullname, email, password );
    bob.insert();
    bob = User.connect( email, password );
    assertNotNull( bob );

    Post_Pic post_pic = new Post_Pic();
    post_pic.insert();
    Comment comment = post_pic.addComment( bob, testComment );
    comment.insert();

    List<Comment> comments = post_pic.comments();       //**** <- Error gets thrown here
    assertEquals( 1, comments.size() );
}

InstantiationException 被抛出到List&lt;Comment&gt; comments = post_pic.comments(); 行。

【问题讨论】:

    标签: java google-app-engine playframework siena


    【解决方案1】:

    尝试在模型中创建默认构造函数... Siena 使用反射来创建类,但它不会尝试使用特定的构造函数! 如果效果更好,请告诉我!

    【讨论】:

    • 感谢您的回复。不幸的是,我已经尝试过,因为我看到了issue raised on the Play Google group(我认为你是帕斯卡是对的吗?)。我将修改我的代码以显示我的测试在哪里抛出了错误。
    • 好的,抽象类可能有问题。这个案例没有单元测试,所以我不能说它是否有效!如果您可以在 github.com/mandubian/siena 上打开带有代码示例和错误的问题,那就太好了。我会尽快修复它!
    • 好的 - 谢谢,我有added an issue
    • 感谢您在 Github 上的回复。如果您不介意发布指向您的 Github 回复的链接,那么我很乐意将其用作已接受的答案!
    • 这是我在 github 上的评论,解释了这个问题:github.com/mandubian/siena/issues/9#issuecomment-2510220
    猜你喜欢
    • 2011-10-02
    • 1970-01-01
    • 2017-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多