I am building my first GAE app using the Play! Framework and I am having issues with Siena and abstract classes.
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.
The Post
class and a respective Picture
subclass are defined as follows:
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
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.
Does Siena (or Java) try to create a new instance of Post
when retrieving a Comment
and as a result throw an 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();
.
Try to create a default constructor in your models... Siena uses reflection to create classes but it doesn't try to use specific constructors! Tell me if it works better!