title: Is there a way to add an object to spring's persistence context without retrieving it from the database?
body: <p>Say I have the following entity classes:</p>
  <pre><code>    public class childClass {
      
      @Id
      private Long Id;
      
      @ManyToOne
      private ParentClass parentClass;
  
  public ChildClass(ParentClass parentClass){
    this.parentClass = parentClass;
  }
      
      //other fields
      }
  
  public class ParentClass{
  
  @Id
  private Long id;
  
  public ParentClass(Long id){
     this.id = id;
  }
  
  }
  </code></pre>
  <p>I want to persist a group of new <code>ChildClass</code> objects based on a <code>Set</code> of <code>ParentClass</code> ids. Ideally i would like to do that like this:</p>
  <pre><code>parentClassSet.stream().map(parentId -&gt; new ChildClass(new ParentClass(parentId)).collect(Collectors.toList());
  childClassDao.save(parentClassSet);
  </code></pre>
  <p>But this will throw an error because the <code>ParentClass</code> objects do not exist in the persistence context and thus spring does not recognize these objects as valid <code>entities</code>.</p>
  <p>I have to retrieve the ParentClass from the database first, based on an id and then use that object to createt a new ChildClass object with. My question is: If i already know that a ParentClass object with an id exists in the database, is it possible to create a ParentClass with that id and tell Spring to treat it as a valid <code>entity</code>?</p>
  <p>Thank you</p>