JPA Composite Key with Object references

孤街浪徒 提交于 2019-12-13 07:33:49

问题


I have faced some issue while creating JPA Composite Key with Object references. Entities are as show in bellow,

1) I wan to remove the ID field from Workflow entity and make a composite key with combining seqNo field and template (object reference) field

2) According to that change updated the existing relationship with Command entity (@JoinColumn(name = "WORKFLOW_ID", referencedColumnName = "ID"))

Thanks.

Template entity:

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

Workflow entity:

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

Command entity:

@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:


I have got the things done on following manner.

EmbeddedId class

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

Workflow Entity (with EmbeddedId)

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

    @EmbeddedId
    private WorkflowPK id;

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

Template entity with bidirectional relation

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

Command entity with JoinColumn reference

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


来源:https://stackoverflow.com/questions/23066432/jpa-composite-key-with-object-references

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!