Composite key query using HQL and separate IdClass

二次信任 提交于 2019-12-25 04:16:21

问题


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)

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;

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


回答1:


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)


来源:https://stackoverflow.com/questions/30095676/composite-key-query-using-hql-and-separate-idclass

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!