-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: ManyToOne join column from other assocations
PostPosted: Fri Nov 25, 2011 11:41 am 
Newbie

Joined: Thu Nov 24, 2011 1:47 pm
Posts: 3
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;
}



Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.