JPA:@JoinTable - 两列都是主键..我该如何阻止它?

发布于 2024-12-07 14:54:58 字数 896 浏览 0 评论 0原文

这是我用来生成连接表的注释。

@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "service_operations", 
        joinColumns = { @JoinColumn(name = "serviceId") },
        inverseJoinColumns = { @JoinColumn(name = "operationId") })
public Set<Operation> getOperations() {
    return operations;
}

考虑到这是一个 OneToMany 关联,我的自然假设是该表将生成一个

[ Primary Key |外键]表,但是每次我删除并重新创建数据库时,情况并非如此:

mysql> describe workflow_services;
+-------------+------------+------+-----+---------+-------+
| Field       | Type       | Null | Key | Default | Extra |
+-------------+------------+------+-----+---------+-------+
| workflow_id | bigint(20) | NO   | PRI | NULL    |       |
| service_id  | bigint(20) | NO   | PRI | NULL    |       |
+-------------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

我对此感到有点困惑。有什么建议吗?

This is my annotation I use to generate my Join Table.

@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "service_operations", 
        joinColumns = { @JoinColumn(name = "serviceId") },
        inverseJoinColumns = { @JoinColumn(name = "operationId") })
public Set<Operation> getOperations() {
    return operations;
}

Considering this is a OneToMany association, my natural assumption is that this table would generate a

[ Primary Key | Foreign Key ] table, however everytime I drop and re create the database it is not the case:

mysql> describe workflow_services;
+-------------+------------+------+-----+---------+-------+
| Field       | Type       | Null | Key | Default | Extra |
+-------------+------------+------+-----+---------+-------+
| workflow_id | bigint(20) | NO   | PRI | NULL    |       |
| service_id  | bigint(20) | NO   | PRI | NULL    |       |
+-------------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

Im a tad baffled by this. Any suggestions?

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

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

发布评论

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

评论(2

恋竹姑娘 2024-12-14 14:54:58

我通过添加以下更改解决了我的问题:

我将 @OneToMany 更改为 @ManyToMany 注释

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "workflow_services", 
        joinColumns = @JoinColumn(name = "workflow_id"), 
        inverseJoinColumns = @JoinColumn(name = "service_id"))
public Set<Service> getServices() {
    return services;
}

我添加了 Set 工作流程;我的服务对象中的关联

@ManyToMany(mappedBy="services")  // map info is in person class
public Set<Workflow> getWorkflows() {
    return workflows;
}

I fixed my problem by adding the following changes:

I changed my @OneToMany to a @ManyToMany annotation

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "workflow_services", 
        joinColumns = @JoinColumn(name = "workflow_id"), 
        inverseJoinColumns = @JoinColumn(name = "service_id"))
public Set<Service> getServices() {
    return services;
}

I added a Set workflows; association in my Service object

@ManyToMany(mappedBy="services")  // map info is in person class
public Set<Workflow> getWorkflows() {
    return workflows;
}
左耳近心 2024-12-14 14:54:58

这对我来说看起来是正确的。连接表中的每一行应标识一对工作流/服务项。因此 (workflow_id, service_id) 应该是主键。此外,workflow_id 应该是 workflow 表中的外键,service_id 应该是 service 表中的外键。

另请注意,A 和 B 之间的一对多关联并不意味着 A 的实例可以多次拥有相同的 B 实例,而是 A 的实例可以拥有多个不同的 B 实例。例如博客 < code>Post 实体可以与 Tag 实体具有一对多关联。这意味着一个博客Post P1可以有多个标签JavaJPAJavaEE,但不能有同一标签多次。

This looks correct to me. Each row in the join table should identify a pair of workflow/service items. So (workflow_id, service_id) should be the primary key. Also workflow_id should be a foreign key into the workflow table and service_id should be a foreign key into the service table.

Also note that a one-to-many association between A and B does not mean that an instance of A can have the same instance of B multiple times, rather an instance of A can have multiple distinct instances of B. For example a blog Post entity can have a one-to-many association with a Tag entity. This means that a blog Post P1 can have multiple tags Java, JPA, JavaEE, but can not have the same tag multiple times.

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