Classes maintaining relationships with Abstract Classes throw Instantiation Exception #9

Closed
OuchMyHead opened this Issue Oct 24, 2011 · 4 comments

Comments

Projects
None yet
2 participants
@OuchMyHead

This bug occured using Siena and GAE within the Play! Framework 1.2.3

When instantiating objects from the database, an Instantiation Exception is thrown if the persisted object references an abstract class.

For example:

************** Abstract Class **************

public abstract class Post extends Model {

//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( String s_comment ){
    Comment comment = new Comment( this, s_comment );
    return comment;
}

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

}

************** Inhertiting Class **************

@table("post_pics")
public class Post_Pic extends Post {

//default constructor
public Post_Pic(){

}

@Column("post")
@Index("post_index")
public Post post;

public String path;
public String description;

}

************** Class Referencing Post **************

@table("comments")
public class Comment extends Model {

@Id(Generator.AUTO_INCREMENT)
public long id;

@Column("post")
@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;
}

}

************** Test Code **************

@test
public void commentPost_Pic(){

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

comment = Comment.findById( comment.id );      //**** <- Error gets thrown here

}

****************** End of Code ******************

An Instantiation Exception is thrown when comment is instantiated from the database.

@mandubian

This comment has been minimized.

Show comment Hide comment
@mandubian

mandubian Oct 24, 2011

Owner

I found the origin of this exception and it's due to the way you use inheritance.
Read this page if you haven't yet: https://github.com/mandubian/siena/blob/master/source/documentation/manuals/inheritance.textile

It explains that Siena only implements the inheritance model: one table per class. So the parent class is just a field container reused in children class but the inheritance is not represented in the tables.
In your case, you have a table for PostPic (and as your Post class is abstract, no table for it). But in Comment, you put a reference to a Post which is not possible in this design since siena tries to instantiate a Post object which is not possible due to the "abstract".
The InstantiationException is due to this.

If you remove the "abstract", the code works but it will fail anyway: if you try to get the post associated to a comment, it will search the "Post" table and not the "PostPic" table due the model "one table per class". It just knows it is a post but not a PostPic.

Do you use GAE? If yes, there are other ways to write it using aggregation.
The last way is to remove the inheritance and declare the Query directly in PostPic.

Sorry about this but Siena is not yet as much evolved as Hibernate ;);)

Owner

mandubian commented Oct 24, 2011

I found the origin of this exception and it's due to the way you use inheritance.
Read this page if you haven't yet: https://github.com/mandubian/siena/blob/master/source/documentation/manuals/inheritance.textile

It explains that Siena only implements the inheritance model: one table per class. So the parent class is just a field container reused in children class but the inheritance is not represented in the tables.
In your case, you have a table for PostPic (and as your Post class is abstract, no table for it). But in Comment, you put a reference to a Post which is not possible in this design since siena tries to instantiate a Post object which is not possible due to the "abstract".
The InstantiationException is due to this.

If you remove the "abstract", the code works but it will fail anyway: if you try to get the post associated to a comment, it will search the "Post" table and not the "PostPic" table due the model "one table per class". It just knows it is a post but not a PostPic.

Do you use GAE? If yes, there are other ways to write it using aggregation.
The last way is to remove the inheritance and declare the Query directly in PostPic.

Sorry about this but Siena is not yet as much evolved as Hibernate ;);)

@OuchMyHead

This comment has been minimized.

Show comment Hide comment
@OuchMyHead

OuchMyHead Oct 25, 2011

Thanks for getting to the route of the problem for me and for the explanation; I will check out other ways of achieving this using aggregation through GAE!

No worries about it not being as eveloved as Hibernate yet - I quite like Siena.

Thanks for getting to the route of the problem for me and for the explanation; I will check out other ways of achieving this using aggregation through GAE!

No worries about it not being as eveloved as Hibernate yet - I quite like Siena.

@OuchMyHead OuchMyHead closed this Oct 25, 2011

@mandubian

This comment has been minimized.

Show comment Hide comment
@mandubian

mandubian Oct 25, 2011

Owner

Siena is not meant to become hibernate anyway :)
Don't hesitate to ask on the mailing list, I'll answer you asap!
About aggregation, in the unit tests, there are lots of samples about it in AggregationTest.
It gives lots of ideas!
Look at :

regards
Pascal

Owner

mandubian commented Oct 25, 2011

Siena is not meant to become hibernate anyway :)
Don't hesitate to ask on the mailing list, I'll answer you asap!
About aggregation, in the unit tests, there are lots of samples about it in AggregationTest.
It gives lots of ideas!
Look at :

regards
Pascal

@OuchMyHead

This comment has been minimized.

Show comment Hide comment
@OuchMyHead

OuchMyHead Oct 25, 2011

Excellent - thank you for the links and I'll be using the Siena mailing list in future!

James

Excellent - thank you for the links and I'll be using the Siena mailing list in future!

James

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment