title: Spring Data JPA - ManyToOne null
body: <p>I'm learning Spring Data and JPA, and I have trouble making a simple ManyToOne - OneToMany connection within my entities.</p>
  
  <p>Example: model relationship between cars and people (owners), one person can own multiple cars, but one car is owned by only one person.</p>
  
  <p>Problem: owner of Car (annotated with ManyToOne) is always null.</p>
  
  <p>App.java</p>
  
  <pre><code>@EnableJpaRepositories
  @SpringBootApplication
  public class App {
  
      @Autowired private PersonRepository personRepository;
      @Autowired private CarRepository carRepository;
  
      @EventListener
      @Transactional
      public void handleContextRefresh(ContextRefreshedEvent event) {
          Car car1 = new Car();
          Car car2 = new Car();
          Person person = new Person();
  
          car1.setName("Ferrari");
          car2.setName("Porsche");
          person.setName("Person Name");
  
          carRepository.save(car1);
          carRepository.save(car2);
  
          person.getCars().add(car1);
          person.getCars().add(car2);
  
          personRepository.save(person);
  
          carRepository.findAll().forEach(System.out::println);   // TODO owner should not be null
          personRepository.findAll().forEach(System.out::println);
      }
  
      public static void main(String[] args) throws IOException, URISyntaxException {
          SpringApplication.run(App.class, args);
      }
  
  }
  </code></pre>
  
  <p>Car.java</p>
  
  <pre><code>@Entity
  public class Car {
  
      @Id
      @GeneratedValue
      private UUID id;
  
      private String name;
  
      @ManyToOne
      private Person owner;
  
      public String getName() { return name; }
      public void setName(String name) { this.name = name; }
  
      public Person getOwner() { return owner; }
      public void setOwner(Person owner) { this.owner = owner; }
  
      @Override
      public String toString() {
          return "Car [id=" + id + ", name=" + name + ", owner=" + owner + "]";
      }
  
  }
  </code></pre>
  
  <p>CarRepository.java</p>
  
  <pre><code>@Repository
  public interface CarRepository extends CrudRepository&lt;Car, UUID&gt;  { }
  </code></pre>
  
  <p>Person.java</p>
  
  <pre><code>@Entity
  public class Person {
  
      @Id
      @GeneratedValue
      private UUID id;
  
      private String name;
  
      @OneToMany(mappedBy="owner", cascade=CascadeType.ALL)
      private Set&lt;Car&gt; cars = new HashSet&lt;&gt;();
  
      public String getName() { return name; }
      public void setName(String name) { this.name = name; }
  
      public Set&lt;Car&gt; getCars() { return cars; }
      public void setCars(Set&lt;Car&gt; cars) { this.cars = cars; }
  
      @Override
      public String toString() {
          return "Person [id=" + id + ", name=" + name + ", cars=" + cars + "]";
      }
  
  }
  </code></pre>
  
  <p>PersonRepository.java</p>
  
  <pre><code>@Repository
  public interface PersonRepository extends CrudRepository&lt;Person, UUID&gt;  { }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Car [id=10b38a23-536d-4b8f-8578-16e12f2f65d1, name=Ferrari, owner=null]
  Car [id=2c15abf9-cb96-4d70-9271-b25096764276, name=Porsche, owner=null]
  Person [id=8484b16f-1645-49af-a4d5-4b6ba7238aba, name=Person Name, cars=[Car [id=2c15abf9-cb96-4d70-9271-b25096764276, name=Porsche, owner=null], Car [id=10b38a23-536d-4b8f-8578-16e12f2f65d1, name=Ferrari, owner=null]]]
  </code></pre>
  
  <p>Owner of both cars is null. How can I fix this?</p>