Hello everyone. First question here. I want to make a mapping that look like this:
Code:
@Entity
public class Event {
@Id
private Long id;
@ManyToOne
private Device device;
@Column
private Impact impact;
@Column
private Severity severity;
// THE PROBLEM
@ManyToOne
@JoinColumns({
@JoinColumn(updatable = false, insertable = false,
table = TableName.DEVICE, referencedColumnName = Device.PROPERTY_IMPACT_NATIVE_QUERY),
@JoinColumn(updatable = false, insertable = false,
table = TableName.DEVICE, referencedColumnName = Device.PROPERTY_PRIORITY_MATRIX_NATIVE_QUERY),
@JoinColumn(updatable = false, insertable = false, referencedColumnName = "severity"),
}
)
private Priority priority;
... 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 ...
}
As you can see, I don't want priority keys to be saved in the Event Table, but I want to use the values from Device (Impact and Priority Matrix) and the severity from event to obtain the priority current value. Is there a proper way to do this with annotations? What am I doing wrong?
To help understand my question, I want to eliminate the necessity to use this SQL Query.
Code:
String EVENT_PRIORITY_QUERY =
"SELECT event.*, priority.* \n"
+ "FROM event \n"
+ " INNER JOIN device \n"
+ " ON ( event.device_id = device.id ) \n"
+ " INNER JOIN trap_value \n"
+ " ON ( event.event_trap_oid = trap_value.oid \n"
+ " AND event.event_trap_value = trap_value.value ) \n"
+ " INNER JOIN priority "
+ " ON ( priority.prioritymatrix_id = device.prioritymatrix_id \n"
+ " AND priority.impact = device.impact \n"
+ " AND priority.severity = trap_value.severity ) \n"
+ "WHERE event.id = :eventId \n";
public Event read(Long eventId) {
SQLQuery query = getEventQuery();
query.setLong("eventId", eventId.longValue()); //$NON-NLS-1$
return extractEvent(query);
}
private Event getEventQuery() {
SQLQuery query = createSQLQuery(EVENT_PRIORITY_QUERY);
query.addEntity(Event.class);
query.addEntity(Priority.class);
return query;
}
private Event extractEvent(query) {
Object[] eventAndPriorityResult = (Object[]) query.uniqueResult();
if (eventAndPriorityResult != null) {
Event evt = (Event) eventAndPriorityResult[0];
Priority priority = (Priority) eventAndPriorityResult[1];
evt.setPriority(priority);
return evt;
}
return null;
}