具有一对多关系的表列上的唯一约束错误

时间:2013-09-25 10:02:25

标签: sql

我有两个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]

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

1 个答案:

答案 0 :(得分:1)

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

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