使用内部联接的简单 hql 命名查询

发布于 2024-12-11 22:18:11 字数 935 浏览 0 评论 0原文

我想在我的域/实体对象中做这样的事情:

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄风 2024-12-18 22:18:11

我认为这可能对你有用,但我猜你的 Usercat 类在这里:

select c from Usercat as uc inner join uc.cat as c where uc.isFavourtie = true and uc.user = :user

I think this might work for you, but I am guessing your Usercat class here:

select c from Usercat as uc inner join uc.cat as c where uc.isFavourtie = true and uc.user = :user
一口甜 2024-12-18 22:18:11

案例问题,正确的查询将是:

from Cat c inner join on Usercat uc where uc.isfavourtie = true and uc.user = :user

注意:Cat 中的 C 是大写,Usercat 中的 U 是大写,其中 Usercat 中的 c 很小,isfavourite 中的 f 很小。

Case Issue, Right query would be:

from Cat c inner join on Usercat uc where uc.isfavourtie = true and uc.user = :user

Note : C in Cat is capital, U in Usercat is capital where as c in Usercat is small and f in isfavourite is small.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文