Using @EmbeddedId with JpaRepository

后端 未结 2 1733
一生所求
一生所求 2020-12-28 13:56

I have simple Entitly class with the @EmbeddedId (Integer and String fields in separate class). And I use the Spring Data (org.s

2条回答
  •  醉梦人生
    2020-12-28 14:38

    (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 :

    @Embeddable
    public 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 in the main class followed by the property you want to query uppon within the PK class:

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

提交回复
热议问题