记录本例查询初衷:
有表:
表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;
如上介绍查询
本文介绍了如何通过表3中的条件反向查询表1的相关数据,使用了Hibernate的多表关联查询方式,具体展示了如何在Java代码中实现many-to-one关系的查询,并详细解释了表间关系及查询过程。
1377

被折叠的 条评论
为什么被折叠?



