使用内部联接的简单 hql 命名查询
我想在我的域/实体对象中做这样的事情:
@Entity
@NamedQueries({
@NamedQuery(name="favouriteCats", query="from Cat c inner join on UserCat uc where uc.isFavourtie = true and uc.user = :user")
})
public final class Cat extends BaseTable
这样在我的服务层中我可以这样做:
Query query = session.getNamedQuery("favouriteCats")
query.setParameter(0, MyUser);
return query.list();
但是,我在 HQL 中的语法不正确 - 十分钟后查看官方文档我决定放弃并询问这里 ... ? 我的 usercat 表是这样连接的:
@ManyToOne(cascade = CascadeType.MERGE)
@JoinColumn(name="cat_fk", insertable=false, updatable=false)
private cat
sql 是这样的,它在我的 db 命令提示符下运行良好:
select c.*
from cat as c inner join usercat as uc on c.id = uc.cat_fk
and uc.isFavourite = 1 //bit field
and uc.user_fk = 74 //just user id
是只有我一个人还是 hibernate 文档相当痛苦,您是否经常想知道它是否是这样只需编写普通的 jdbc 准备好的语句来填充您的 pojos/域对象/dto 会更快吗?
I want to do something like this in my domain/entity object :
@Entity
@NamedQueries({
@NamedQuery(name="favouriteCats", query="from Cat c inner join on UserCat uc where uc.isFavourtie = true and uc.user = :user")
})
public final class Cat extends BaseTable
So that in my service layer I can do this :
Query query = session.getNamedQuery("favouriteCats")
query.setParameter(0, MyUser);
return query.list();
However, my syntax in HQL is incorrect - and aftern ten minutes looking at official docs I have decided to give up and ask here ... ?
My usercat table is joined like so :
@ManyToOne(cascade = CascadeType.MERGE)
@JoinColumn(name="cat_fk", insertable=false, updatable=false)
private cat
The sql is this, it works fine at my db command prompt:
select c.*
from cat as c inner join usercat as uc on c.id = uc.cat_fk
and uc.isFavourite = 1 //bit field
and uc.user_fk = 74 //just user id
Is it just me or is the hibernate documentation rather painful, and do you find yourself often wondering whether it would be quicker just to write normal jdbc prepared statements to populate your pojos/domain objects/dto's... ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这可能对你有用,但我猜你的 Usercat 类在这里:
I think this might work for you, but I am guessing your Usercat class here:
案例问题,正确的查询将是:
注意:Cat 中的 C 是大写,Usercat 中的 U 是大写,其中 Usercat 中的 c 很小,isfavourite 中的 f 很小。
Case Issue, Right query would be:
Note : C in Cat is capital, U in Usercat is capital where as c in Usercat is small and f in isfavourite is small.