hibernate DetachedCriteria实现多表关联查询createAlias的使用

 

记录本例查询初衷:

有表:

表1,表2,表3

关系

1 many-to-one 2
2 many-to-one 3

 结果:要通过表3中的条件反向查询表1中相关的数据

 

    
    public Page<WeiCommentComment> findPageWeiCommentCommentByUserid(
            String userid, PageParam pageParam) {
        DetachedCriteria dc = super.getDetachedCriteria(); 
        if(StringUtils.isNotBlank(userid)){
            DetachedCriteria dcwei = dc.createAlias("weiComment", "w");
            dcwei.add(Restrictions.eq("user.idStr", userid));
        }
        Page<WeiCommentComment> page =  super.findPage(dc, pageParam);
        return page;
    }

 

上面介绍:

WeiCommentComment:表1
weiComment:为表1中的定义的关联关系
    @ManyToOne(targetEntity = WeiComment.class)
    @JoinColumn(name="wei_id",updatable=false)
    private WeiComment weiComment;

 

user:为表2中也就是WeiComment 中的表关联关系
    @ManyToOne(targetEntity = NsanbanUser.class)
    @JoinColumn(name="userID",updatable=true)
    private NsanbanUser user;

 

 

如上介绍查询

 

文章来自:http://www.cnblogs.com/hwaggLee/p/4682617.html
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3