-1

I have the following models:

@Entity
      @Table(name = "post")
      public class Post extends Model {
      @Id
      @GeneratedValue
      public Long id;
      @Column(name = "url", nullable = false, length = 255)
      public String url;
      @Column(name = "content")
      public String content;
      @OneToMany(cascade = CascadeType.ALL, mappedBy = "post")
      public List comments;
      [...]
  
  and
@Entity
@Table(name = "comment")
public class Comment extends Model {
@Id
@GeneratedValue
public Long id;
@Column(name = "content", nullable = false, length = 255)
public String content;
@Column(name = "isDelete")
public boolean isDelete = false;
@ManyToOne(cascade = CascadeType.ALL)
public Post post;

Then I try to do a search like:

 Post post = find.select("*").fetch("comments").where().eq("id", postId).eq("comments.isDelete", true).findUnique();

But That's not working SQL query like :

return Post SELECT * FROM POST p INNER JOIN COMMENT c ON POST.ID=COMMENT.ID WHERE p.id = ? AND c.isDelete = false;

How do i can use Ebean ? Please help me.

Rua Tahi
  • 238
  • 5
  • 15

1 Answers1

1

Add this to your Post class:

public static Find<Long,Post> find = new Find<Long,Post>(){};

Add this to your Comment class:

public static Find<Long,Comment> find = new Find<Long,Comment>(){};

In your Post class is a little error in it, replace it like so:

@OneToMany(cascade = CascadeType.ALL, mappedBy = "post")
    public List<Comment> comments;

Now you can query it like this:

Post post = Post.find.fetch("comment").where().eq("id", postId).eq("comment.isDelete", true).findUnique();

Now you can access the comments like so:

post.comments

or:

List<Comment> comments = post.comments;

You get the idea

AME
  • 2,262
  • 6
  • 19
  • 39
  • thanks for answering but in my query `eq("comments.isDelete", true)` That not working – Rua Tahi Dec 06 '16 at 03:00
  • thanks ALEX. But in Java code `@OneToMany(cascade = CascadeType.ALL, mappedBy = "post") public List comments;` So `fetch("comment")` That not working. But I try writing `Post post = find.fetch("comments").where().eq("id",postId).eq("t1.isDelete", false).findUnique();` That ok but not good. p/s : Because I'm Japanese, so English was bad. So sorry – Rua Tahi Dec 07 '16 at 03:25
  • @RuaTahi Don't worry I understand you, you could try it from the other side ( comments ) like: List comments = Comment.find.where().eq("postId", postId).eq("isDeleted", false).findList(); – AME Dec 07 '16 at 09:15