0

我正在使用 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对象的评论时,InstantiationException会抛出 an 。

PostSiena(或Java)在检索 a 时是否尝试创建一个新实例Comment并因此抛出 a 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<Comment> comments = post_pic.comments();

4

1 回答 1

1

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

于 2011-10-23T19:10:36.580 回答