Using @EmbeddedId with JpaRepository Using @EmbeddedId with JpaRepository java java

Using @EmbeddedId with JpaRepository


(by Yosi Lev)This can be done as the following:Suppose your main entity is:

@Entity@Table(name="JRULES_FLOW")public class JrulesFlow implements Serializable {   private static final long serialVersionUID = 1L;   @EmbeddedId   private JrulesFlowPK id;   @Column(name="NEXT_SEQ")   private int nextSeq;   @Column(name="REF_ID")   private String refId;   @Column(name="TASK_TYPE")   private String taskType;   @Column(name="VALUE_TO_FIND")   private String valueToFind;}

And your PK class is :

@Embeddablepublic class JrulesFlowPK implements Serializable {   //default serial version id, required for serializable classes.   private static final long serialVersionUID = 1L;   @Column(name="FLOW_ID")   private String flowId;   @Column(name="TASK_SEQ")   private long taskSeq; }

The JPA repository method name shouls include the name of the id field inthe main class followed by the property you want to query uppon within the PK class:

public interface JrulesFlowRepository extends JpaRepository<JrulesFlow,       JrulesFlowPK> { // NOTE: put here both classes - also the pk class..   public List<JrulesFlow>  findByIdFlowId(String flowId);  // Id - is the                   // @EmbeddedId in JrulesFlow. FlowId is an attribute                   // within JrulesFlowPK}


It seems your query is using column names. It should contain the property names, including navigation into embedded objects. There's also a related question here on SO: How to write JPQL SELECT with embedded id?

select distinct id.id from EntityClass where userId = :userId and (...)

The first id refers to attribute id of EntityClass (of type EmbeddedIdClass), and the second one refers to the id property of EmbeddedIdClass.

Also, make sure there's a userId property in EntityClass.