Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA Entity Mapping which is related based on two other entity mappings

Not sure if this is possible but trying to map WorkflowInstancePlayer player which is related based on two other entity mappings, WorkActionClass and WorkflowInstance in the entity below.

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;


The workflowInstancePlayer is derived based on workflow_instance_id and role_class_id but role_class_id is really an attibute of workActionClass mapped above (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;

Example data would be:

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

So in the Action Entity, I'll know the Workflow Instance Player is Employee with Id 111222 without actually storing the badge in the Action table.

UPDATE

Based on Vlad's post I tweaked it to be

@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"))
})
like image 500
jeff Avatar asked Jul 14 '15 13:07

jeff


1 Answers

Try with this mapping:

@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;
like image 69
Vlad Mihalcea Avatar answered Oct 20 '22 10:10

Vlad Mihalcea