我有一个如下所示的实体结构:
@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 ...
}
影响和严重程度是具有固定值的枚举。
我可以向设备影响和优先级矩阵映射的事件实体以及事件严重性添加“瞬态”属性“优先级”吗?如果是,怎么样?
在SQL中它会是一些连接,类似于
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
}
我想这样做是因为优先级矩阵可以由不同的设备更新和共享,因此在获取事件时必须始终获得优先级值,但我想在加载事件时加载优先级值。 / p>
答案 0 :(得分:0)
您可以拥有临时对象,但它不会对您的数据库生效,例如:在生成的sql中。
//Event class:
@Transient
private Priority priority;
//setter
public void setPriority(Priority p){
//if you need to change/update the persistent object when setting a Priority,
//you could set the mapped fields in this setter
this.device.setImpact(p.getId().getImpact());
this.setServerity(p.getId().getSeverity());
}
//getter
public Priority getPriority(){
//since annotated by Transient annotation,this will NOT return persistent object.
// but you could get the transient Priority object and then load it in future
Priority p = new Priority();
//create a PriorityId object
priorityId.set(..this.device..this.severity..)
p.setPriorityId(priorityId);
p.setValue(...);
...
return p; //transient object!
}
以上代码不是用IDE编写的,可能有拼写错误,但它显示了这个想法。
答案 1 :(得分:0)
解决方案是直接在Event中保存优先级以解决性能问题。然后,当修改设备影响时,事件优先级会相应更改。