I have an entity structure that looks like this:
@Entity
public class Event {
@Id
private Long id;
@ManyToOne
private Device device;
@Column
private Severity severity;
... getters/setters/other attrs ...
}
@Entity
public class Device {
@Column
private Impact impact;
@ManyToOne
private PriorityMatrix priorityMatrix;
... getters/setters/other attrs ...
}
@Entity
public class Priority {
@EmbeddedId
private PriorityId id;
@Column
private Long value;
... getters/setters ...
}
@Embeddable
public class PriorityId {
@Column
private Severity severity;
@Column
private Impact impact;
@ManyToOne
private PriorityMatrix matrix;
... getters/setters ...
}
Impact and Severity are enums with fixed values.
Can I add a ‘transient’ attribute “Priority” to Event entity that is mapped by the devices impact and priority matrix, and by the event severity? If yes, how?
in SQL it would be some joins, something like
SELECT priority_matrix.priority_value,
-- event attributes
-- device attributes
FROM event
INNER JOIN device ON { -- event x device join }
INNER JOIN priority_matrix ON {
device.priority_matrix_id = priority_matrix.id
AND device.impact = priority_matrix.impact
AND event.severity = priority_matrix.severity
}
I want to do this because the priority matrix can be updated and shared by different devices, so the priority value must be always obtained when getting the event, but I want to load the priority value at the moment I load Event.
The solution was to save the priority directly at Event for performance issues. Then, when device impact is modified, the event priority is changed accordingly.