使用Spring数据将下面提供的实体存储到数据库时出现问题。两者之间使用了OneToOne关系。仅当parentObject显式设置为childObject时,它才有效。这不是一个很好的解决方案,因为我希望那个子对象根本没有父对象。有没有可能?
相关问题已发布here

@Entity
@Table(name = "parent_object")
public class ParentObject {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Long id;

@Column(name = "age")
private Long age;

@OneToOne(cascade = CascadeType.ALL, mappedBy = "parentObject")
private ChildObject childObject;

public ParentObject() {
}

public ParentObject(Long age, ChildObject childObject) {
    this.age = age;
    this.childObject = childObject;
}

public Long getId() {
    return id;
}

public Long getAge() {
    return age;
}

public ChildObject getChildObject() {
    return childObject;
}

public void setChildObject(ChildObject childObject) {
    this.childObject = childObject;
}
}

@Entity
@Table(name = "child_object")
public class ChildObject {
@Id
@Column(name = "child_id", unique = true, nullable = false)
private Long id;

@MapsId
@OneToOne
@JoinColumn(name = "child_id")
private ParentObject parentObject;

@Column(name = "name")
private String name;

public ChildObject() {
}

public ChildObject(String name) {
    this.name = name;
}

public Long getId() {
    return id;
}

public ParentObject getParentObject() {
    return parentObject;
}

public String getName() {
    return name;
}

public void setParentObject(ParentObject parentObject) {
    this.parentObject = parentObject;
}
}


但是,我真的不喜欢将parentObject设置为ChildObject:

ChildObject childObject = new ChildObject("name");
ParentObject parentObject = new ParentObject(12L, childObject);
childObject.setParentObject(parentObject);


这是共享ID的唯一方法吗?如果未显式设置parentObject,则会引发以下错误:

Caused by: org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property [test.ChildObject.parentObject]
    at org.hibernate.id.ForeignGenerator.generate(ForeignGenerator.java:83) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:101) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
    at org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:67) ~[hibernate-entitymanager-5.0.11.Final.jar:5.0.11.Final]

最佳答案

您可以轻松地封装操作,从而避免有问题的调用:实际上,应该始终这样做,以便内存模型始终处于一致状态:

public class ParentObject{

    public ParentObject(Long age, ChildObject childObject) {
        this.age = age;
        this.childObject = childObject;

        childObject.setParentObject(this);
    }

    public void setChildObject(ChildObject childObject) {
        this.childObject = childObject;
        childObject.setParentObject(this);
    }
}


现在,您的域模型API的客户端不需要了解有关反向引用表单“子级到父级”的任何信息。

ChildObject childObject = new ChildObject("name");
ParentObject parentObject = new ParentObject(12L, childObject); //all relationships now set correctly

关于java - 具有共享ID的OneToOne关系要求 child 了解父级,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43391714/

10-11 02:52