[英]GAE & Siena - abstract classes and instantiation exceptions
I am building my first GAE app using the Play! 我正在使用Play构建我的第一个GAE应用! Framework and I am having issues with Siena and abstract classes.
框架,我遇到了Siena和抽象类的问题。
A feature within the app is to allow a user to upload a post on which other users can comment. 该应用程序中的一项功能是允许用户上传其他用户可以在其上发表评论的帖子。 However, I am running into problems when I try to create multiple post types which inherit from an abstract
Post
class. 但是,当我尝试创建从抽象
Post
类继承的多种发布类型时,我遇到了问题。
The Post
class and a respective Picture
subclass are defined as follows: Post
类和相应的Picture
子类定义如下:
Post
发布
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();
}
}
Picture
图片
public class Picture extends Post {
//***EDIT*** - added default constructor
public Post_Pic(){
}
public String path;
public String description;
}
And the respective Comment
class: 以及相应的
Comment
类:
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;
}
}
The problem that I am having is that when I try and get comments for a Picture
object, an InstantiationException
is thrown. 我遇到的问题是,当我尝试获取
Picture
对象的注释时,会引发InstantiationException
。
Does Siena (or Java) try to create a new instance of Post
when retrieving a Comment
and as a result throw an InstantiationException
? Siena(或Java)在检索
Comment
时是否尝试创建Post
的新实例并因此引发InstantiationException
? If so, can anyone point me in the right direction for achieving what I am trying to do? 如果是这样,有人能指出我正确的方向来实现我的目标吗?
I have added default constructors to my models but this has unfortunately had no effect. 我已经在模型中添加了默认构造函数,但是不幸的是这没有效果。
The test code which is throwing the error is as follows: 引发错误的测试代码如下:
@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() );
}
The InstantiationException
is thrown at the line List<Comment> comments = post_pic.comments();
InstantiationException
抛出在List<Comment> comments = post_pic.comments();
. 。
Try to create a default constructor in your models... Siena uses reflection to create classes but it doesn't try to use specific constructors! 尝试在模型中创建默认的构造函数... Siena使用反射来创建类,但不会尝试使用特定的构造函数! Tell me if it works better!
告诉我它是否更好!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.