【问题标题】:Java Play - How to query OneToMany EbeanJava Play - 如何查询 OneToMany Ebean
【发布时间】:2016-12-05 03:42:13
【问题描述】:

我有以下型号:

@Entity
      @表(名称=“帖子”)
      公共类帖子扩展模型{
      @ID
      @GeneratedValue
      公共长ID;
      @Column(name = "url", nullable = false, length = 255)
      公共字符串网址;
      @Column(名称=“内容”)
      公共字符串内容;
      @OneToMany(cascade = CascadeType.ALL, mappedBy = "post")
      公共列表 cmets;
      [...]
  
  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;

然后我尝试进行如下搜索:

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

但这不起作用 SQL 查询如下:

回邮 SELECT * FROM POST p INNER JOIN COMMENT c ON POST.ID=COMMENT.ID 在哪里 p.id = ? 和 c.isDelete = false;

如何使用 Ebean?请帮帮我。

【问题讨论】:

    标签: playframework one-to-many ebean


    【解决方案1】:

    将此添加到您的 Post 类中:

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

    将此添加到您的评论类:

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

    在你的 Post 类中有一个小错误,像这样替换它:

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

    现在可以这样查询了:

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

    现在您可以像这样访问 cmets:

    post.comments
    

    或:

    List<Comment> comments = post.comments;
    

    你懂的

    【讨论】:

    • 感谢您的回答,但在我的查询中 eq("comments.isDelete", true) 那不起作用
    • 感谢亚历克斯。但是在 Java 代码中 @OneToMany(cascade = CascadeType.ALL, mappedBy = "post") public List&lt;Comment&gt; comments; 所以 fetch("comment") 那行不通。但我尝试写Post post = find.fetch("comments").where().eq("id",postId).eq("t1.isDelete", false).findUnique(); 可以,但不好。 p/s:因为我是日本人,所以英语不好。很抱歉
    • @RuaTahi 别担心,我理解你,你可以从另一边( cmets )试试: List cmets = Comment.find.where().eq("postId" , postId).eq("isDeleted", false).findList();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-15
    相关资源
    最近更新 更多