我想在我的域/实体对象中执行以下操作:

@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是这样,它在我的数据库命令提示符下工作正常:

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


仅仅是我还是休眠文档比较痛苦,您是否发现自己经常想知道,编写普通的jdbc准备好的语句来填充pojos / domain objects / dto's会更快吗?

最佳答案

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

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

10-06 03:19