title: During a Hibernate Cascaded save, how can we ensure a conditional update of inner objects
body: <pre><code>Consider the following class:
  Class MainObject {
      ....
      ChildObject1 child1;
      ChildObject2 child2;
      ChildObject3 child3;
      ChildObject4 child4;
  }
  </code></pre>
  
  <p>I need to save a newly created object of type MainObject using Hibernate. I added two newly created objects to it child1 and child2 since they were not present in DB. 
  Let us say child3 and child4 were already present in DB. However I may not prefer to load the entire objects from DB and add them to the MainObject since those objects may have too many attributes, which are not needed.
  So I just fetch the DB keys for the two objects and add the keys to newly instantiated ChildObject3 and ChildObject4 objects (which will have all other fields as null). I add these objects to MainObject.</p>
  
  <p>Now, if I try to save MainObject in CASCADED mode, ChildObject3 and ChildObject4 will also get updated in DB with all null values, which I do not want. What I want is a way of cascading by which only non-persisted objects will be saved to DB (in this case ChildObject1 and ChildObject2). The other objects which hold a DB key will not be updated and will only serve to save MainObject.</p>
  
  <p>I know cascading cannot function in this way. Is there any other way to do it such refresh the whole object hierarchy such that ChildObject3 and ChildObject4 get refreshed from DB and then I can save the MainObject in cascaded mode? Or can I put certain conditions in callout methods whether to update or not to update an object by checking if it holds a DB key or not?</p>
  
  <p>I feel this is a needed operation. Whenever we save a new object, we may not always want to load all attributes of all the inner objects, just to be able to save the main object in cascaded mode. All we need is the DB primary key and may not prefer to stress the memory by loading all fields of all inner objects. </p>
  
  <p>Any good solution or best practices, please suggest.</p>