使用HQL和单独的IdClass进行复合键查询 [英] Composite key query using HQL and separate IdClass

查看:155
本文介绍了使用HQL和单独的IdClass进行复合键查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用组合键定义的hibernate实体,在列上使用id class和id annoation。工作正常。但是,我现在想要执行组合键查询

I have a hibernate entity defined with a composite key, using id class and id annoation on columns. Works fine. However I now want to do a composite key query.

Select mt 
from MyTable mt 
where (mt.id, mt.column2) in (:myListOfCompositeKeys)

什么是使用HQL的语法,当我定义如下实体时(不使用embeddedId )。

What is the syntax using HQL, when I have defined the entity like below (not using embeddedId).

@Entity
@IdClass(MyKey.class)
@Table(name = "MY_TABLE")
public class MyTable implements Serializable {
    @Column(name = "ID")
    @Id
    private Long id;
    @Column(name = "Column2")
    @Id
    private Long column2;

或者我需要使用 embeddedId 和如果是这样的话,那么 HQL 语法是什么?

Or do I need to use embeddedId and if so what would be the HQL syntax for that ?

推荐答案

您使用 @EmbededId ,您必须覆盖等于 hashCode 方法和HQL将如下所示。

If you use @EmbededId, you will have to override equals and hashCode methods and HQL for it would be something like below.

SELECT mt
FROM MyTable mt
WHERE mt.id 
IN(:listOfKeys)

这篇关于使用HQL和单独的IdClass进行复合键查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆