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;

 

 

如上介紹查詢

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM