带对象引用的JPA Composite Key

时间:2014-04-14 17:28:10

标签: java entity-framework jpa-2.0 composite-key

在使用Object引用创建JPA Composite Key时遇到了一些问题。实体如下图所示,

1)我想从工作流实体中删除 ID 字段,并使用 seqNo 字段和模板组合制作复合键(对象参考) )字段

2)根据该更改更新了与 Command 实体的现有关系(@JoinColumn(name =“WORKFLOW_ID”,referencedColumnName =“ID”))

感谢。

模板实体:

@Entity
@Table(name = "template")
public class Template implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @SequenceGenerator(name="template_se", sequenceName="TEMPLATE_SE", allocationSize=1, initialValue=1)
    @Basic(optional = false)
    @Column(name = "ID")
    private Integer id;

    @Basic(optional = false)
    @Column(name = "NAME", unique = true)
    private String name;

    @OneToMany(cascade = CascadeType.REMOVE, mappedBy = "template")
    @LazyCollection(LazyCollectionOption.FALSE)
    private List<Workflow> workflowList;
}

工作流程实体:

@Entity
@Table(name = "workflow")
public class Workflow implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @SequenceGenerator(name="wf_se", sequenceName="WF_SE", allocationSize=1, initialValue=1)
    @Basic(optional = false)
    @Column(name = "ID")
    private Integer id;

    @Basic(optional = false)
    @Column(name = "SEQ_NO")
    private int seqNo;

    @JoinColumn(name = "T_ID", referencedColumnName = "ID", nullable = false)
    @ManyToOne
    private Template template;

    @Basic(optional = false)
    @Column(name = "NAME")
    private String name;
}

指挥实体:

@Entity
@Table(name = "command")
public class Command implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @SequenceGenerator(name="command_se", sequenceName="COMMAND_SE", allocationSize=1, initialValue=1)
    @Basic(optional = false)
    @Column(name = "ID")
    private Integer id;

    @JoinColumn(name = "WORKFLOW_ID", referencedColumnName = "ID")
    @ManyToOne(optional = false)
    private Workflow workflow;
} 

1 个答案:

答案 0 :(得分:2)

我已按照以下方式完成了这些事情。

EmbeddedId类

@Embeddable
public class WorkflowPK implements Serializable {

    @Basic(optional = false)
    @Column(name = "SEQ_NO")
    private int seqNo;

    @JoinColumn(name = "T_ID", referencedColumnName = "ID", nullable = false)
    @ManyToOne
    private Template template;
} 

工作流程实体(使用EmbeddedId)

@Entity
@Table(name = "workflow")
public class Workflow implements Serializable {

    @EmbeddedId
    private WorkflowPK id;

    @Basic(optional = false)
    @Column(name = "NAME")
    private String name;
}
具有双向关系的

模板实体

@Entity
@Table(name = "template")
public class Template implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @SequenceGenerator(name="template_se", sequenceName="TEMPLATE_SE", allocationSize=1, initialValue=1)
    @Basic(optional = false)
    @Column(name = "ID")
    private Integer id;

    @Basic(optional = false)
    @Column(name = "NAME", unique = true)
    private String name;

    // Refer the filed "template" inside the Composite key of Workflow entity

    @OneToMany(cascade = CascadeType.REMOVE, mappedBy = "id.template")
    @LazyCollection(LazyCollectionOption.FALSE)
    @OrderBy(value = "id.stepSeqNo")
    private List<Workflow> workflowList;
} 
带有JoinColumn参考的

命令实体

@Entity
@Table(name = "command")
public class Command implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @SequenceGenerator(name="command_se", sequenceName="COMMAND_SE", allocationSize=1, initialValue=1)
    @Basic(optional = false)
    @Column(name = "ID")
    private Integer id;

    // Refer the Composite key of Workflow entity

    @ManyToOne(optional = false)
    @JoinColumns ({
            @JoinColumn(name="WORKFLOW_SEQ_NO_ID", referencedColumnName = "SEQ_NO"),
            @JoinColumn(name="WORKFLOW_T_ID", referencedColumnName = "T_ID"),
    })
    private WorkflowStep workflow;
}