0

我有这两个 Openjpa 实体:

@Entity
@Table(name = "os_wfentry")
public class JPAWorkflowEntry implements WorkflowEntry, Serializable {

    private static final long serialVersionUID = -755511983025049452L;

    @Id
    private long id;

    @Column(name = "name")
    private String workflowName;

    @Column(name = "state")
    private Integer workflowState;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "workflowEntry")
    private final List<JPACurrentStep> currentSteps;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "workflowEntry")
    private final List<JPAHistoryStep> historySteps;

... 和这个:

@Entity
@Table(name = "os_currentstep")
public class JPACurrentStep implements Serializable, Step {

    private static final long serialVersionUID = -3662582760248954945L;

    @Id
    private Long id;

    @Column(name = "action_Id")
    protected Integer actionId;

    @Column(name = "step_Id")
    protected Integer stepId;

    protected String caller;

    @Column(name = "finish_Date")
    @Temporal(TemporalType.TIMESTAMP)
    protected Date finishDate;

    @Column(name = "start_Date")
    @Temporal(TemporalType.TIMESTAMP)
    protected Date startDate;

    @Column(name = "due_Date")
    @Temporal(TemporalType.TIMESTAMP)
    protected Date dueDate;

    @Column
    protected String owner;

    protected String status;

    @ManyToOne
    protected JPAWorkflowEntry workflowEntry;

当我运行我的应用程序时,我遇到了这个 SQL 错误:

ERROR: there is no unique constraint matching given keys for referenced table "os_wfentry" {stmnt 989537113 ALTER TABLE os_currentstep ADD FOREIGN KEY (workflowentry_id) REFERENCES os_wfentry (id) DEFERRABLE} [code=0, state=42830]

在我看来没关系,实际上表格是由唯一的 id “链接”的,我不明白为什么它会给我这个错误。

4

1 回答 1

1

它正在尝试设置外键关系os_currentstep.workflowentry_id->os_wfentry.id但要使其有效,os_wfentry.id必须是有效的候选键,从 SQL Server 的角度来看,这意味着它必须是主键或由唯一约束覆盖的不可为空的列-该错误告诉您情况并非如此,因此无法添加外键。

我不熟悉 Openjpa,所以我不能告诉你如何在它的语法中解决这个问题。您需要标记为该表的主键(假设它是)id添加覆盖它的唯一索引。os_wfentry

于 2013-09-25T10:34:55.943 回答