JPA实体映射,它基于另外两个实体映射关联

时间:2015-07-14 13:32:49

标签: java hibernate jpa orm hibernate-mapping

不确定这是否可行,但尝试根据下面实体中的其他两个实体映射WorkflowInstancePlayer playerWorkActionClass来映射相关的WorkflowInstance

public class Action implements Serializable {
@Id
private Long action_id;

@ManyToOne
@JoinColumn(name = "work_action_class_id", referencedColumnName = "work_action_class_id")
private WorkActionClass workActionClass;

@ManyToOne
@JoinColumn(name = "workflow_instance_id", referencedColumnName = "workflow_instance_id")
private WorkflowInstance workflowInstance;

UPDATE: How can I map to a WorkflowInstancePlayer player?????
 @ManyToOne
 @JoinColumns( {
        @JoinColumn(name = "workflow_instance_id", referencedColumnName = "workflow_instance_id", insertable = false, updatable = false),
        @JoinColumn(name = "workActionClass.role_class_id", referencedColumnName = "role_class_id", insertable = false, updatable = false)
})
 private WorkflowInstancePlayer player;


workflowInstancePlayer是基于workflow_instance_idrole_class_id派生的,但role_class_id实际上是workActionClass (workActionClass.role_class_id)上面映射的public class WorkflowInstancePlayer implements Serializable { @Id private WorkflowInstance workflowInstance; @Id private RoleClass roleClass; @ManyToOne @JoinColumn(name = "badge", referencedColumnName = "badge") private Employee employee; 的归属地


public class WorkActionClass implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "trx_seq")
private Long work_action_class_id;

@ManyToOne
@JoinColumn(name = "role_class_id")
private RoleClass roleClass;


Action
------
Id = 10
work_action_class_id = 7
workflow_instance_id = 2


WorkActionClass
---------------
Id = 7
role_name = reviewer
role_class_id = 3


WorkflowInstancePlayer
----------------------
workflow_instance_id = 2
role_class_id = 3
badge = 111222

示例数据将是:

@ManyToOne(fetch=FetchType.LAZY)
   @JoinColumnsOrFormulas({
   @JoinColumnOrFormula(formula=@JoinFormula(value="(SELECT a.role_class_id FROM (Use Table Name not Entity Name here) a WHERE a.work_action_class_id = work_action_class_id)", referencedColumnName="role_class_id")),
   @JoinColumnOrFormula(column = @JoinColumn(name="workflow_instance_id", referencedColumnName="workflow_instance_id"))
})

因此,在Action Entity中,我将知道工作流实例播放器是具有Id 111222的Employee而没有将徽章实际存储在Action表中。

更新

根据弗拉德的帖子,我把它调整为

{{1}}

1 个答案:

答案 0 :(得分:6)

尝试使用此映射:

@ManyToOne
@JoinColumn(
    name = "workflow_instance_id", 
    referencedColumnName = "workflow_instance_id",
    insertable = false, 
    updatable = false
)
private WorkflowInstance workflowInstance;

@ManyToOne
@JoinColumnOrFormula(
    formula = @JoinFormula(
            value="(SELECT a.work_action_class_id FROM WorkActionClass a WHERE a.role_class_id = role_class_id)", 
        referencedColumnName = "work_action_class_id"
    )
)
private WorkActionClass workActionClass;

@ManyToOne
@JoinColumns( {
    @JoinColumn(
        name = "workflow_instance_id", 
        referencedColumnName = "workflow_instance_id"),
    @JoinColumn(
        name = "role_class_id", 
        referencedColumnName = "role_class_id")
})
private WorkflowInstancePlayer player;