推土机 - 创建父对象的引用

时间:2015-10-30 18:53:04

标签: hibernate dozer

我正在尝试使用Dozer从JAXB实体“JaxbParent”填充Hibernate实体 - “Parent”。 我的Hibernate实体:

public class Parent 
{
    String name;
    String age; 

    @OneToMany
    private Set<Child> childSet;
}

public class Child
{
  String name;
  String age;

  @ManyToOne
  private Parent parent;
}

我的Jaxb实体看起来像:

public class JaxbParent 
{
   List<JaxbChild> childList;
}

我的Dozer xml映射配置:

<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>

因此,为了将childList转换为childSet,我使用CustomConverter,并获得正确的数据字段。 问题是,Hibernate需要每个Child都引用Parent对象(执行保存),但是当前它是null。我尝试将'this'引用传递给MyCustomConverter,但这没有成功。 如何将Parent对象的引用传递给customConverter,并传递给每个Child对象?也许我应该使用另一种方法?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

最后,我最终在保存我的实体之前手动添加对DAO图层中父对象的引用:

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

不幸的是,我无法在Dozer文档中找到另一种解决方案。