0

这是我的“标题”实体:

@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);

?

为什么??

4

2 回答 2

1

是的,你必须打电话row.setTestataDocumento(d);,因为这是关系的拥有方。理想情况下,您将addRow()在 Documento 中有一个方法,该方法将添加要设置的行并设置该行的文档。

于 2013-01-01T17:43:02.760 回答
1

是的,您必须这样做,因为您有一个双向关联,并且关联的所有者是 RigaDocumento。所有者方是没有mappedBy属性的一方。另一面称为反面,JPA/Hibernate 会忽略它。

于 2013-01-01T17:43:33.803 回答