我有兩個實體:休眠插入級聯不插入外鍵
@Entity
public class File
.......
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@OneToMany(fetch=FetchType.LAZY, mappedBy="file", cascade=CascadeType.ALL)
private List<Tag> tags;
.......
OTHER PROPERTIES
.......
@Entity
public class Tag
.......
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@ManyToOne
@JoinColumn(name="file_id")
private File file;
@Column
private String tag;
.......
OTHER PROPERTIES
.......
我試圖做插入到文件(以及隨後TAG)以下:
File file = new File();
Tag tag = new Tag();
tag.setTag("tag1");
Tag2 tag2 = new Tag();
tag2.setTag("tag2");
List<Tag> tags = new ArrayList<Tag>();
tags.add(tag);
tags.add(tag2);
file.setTags(tags);
---Add other file attributes here---
我然後插入用我的DAO文件:
sessionFactory.getCurrentSession().saveOrUpdate(file);
在我的日誌我看到一個插入到我的「文件」表2個鑲入我的標籤表,但是,我的標記表中指向我的文件表(file_id)的外鍵是NULL。
我可能會做錯什麼?
謝謝,這個工作。由於某種原因,我確實認爲Hibernate會爲我假設我希望它被Tag更新爲我的文件,因爲我有雙方註釋的關係。 – 2010-12-21 15:53:00
該鏈接不存在更多:(你可以提供其他來源? – Filipe 2014-02-20 21:30:03
我已經改變了答案,指向最新版本。 – jpkrohling 2014-02-21 08:03:32