+ -
当前位置:首页 → 问答吧 → Hql 连接查询问题

Hql 连接查询问题

时间:2011-10-19

来源:互联网

两表tb_user(userID,userName)
tb_topic (topicID,userid)

其中userid为外键
怎样写HQL得到(topicID,userName)
对应的实体类

Class TbTopic
{
private Integer topicID;
private TbUser tbUser;
}
Class TbUser{
private Integer userID;
private String userName;

}

作者: longcrazy   发布时间: 2011-10-19

select b.topicID,a.userName from tb_user a,tb_topic b where b.tbUser.userID=a.userID

作者: selly0919   发布时间: 2011-10-20

引用 1 楼 selly0919 的回复:

select b.topicID,a.userName from tb_user a,tb_topic b where b.tbUser.userID=a.userID


hibernate的list()方法, 将返回List<Object[]>
一行记录放到一个Object[]数组

Object[0] : topicID
Object[1] : userName

作者: a131988   发布时间: 2011-10-20

STRING HQL = "from TbTopic as model where model.TbUser.topicID=? and model.TbUser.userName=?;
这是Hql语句的用法,根据实体对应的数据表查询。
如果在页面取值的话直接${model.TbUser.topicID},${model.TbUser.userName}

作者: love_se   发布时间: 2011-10-20

把model改成list就可以了

作者: love_se   发布时间: 2011-10-20

select t.topicID , t.tbUser.userName from TbTopic t 
这样就行了

作者: wen157604891   发布时间: 2011-10-23

得到你的结果可以像3楼所说的办法
也可以不用hql="from xxxx"
用sql="select xxx from xxx"
但是不同的是
你要把原来代码里面用到的Query对象
换成SQLQuery来使用

作者: oulinhnzz   发布时间: 2011-10-23

热门下载

更多