title: Hibernate create domain instance with foreign key
body: <p>i have domain classes like this:</p>
  
  <p>extremely Simplified:</p>
  
  <pre><code>class Person extends Domain {
    Set&lt;Contracts&gt; contracts;
  
    //...
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "person", cascade = CascadeType.ALL)
    public Set&lt;Contracts&gt; getContracts() {
      return contracts;
    }
    //...
  }
  
  class Contract extends Domain {
    Person person;
  
    //...
    @JoinColumn(name = "PERSON__ID", nullable = false)
    public Person getPerson() {
      return this.person;
    }
    //...
  }
  </code></pre>
  
  <p>I got a instance of a person and want to create a new contract. 
  All over reflection.</p>
  
  <p>My first try was something like that:</p>
  
  <pre><code>Domain domain = ...;
  Method getter = resolveGetter(domain); // Person.getContracts()
  ParameterizedType returnType = (ParameterizedType) getter.getGenericReturnType();
  
  Class&lt;Domain&gt; cls = (Class&lt;Domain&gt;)returnType.getActualTypeArguments()[0];
  Domain instance = cls.newInstance();
  
  OneToMany annotation = getter.getAnnotation(OneToMany.class);
  String mappedBy = annotation.mappedBy();
  </code></pre>
  
  <p>and now i need the setter method from the cls wich the mappedBy descripe to set the person in the contract.
  But how i get the setter correctly?</p>
  
  <p>A possible solution is via method name of the class but i think its ugly. And i dont find a method of the hibernate framework to do this.</p>
  
  <p>Has someone an idea?</p>
  
  <p>Usage:</p>
  
  <ul>
  <li>Java 6 hibernate-core-3.6.10 </li>
  <li>hibernate-commons-annotations-3.2.0</li>
  <li>hibernate-jpa-2.0-api-1.0.1</li>
  </ul>
  
  <p>-- Update:</p>
  
  <p>I got the hibernate property via</p>
  
  <pre><code>JavaReflectionManager manager = new JavaReflectionManager();
  XClass xClass = manager.toXClass(cls);
  
  List&lt;XProperty&gt; declaredProperties = xClass.getDeclaredProperties(XClass.ACCESS_PROPERTY); // Our domains use only Property Access
  for (XProperty xProperty : declaredProperties) {
    if(xProperty.getName().equals(mappedBy)) {
       // How to set the value in the property?
    }
  }
  </code></pre>
  
  <p>Now were the question how to set the value in the property?</p>