【发布时间】:2014-12-11 09:48:18
【问题描述】:
我想在 hql 中提供带有连接条件的额外参数。 在我写的 sql 查询中
select a.*,ur.fname,ur.lname
from
atom as a
left join user as ur
on
a.id=ur.id
left join album as al
on
a.id=al.aid
and al.name='Profile'
left join post_images as pi
on
a.id=pi.aid
and pi.is_album_cover='yes'
where
(ur.fname like '%n%'
or ur.fname like '%n%')
and a.status='active';
它可以正常工作并提供所需的结果。 在 hql 我不知道如何给额外的参数加入 用于获取我写的数据(没有额外的参数)
feeds = (List<Atom>) session.createQuery(
"select distinct atom from Atom as atom "
+ "left join fetch atom.albums as album "
+ "left join fetch album.postImageses as coverImage "
+ "left join fetch atom.user as user "
+ "where "
+ "(atom.user.fname like :name "
+ "or atom.user.lname like :name )"
+ "and album.name=:albumName "
+ "and coverImage.isAlbumCover=:isCover "
+ "and atom.status=:status ")
.setParameter("albumName", "Profile")
.setParameter("name", '%' + name + '%')
.setParameter("name", '%' + name + '%')
.setParameter("isCover", "yes")
.setParameter("status", "active")
.setFirstResult(0)
.setMaxResults(30)
.list();
这没有给出任何结果,我怎样才能给加入额外的参数
【问题讨论】:
-
您可以打印出生成的 SQL 并将其与您的工作 SQL 进行比较。顺便说一句,在 SQL 中你有
a.status='active',但在 hql 中你使用setParameter("status", "Active")(首字母大写)。会不会是个问题? -
与
isCover相同(yes和Yes) -
我改了问题`setParameter("status", "active")'