【问题标题】:Dozer - create reference of parent objectDozer - 创建父对象的引用
【发布时间】:2015-10-30 18:53:03
【问题描述】:

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

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

我的推土机 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 对象?也许我应该使用另一种方法?任何帮助表示赞赏。

【问题讨论】:

    标签: hibernate dozer


    【解决方案1】:

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

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

    不幸的是,我在 Dozer 文档中找不到其他解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-19
      • 1970-01-01
      • 2017-07-14
      • 2013-04-06
      相关资源
      最近更新 更多