Hibernate插入级联不插入外键

发布于 2024-10-08 10:23:08 字数 1018 浏览 0 评论 0原文

我有两个实体:

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世界如花海般美丽 2024-10-15 10:23:08

您没有为标签设置文件,只是将标签设置为文件。请记住,在 OOP 中,与关系模型相反,您必须设置关系的两端。您不能仅仅因为向文件添加了一组标签而从标签导航到文件。在您的情况下,您只需从文件导航到标签(即:列出文件的所有标签)。仅通过查看标签无法判断标签属于哪个文件。

通常所做的是模型之一中的辅助方法,如下所示:

public void addTag(Tag tag) {
  this.tags.add(tag);
  tag.setFile(this);
}

请参阅 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:

public void addTag(Tag tag) {
  this.tags.add(tag);
  tag.setFile(this);
}

See this for an example (from Hibernate's test suite):

清醇 2024-10-15 10:23:08

数据库中的外键反映了 Tag.file 的状态(因为 Tag 是关系的拥有方,作为双向多对一中的“多”方)关系)。

我看不到你在哪里设置的。

Foreign key in the database reflects the state of Tag.file (since Tag 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文