FlyingTurtle
FlyingTurtle

Reputation: 155

Query child record from parent

I have 2 classes: Staff and State.

public class State {
  @Id
  private String id;
  private String name;
}

public class Staff {

  @Id
  @Column(nullable = false, columnDefinition="VARCHAR(100)")
  private String id;
  private String name;
  private String phoneNo;
  private String address;

  @OneToOne
  @JoinColumn(name = "state_id")
  private State state;
}

How to get query using JpaRepository to filter by State.name. Since it is mapped using state_id?

Something similar to this:

//current DAO to filter by staff's name
List<Staff> findByNameContaining(String name);

But I want to filter by name of Staff and by name of State.

Upvotes: 0

Views: 43

Answers (2)

pvpkiran
pvpkiran

Reputation: 27078

try this.

List<Staff> findByNameAndState_Name(String staffName, String stateName)

Upvotes: 2

Pratik Shah
Pratik Shah

Reputation: 1852

  • You can do that as well. try to create a method as following. pass the state name.

    List findByStateName(String stateName);

Upvotes: 0

Related Questions