how to make findby with composite key in spring data - jpa -hibernate ? @EmbeddedId

pebC picture pebC · Mar 10, 2020 · Viewed 6.9k times · Source
@Embeddable
public class AccountTransactionId implements Serializable {
    private String trxDate;
    private String acctNo;
    private int trxNo;
}

@Entity
public class Transaction {
    @EmbeddedId
    private AccountTransactionId accountTransactionId;

    private int amt;
    private int fee;
    private String cancelYn;
}

this is my repository. how to make the find method? i don't have any idea about that

List<Map<String, Object>> findByAccountTransactionId_trxDate(String trxDate);

i tried "findByAccountTransactionId_trxDate", "findByAccountTransactionIdTrxDate", "findByIdTrxDate"...

Answer

Umang Soni picture Umang Soni · Mar 11, 2020

You can write like this:

public interface TransactionRepository extends JpaRepository<Transaction, AccountTransactionId> {

    List<Map<String, Object>> findByTrxDateAndAcctNo(String trxDate, String acctNo);

}