title: JPA select Query to return Entity with @ManyToOne Mapping
body: <p>I am a beginner, learning JPA, for practice I was working on this problem where I have two entity classes Person and Gym. </p>
  
  <p>Person has :
     - id (auto-generated)
     - name
     - age
     - Gym (Many to One mapping)</p>
  
  <p>Gym has :
     - id (auto-generated)
     - name
     - rating
     - fee
     - List of Person (One to Many mapping)</p>
  
  <p>Now, I have my PersonRepository which extends JpaRepository and have this following JPQL query where I am try to retrieve all persons with age &lt; (some user input value)</p>
  
  <p>The problem is the retrieved person list is always empty. I tried used fetch join but still it returns empty list.</p>
  
  <p>What should be the appropriate JPQL query for this scenario ?</p>
  
  <p>Thanks ! Balasubramanyam </p>
  
  <p>Gym Entity</p>
  
  <pre class="lang-java prettyprint-override"><code>@Entity
  public class Gym {
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      private int gym_id;
  
      @NotNull
      private String name;
  
      @NotNull
      private String city;
  
      @NotNull
      @Max(5)
      private double rating;
  
      @NotNull
      private double fee;
  
      @OneToMany(mappedBy="gym", 
                  cascade= {CascadeType.MERGE, CascadeType.PERSIST,
                          CascadeType.REFRESH}, fetch=FetchType.EAGER)
      @JsonManagedReference
      private List&lt;Person&gt; personList;
  
      public Gym() {
          super();
      }
  
      public Gym(int gym_id, @NotNull String name, @NotNull double rating, @NotNull double fee, List&lt;Person&gt; personList,
              @NotNull String city) {
          super();
          this.gym_id = gym_id;
          this.name = name;
          this.rating = rating;
          this.fee = fee;
          this.personList = personList;
          this.city = city;
      }
  // getters and setters
  </code></pre>
  
  <p>Person Entity</p>
  
  <pre class="lang-java prettyprint-override"><code>@Entity
  public class Person {
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      private int id;
  
      @NotNull
      private String name;
  
      @NotNull
      private int age;
  
      @ManyToOne (cascade={CascadeType.MERGE, CascadeType.PERSIST,
          CascadeType.REFRESH, CascadeType.DETACH})
      @JoinColumn
      @JsonBackReference
      private Gym gym;
  
      public Person() {
          super();
      }
  
      public Person(int id, @NotNull String name, @NotNull int age, Gym gym) {
          super();
          this.id = id;
          this.name = name;
          this.age = age;
          this.gym = gym;
      }
  // getters and setters
  </code></pre>
  
  <p>PersonRepository</p>
  
  <pre class="lang-java prettyprint-override"><code>public interface PersonRepository extends JpaRepository&lt;Person, Integer&gt;{
  
  @Query("select p from Person p join fetch p.gym where p.age&lt;=(:age)")
  List&lt;Person&gt; filterByAge(@Param("age") int age);
  }
  </code></pre>
  
  <p>In my service class this is what I am doing</p>
  
  <pre class="lang-java prettyprint-override"><code>List&lt;Person&gt; filteredPersonList = personRepository.filterByAge(age);
  System.out.println(filteredPersonList); // prints empty
  </code></pre>