这是我的“标题”实体:
@Entity
@Table(name = "documenti")
@Inheritance(strategy=InheritanceType.JOINED)
public class Documento implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@OneToMany(
mappedBy="testataDocumento",
cascade=CascadeType.ALL,
fetch=FetchType.LAZY
)
private Set<RigaDocumento> righe= new HashSet<RigaDocumento>();
//...
}
这些是“行”:
@Entity
@Table(name="righe_documenti")
public class RigaDocumento implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@ManyToOne
private Documento testataDocumento;
//...
}
我有一个常见的情况:
Documento d = new Documento();
// various Documento settings
List<RigaDocumento> rows;
// creation of rows
d.setRighe( rows );
然后我坚持d。
标题和行也正确保留,但是...
行记录中的“testataDocumento_id”字段(关系“一”方的键)为 NULL。
我必须这样做吗:
row.setTestataDocumento(d);
?
为什么?
最佳答案
是的,您必须调用row.setTestataDocumento(d);
,因为它是关系的拥有方。理想情况下,Documento 中应该有一个 addRow()
方法,该方法将添加要设置的行并设置该行的文档。
关于java - @OneToMany关系: "one" id not persisted,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14112673/