JPA:@JoinTable - 两列都是主键..我该如何阻止它?
这是我用来生成连接表的注释。
@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过添加以下更改解决了我的问题:
我将 @OneToMany 更改为 @ManyToMany 注释
我添加了 Set 工作流程;我的服务对象中的关联
I fixed my problem by adding the following changes:
I changed my @OneToMany to a @ManyToMany annotation
I added a Set workflows; association in my Service object
这对我来说看起来是正确的。连接表中的每一行应标识一对工作流/服务项。因此
(workflow_id, service_id)
应该是主键。此外,workflow_id
应该是workflow
表中的外键,service_id
应该是service
表中的外键。另请注意,A 和 B 之间的一对多关联并不意味着 A 的实例可以多次拥有相同的 B 实例,而是 A 的实例可以拥有多个不同的 B 实例。例如博客 < code>Post 实体可以与
Tag
实体具有一对多关联。这意味着一个博客Post
P1可以有多个标签Java
、JPA
、JavaEE
,但不能有同一标签多次。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. Alsoworkflow_id
should be a foreign key into theworkflow
table andservice_id
should be a foreign key into theservice
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 aTag
entity. This means that a blogPost
P1 can have multiple tagsJava
,JPA
,JavaEE
, but can not have the same tag multiple times.