Spring Web MVC 中的关系(使用 Roo)

发布于 2024-11-13 06:11:58 字数 247 浏览 5 评论 0原文

在 Spring Web MVC(使用 Spring Roo)中指定一对多关系的正确方法是什么?

示例:一个人有姓名和电子邮件。一个团队有一个名字。一个人拥有一个团队的成员资格,而一个团队有零个或多个成员。用户想要 a) 设置个人的成员资格,b) 设置团队的成员。

如果使用“人员”的引用字段创建关系,则成员在“团队”视图中不可见。如果使用团队的设置字段创建关系(无论如何,这实际上是多对多关系),则会出现类似的结果。

我缺少什么?

What is the correct way to specify a one-to-many relationship in Spring Web MVC (using Spring Roo)?

Example: A Person has a name and an email. A Team has a name. A Person has a membership in a Team, and a Team has zero or more members. The user would like to a) Set the membership for a person, b) Set the members for a Team.

If the relation is created using a reference field for Person, members are not visible in the view for Team. There's a similar result if the relation is created using a set field for Team (which really is a many-to-many relation anyway).

What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

听风吹 2024-11-20 06:11:58

这里您需要的是双向关系(默认情况下不创建)。

生成实体时,您需要在团队中添加集合关联,以及团队中的人员关联。这可能也是一个好主意(根据您的命名约定,在生成的团队集的 OneToMany 注释中添加mappedBy 属性。如果您想由 Roo 完成此操作,只需在字段集命令上使用 --mappedBy 即可。该值将是 Person 实体中 Team 引用的字段名称。

两个引用都就位后,roo 应该生成正确的脚手架。

What you need here is a bidirectional relationship (which is not created by default).

When you generate your entities, you need to add both the Set association in Team, AND the Person association in Team. It will probably also be a good idea (depending on your naming convention to add the mappedBy attribute in the OneToMany annotation on the generated Team Set. If you want to get this done by Roo, just used --mappedBy on the field set command. The value for this will be the field name of the Team reference in the Person entity.

With both references in place, roo should generate the correct scaffolding.

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