JPA:@JoinTable-这两列都是主键. [英] JPA: @JoinTable - Both columns are Primary Keys.. How do I stop that?

查看:507
本文介绍了JPA:@JoinTable-这两列都是主键.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用来生成联接表的注释.

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;
}

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

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?

推荐答案

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

I fixed my problem by adding the following changes:

我将我的@OneToMany更改为@ManyToMany注释

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;
}

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

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;
}

这篇关于JPA:@JoinTable-这两列都是主键.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆