编写简单的hql命令_使用内部联接的简单hql命名查询

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... ?

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值