[英]@OneToMany relationship: “one” id not persisted
这是我的“标题”实体:
@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 );
然后,我一直存在天 。
标题是正确持久和行,但......
行记录中的“testataDocumento_id”字段(关系的“一”侧的键)为NULL。
我必须做一个:
row.setTestataDocumento(d);
?
为什么??
是的,你必须调用row.setTestataDocumento(d);
因为这是关系的拥有方。 理想情况下,您将在Documento中使用addRow()
方法,该方法将添加要设置的行并设置行的文档。
是的,您必须,因为您有双向关联,并且该关联的所有者方是RigaDocumento。 所有者方是没有mappedBy
属性的一方。 另一边称为反面,并被JPA / Hibernate忽略。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.