问题
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