title: How to get the parent entity from the child primary key in spring data jpa?
body: <p>Hi I have parent entity User which has one to many relation with Vehicle entity and vehicle entity has many to one relation with User. I have a foreign key "user_fk" in my vehicle table. I have a UserRepository which extends Spring Data JPARepository that has save and findById methods.</p>
  
  <p>Also I have two attributes in vehicle table which uniquely identifies a vehicle and I need to use that data to find out the corresponding User Object .I know it can be simply done by creating a Vehicle Repository which extends JPA repository and can use the find by method but I don't want to create a new Vehicle repository class just to achieve this functionality. How can I find the User_fk column value or User object without creating Vehicle Repository??Can I achieve this using UserRepository??</p>
  
  <p>My UserClass:</p>
  
  <pre><code>    @Entity
      @Table(name = "usr")
      class User{
      @Id
      @Column(name = "user_id", updatable = false, nullable = false)
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long userId;
      private String username;
      @OneToMany(mappedBy = "user",cascade = CascadeType.ALL)
      private List&lt;Vehicle&gt; vehicles= new ArrayList&lt;&gt;();
      //getters and setters
      }
  </code></pre>
  
  <p>My vehicleClass:</p>
  
  <pre><code>       @Entity
         @Table(name = "vehicle")
         class Vehicle{
         @Id
         @Column(name = "vehicle_id", updatable = false, nullable = false)
         @GeneratedValue(strategy = GenerationType.IDENTITY)
         private Long vehicleId;
         private String vehicleName;
         @ManyToOne(fetch = FetchType.LAZY)
         @JoinColumn(name = "user_fk" , nullable = false)
         private User user;
        //getters and setters 
        }
  </code></pre>
  
  <p>I have the values of vehicleId and vehicleName and from these two attribute values I want to get the User object.</p>
  
  <p>I don't want to create a vehicle repository only just to find the User object.I know it can be achieved by creating a Vehicle Repository which extends JPA repository in springdatajpa.</p>