6

I have a Posts model and every post also contains Blocks (also a model). I'm using the play framework for this website, and what I want to do is show X number of post with all of it's blocks on one page. JPA (or play framework's implementation, don't know which one it is) has the find() method with which I could query for posts in my controller, and than I would send the post list to my view like this:

render(postList);

What I wanted to know is what would be the best way to send the blocks for each post to the view. I could add a getBlocks() method to my Post model, which sends back a blocksList, and call it from the view, but this seems messy to me, and it would defeat the purpose of MVC since the blocks would be fetched from the view.. (or am I wrong about this?)

Does JPA or Play! offer some way of retrieving the blocks together with the posts?

This is what my Post model looks like right now, without getters and setters:@Entity

@Table(name="posts")
public class Post extends GenericModel{

    @Id
    @Column(name="post_id")
    private int id;

    @Column(name="post_situation")
    private String situation;

    @Column(name="post_date")
    private Date date;

    @Column(name="post_userid")
    private int userid;

    private List<Block> blockList;
    public List<Block> getBlocks() {
        List<Block> block = null;
        return blockList;
    }

}

How should I do this?

  • If `blocks` are an integral part of a `post` then how does `render` render some `post` without the `blocks`? – toto2 Jun 06 '11 at 20:00
  • I'm probably wrong about that. Does that mean that once I create my postList the code inside getBlocks is executed and each object will contain the right Blocks? –  Jun 06 '11 at 20:25
  • You declare `blockList` as a member of `Post`. I assumed that somewhere (constructor of `Post`?) `blockList` was initialized before `render` is called on the `Post`. – toto2 Jun 06 '11 at 20:33
  • No it's not being called. I thought maybe there would be some way to let JPA take care of this.. –  Jun 06 '11 at 20:35
  • OK. I'm not sure how to do that. But I guess you should have your `blockList` initialized before you call `render`. – toto2 Jun 06 '11 at 20:37

1 Answers1

11

Just use the one-to-many keyword:

@OneToMany
private List<Block> blockList;

However it's not clear if you need the getBlocks method. If you are just getting the blocks from the database then you don't need this method.

Note: you probably want to read more on @OneToMany. You can for example add the option @OneToMany(cascade = {CascadeType.PERSIST, CascadeType.REMOVE}) such that when you persist or remove a Post from the database, the inner blockList does the same.

toto2
  • 5,306
  • 21
  • 24
  • So I need to add `@ManyToOne private Post post;` to my block. Do I also need `private int postid;` to reference to the post, or only the object? –  Jun 06 '11 at 21:05
  • No, it's as I have written: `@OneToMany` is a tag on the field `blockList`. The database creates a new table for the `blockList` of each `Post` automatically with that keyword. – toto2 Jun 06 '11 at 22:34
  • So I don't need to create an SQL table upfront? –  Jun 06 '11 at 23:24
  • If you configure your `persistence.xml` correctly, including `Post.class`, you shouldn't have to create any tables yourself. But you should nonetheless use some tool outside Java to navigate your database after its creation by Java and see what it looks like. You probably have some command line tool or browser based tool with your database. – toto2 Jun 07 '11 at 12:12
  • 2
    This is an old question and answer... but ... where can one read more about this? It's proving suprisingly hard to find doco for OneToMany! – GreenAsJade Dec 31 '13 at 07:11