Hibernate插入级联不插入外键
我有两个实体:
@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
.......
我尝试通过执行以下操作插入到文件(以及随后的标签)中:
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。
我可能做错了什么?
I have two entities:
@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
.......
I am trying to insert into File (and subsequently Tag) by doing the following:
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---
I am then inserting the file in my DAO using:
sessionFactory.getCurrentSession().saveOrUpdate(file);
In my logs I see an insert into my "file" table and 2 inserts into my tag table, however, the foreign key in my tag table that points to my file table (file_id) is NULL.
What could I possibly be doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有为标签设置文件,只是将标签设置为文件。请记住,在 OOP 中,与关系模型相反,您必须设置关系的两端。您不能仅仅因为向文件添加了一组标签而从标签导航到文件。在您的情况下,您只需从文件导航到标签(即:列出文件的所有标签)。仅通过查看标签无法判断标签属于哪个文件。
通常所做的是模型之一中的辅助方法,如下所示:
请参阅 this 为例(来自 Hibernate 的测试套件):
You are not setting the File for a Tag, just the Tag's to a File. Remember that in OOP, as opposed to the Relational Model, you have to set both ends of a relationship. You can't navigate from Tag to File just because you added a set of Tags to a File. In your case, you can just navigate from File to Tag (ie: list all Tags for a File). You can't tell which File a Tag belongs to, by looking only at the Tag.
What is usually done is a helper method in one of the models, like this:
See this for an example (from Hibernate's test suite):
数据库中的外键反映了
Tag.file
的状态(因为Tag
是关系的拥有方,作为双向多对一中的“多”方)关系)。我看不到你在哪里设置的。
Foreign key in the database reflects the state of
Tag.file
(sinceTag
is the owning side of the relationship as a "many" side in a bidirectional many-to-one relationship).I can't see where you set it.