java - 外键未存储在子实体中(一对多)

标签 java hibernate mapping annotations

我是 hibernate 的新手,偶然发现了这个问题,我找不到解决方案。

当持久化父对象(与子对象具有一对多关系)时,该父对象的外键不存储在子对象的表中。

我的类(class):

父类.java

@javax.persistence.Table(name = "PARENT")
@Entity
public class PARENT {
  private Integer id;

  @javax.persistence.Column(name = "ID")
  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  private Collection<Child> children;

  @OneToMany(mappedBy = "parent", fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
  @Cascade({org.hibernate.annotations.CascadeType.ALL})
  public Collection<Child> getChildren() {
    return children;
  }

  public void setChildren(Collection<Child> children) {
    this.children = children;
  }
}

子类.java

@javax.persistence.Table(name = "CHILD")
@Entity
@IdClass(Child.ChildId.class)
public class Child {
  private String childId1;

  @Id
  public String getChildId1() {
    return childId1;
  }

  public void setChildId1(String childId1) {
    this.childId1 = childId1;
  }

  private String childId2;

  @Id
  public String getChildId2() {
    return childId2;
  }

  public void setChildId2(String childId2) {
    this.childId2 = childId2;
  }

  private Parent parent;

  @ManyToOne
  @javax.persistence.JoinColumn(name = "PARENT_ID", referencedColumnName = "ID")
  public Parent getParent() {
    return parent;
  }

  public void setParent(Operation parent) {
    this.parent = parent;
  }

  public static class ChildId implements Serializable {
    private String childId1;

    @javax.persistence.Column(name = "CHILD_ID1")
    public String getChildId1() {
      return childId1;
    }

    public void setChildId1(String childId1) {
      this.childId1 = childId1;
    }

    private String childId2;

    @javax.persistence.Column(name = "CHIILD_ID2")
    public String getChildId2() {
      return childId2;
    }

    public void setChildId2(String childId2) {
      this.childId2 = childId2;
    }

    public ChildId() {
    }

    @Override
    public boolean equals(Object o) {
      if (this == o) return true;
      if (o == null || getClass() != o.getClass()) return false;

      ChildId that = (ChildId) o;

      if (childId1 != null ? !childId1.equals(that.childId1) : that.childId1 != null) 
        return false;
      if (childId2 != null ? !childId2.equals(that.childId2) : that.childId2 != null)
        return false;

      return true;
    }

    @Override
    public int hashCode() {
      int result = childId1 != null ? childId1.hashCode() : 0;  
      result = 31 * result + (childId2 != null ? childId2.hashCode() : 0);
      return result;
    }
  }
}

测试.java

public class Test() {

  private ParentDao parentDao;

  public void setParentDao(ParentDao parentDao) {
    this.parentDao = parentDao;
  }

  private ChildDao childDao;

  public void setChildDao(ChildDao childDao) {
    this.childDao = parentDao;
  }

  test1() {
    Parent parent = new Parent();

    Child child = new Child();
    child.setChildId1("a");
    child.setChildId2("b");
    ArrayList<Child> children = new ArrayList<Child>();
    children.add(child);

    parent.setChildren(children);
    parent.setValue("value");

    parentDao.save(parent); //calls hibernate's currentSession.saveOrUpdate(entity)
  }

  test2() {
    Parent parent = new Parent();
    parent.setValue("value");
    parentDao.save(parent); //calls hibernate's currentSession.saveOrUpdate(entity)

    Child child = new Child();
    child.setChildId1("a");
    child.setChildId2("b");
    child.setParent(parent);
    childDao.save(); //calls hibernate's currentSession.saveOrUpdate(entity)
  }
}

当调用 test1() 时,两个实体都被写入数据库,但 CHILD 表中的字段 PARENT_ID 保持为空。

到目前为止,我唯一的解决方法是 test2() - 先保留父项,然后是子项。

我的目标是在 Parent 上对 save() 的一次调用中保留父项及其子项。 有什么想法吗?

最佳答案

你也可以给你的 parent 添加一个额外的方法,比如

public void addChild(Child child) 

它做了类似的事情

    child.setParent(this);

关于java - 外键未存储在子实体中(一对多),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2521659/

相关文章:

reactjs - 当映射到 React 中的对象数组时,所有模态都会立即打开

java - 流列表、映射和设置为新列表

java - 在 Android java 中读取文本文件或 XML 文件

c# - 对象内的链接是否被视为反模式?

java - 查询Class类的实例

java - jackson 将多个对象序列化为一个

c# - NHibernate 映射域 "extension properties"?

java - 在Java中,什么命令行可以让计算机点击屏幕?

java - Spring Boot,多对一关系获取 LAZY 或 EAGER

java - 防止 hibernate 刷新只读操作