简体   繁体   中英

Can Entity attribute be mapped by other attributes from same entity?

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.

you can have transient object, but it won't take effect to your database, eg in generated 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!
}

above codes are not written in IDE, may have typo, but it shows the idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM