Search code examples
hibernatedozer

Dozer - create reference of parent object


I'm trying to populate Hibernate entity - "Parent", from JAXB entity "JaxbParent" using Dozer. My Hibernate entity:

public class Parent 
{
    String name;
    String age; 

    @OneToMany
    private Set<Child> childSet;
}

public class Child
{
  String name;
  String age;

  @ManyToOne
  private Parent parent;
}

My Jaxb entity looks like:

public class JaxbParent 
{
   List<JaxbChild> childList;
}

My Dozer xml mapping config:

<mapping wildcard="false">
    <class-a>com.test.Parent</class-a>
    <class-b>com.test.JaxbParent</class-b>
    <field custom-converter="com.test.MyCustomConverter">
      <a>childSet</a>
      <b>childList</b>
    </field>
</mapping>

So, for converting childList to childSet i use CustomConverter, and i get correct data fields. The problem is, that Hibernate needs that every Child have reference to Parent object(to perform saving), but currently it is null. I try to pass 'this' reference to MyCustomConverter, but this don't succeded. How can i pass reference of Parent object to customConverter, to every Child object? Maybe I should use another approach? Any help appreciated.


Solution

  • Finally, i end up with manually adding reference to Parent object in my DAO layer, just before saving my entity:

    if(child.parent == null) {
    child.parent = parent;
    }
    

    Unfortuntely, i can't find another solution in Dozer docs.