2012-03-16 38 views
1

我有一个实体结构,看起来像这样:实体属性可以被来自同一实体的其他属性映射吗?

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

我想这样做,因为优先矩阵可以更新,并通过不同的设备共享,因此必须得到时可以始终得到优先级值事件,但我想在加载事件的那一刻加载优先级值。

回答

0

解决方案是直接在Event上保存优先级以解决性能问题。然后,在修改设备影响时,事件优先级会相应更改。

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中,可能有错字,但它给出了这个概念。

+0

是的,已经使用过类似的东西,但我在数据库中有数千个事件。使用类似的东西的表现会非常糟糕:/ – 2012-03-16 15:43:04

相关问题