title: Can you pass substitution variables to Hibernate mapping files?
body: <p>I had asked this question on the Hibernate forums a while back, but as of yet have not received a reply:
  <a href="https://forum.hibernate.org/viewtopic.php?f=1&amp;t=1008487" rel="nofollow">https://forum.hibernate.org/viewtopic.php?f=1&amp;t=1008487</a></p>
  
  <p>I am trying to figure out if there's some way to specify variables or properties during Hibernate configuration in order to modify the details of my mapping files.  </p>
  
  <p>For instance, say I have the following:</p>
  
  <pre><code>&lt;hibernate-mapping default-access="field"&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>  &lt;id name="${standardIdName}" column="id" type="long"&gt;
       &lt;generator class="sequence"&gt;
          &lt;param name="sequence"&gt;warning_seq&lt;/param&gt;
       &lt;/generator&gt;
    &lt;/id&gt;
  
    &lt;version name="version" column="version" /&gt;
  </code></pre>
  
  <p>
  </p>
  
  <p>I would like to have the value for id's name attribute set to ${standardIdName} and then pass in that ${standardIdName} during application startup.  This is very similar to what Spring allows you to do via PropertyConfigurer.</p>
  
  <p>The real use case driving this is that I would like to be able to have all of my relationships cascade during certain phases of testing.  </p>
  
  <p>If I am testing Child class and it needs to have a Parent class reference, I can use the test data builder pattern and a single hibernate save call to persist my object graph:</p>
  
  <pre><code>public class Parent {
      ...
  }
  
  public class Child {
      private Parent parent; 
      ...
  }
  </code></pre>
  
  <p>My test does something like:</p>
  
  <pre><code>Child child = new ChildBuilder().build() //Builds child class, a "dummy" Parent class and gives the Child that "Dummy" Parent class.
  childRepository.save(child);
  </code></pre>
  
  <p>In order for that second line to work, saving both the Child and the Parent, I need my mapping file to be something like this, where the Parent creation will be cascaded:</p>
  
  <pre><code>&lt;hibernate-mapping&gt;
      &lt;class name="Child" table="child"&gt;
          &lt;key column="id" /&gt;
  
          &lt;many-to-one name="Parent" column="parent_id" not-null="true"
              class="parent" cascade="all"/&gt;
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>But I really shouldn't have Child cascading up to Parent in production.  My proposed solution would be something like:</p>
  
  <pre><code>&lt;hibernate-mapping&gt;
      &lt;class name="Child" table="child"&gt;
          &lt;key column="id" /&gt;
  
          &lt;many-to-one name="parent" column="parent_id" not-null="true"
              class="parent" cascade="${cascadeType}"/&gt;
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>Where cascadeType is passed in during Hibernate configuration.</p>
  
  <p>Is there any functionality like this in Hibernate?</p>
  
  <p>Thanks!</p>
  
  <p>Leo</p>