2013-01-01 28 views
0

這是我的 「頭」 實體:@OneToMany關係: 「一」 ID不持久

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

爲什麼?

回答

1

是的,你必須打電話row.setTestataDocumento(d);,因爲它是關係的擁有方。理想情況下,您將在Documento中使用addRow()方法,該方法將添加要設置的行並設置該行的文檔。

1

是的,你必須,因爲你有一個雙向關聯,並且該關聯的所有者一方是RigaDocumento。所有者方是沒有mappedBy屬性的一方。另一面被稱爲反面,並被JPA/Hibernate忽略。