Home > database >  How to use JPA findById for querying on CompositeKey?
How to use JPA findById for querying on CompositeKey?

Time:11-18

I have the following IdClass

public class EmployeeId implements Serializable {

  private static final long serialVersionUID = -2221228212747288530L;

  private String id;

  private long joiningDate;
}

and EntityClass

@Entity
@IdClass(EmployeeId.class)
private class Employee {
  
  @Id
  private String Id;

  @Id
  private long joiningDate;

  //rest of columns   

}

How do I use findById here with only Id column? I have already tried findByIdId, findByTransportOrderIdId. Not working.

CodePudding user response:

Simply create a method findById that only takes a String as parameter:

public interface EmployeeRepository extends JpaRepository<Employee, EmployeeId> {

    Optional<Employee> findById(String id);
}
  • Related