title: JPA many-to-one relation - need to save only Id
body: <p>I have 2 classes: Driver and Car. Cars table updated in separate process. What I need is to have property in Driver that allows me to read full car description and write only Id pointing to existing Car. Here is example:</p>
  <pre><code>@Entity(name = &quot;DRIVER&quot;)
  public class Driver {
  ... ID and other properties for Driver goes here .....
  
      @ManyToOne(fetch=FetchType.LAZY)
      @JoinColumn(name = &quot;CAR_ID&quot;)
      private Car car;
  
      @JsonView({Views.Full.class})
      public Car getCar() {
        return car;
      }
      @JsonView({Views.Short.class})
      public long getCarId() {
        return car.getId();
      }
      public void setCarId(long carId) {
        this.car = new Car (carId);
      }
  
  }
  </code></pre>
  <p>Car object is just typical JPA object with no back reference to the Driver.</p>
  <p>So what I was trying to achieve by this is:</p>
  <ol>
  <li>I can read full Car description using detailed JSON View</li>
  <li>or I can read only Id of the Car in Short JsonView</li>
  <li>and most important, when creating new Driver I just want to pass in JSON ID of the car.
  This way I dont need to do unnesessery reads for the Car during persist but just update Id.</li>
  </ol>
  <p>Im getting following error:</p>
  <pre><code>object references an unsaved transient instance - save the transient instance before flushing : com.Driver.car -&gt; com.Car
  </code></pre>
  <p>I dont want to update instance of the Car in DB but rather just reference to it from Driver. Any idea how to achieve what I want?</p>
  <p>Thank you.</p>
  <p>UPDATE:
  Forgot to mention that the ID of the Car that I pass during creation of the Driver is valid Id of the existing Car in DB.</p>