簡體   English   中英

@OneToMany關系:“one”id沒有持久化

[英]@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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM