如何使用Hibernate Criteria在两个inpedendent表之间进行@OneToMany映射

时间:2017-12-16 07:26:15

标签: java hibernate criteria hibernate-criteria

我试图在两个独立的表之间建立@oneToMany关系。我想得到每个帖子的评论数。

Select * from post_id, post_msg, count(*) from POST, COMMENTS where post_id = comment_gen_id

POST table
////////////
post_id (PRIMARY KEY)
post_msg (NOT NULL)

COMMENT table
///////////////
Comment_id (PRIMARY KEY)
comment_gen_id 
comment_user

以下是我的映射,

Class Post{
    @Column(name="POST_ID")
    private int post_id;

    @Column(name="POST_MSG")
    private String post_msg;

    @OneToMany(mappedBy="post")
    private Set<Comments> commentsList;
}

class Comments{
    @ManyToOne
    private Post post;

    @Column(name="comment_id")
    private int commentId;


    @Column(name="comment_gen_id")
    private int commentGenId;

    @Column(name="comment_user")
    private String user;
}

我不知道如何使用休眠标准制作( post_id = comment_gen_id )。

Criteria = session.createCriteria(Post.class)
       .createAlias("commentsList", "comments")
       .add(Restriction.eq("post_id", "comments.commentGenId");

现在我没有从此查询中获取任何记录。请帮我解决这个问题。

先谢谢!!!!。

0 个答案:

没有答案
相关问题