在具有连接表的JPA中加入复合键的一部分

时间:2017-07-19 05:56:23

标签: java jpa join spring-boot composite-key

我的两个类之间有一个连接条件,它使用连接表连接。

项目类别:

public class Item {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "item_workflow", joinColumns = 
@JoinColumn(name = "item_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "workflow_id",
referencedColumnName = "id"))
private List<Workflow> workflow;}

工作流程类:

public class Workflow {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String actor;
private String assignee;
private String date;
private String team;}

这两个表由一个连接表连接,连接表包含以下列:

|Item_id | Workflow_id|

上面的代码工作正常。我现在正在尝试向工作流类添加一个复合键,它将是id,actor和date的组合。我想这样做,因为在同一时间戳(日期)两次应该没有同一个actor的工作流ID。

当我使用id类创建复合键时,我收到以下错误:

Caused by: org.hibernate.AnnotationException: referencedColumnNames(id) of application.model.Item.workflow referencing application.model.Workflow not mapped to a single property

以下是Id类:

public class Workflow_comp_key implements Serializable {

@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String actor;
private String date;}

将id类添加到工作流后,它的外观如下:

@IdClass(Workflow_comp_key.class)
public class Workflow {
public static final String FIND_ALL = "findAll";

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Id
private String actor;
private String assignee;
@Id
private String date;
private String team;
}

我搜索了这个问题,大部分答案都要求我在连接条件中添加复合键的所有组件。我尝试了以下方法:

@OneToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "item_workflow", joinColumns = @JoinColumn(name = "item_id", referencedColumnName = "id"),
            inverseJoinColumns = {@JoinColumn(name = "workflow_id", referencedColumnName = "id"),
                                  @JoinColumn(name = "actor", referencedColumnName = "actor"),
                                  @JoinColumn(name = "date", referencedColumnName = "date")})
    private List<Workflow> workflow;

一旦我添加了这个,编译器就无法在连接表中找到actor和date,这很明显,它们在连接表中不存在。以下是问题:

Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing column [actor] in table [item_workflow]

现在我无法完成这项工作。任何帮助高度赞赏。

0 个答案:

没有答案