Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[eclipselink-users] JPA 2.0 question: derived @ManyToOne?

Does JPA (and EclipseLink, as its reference implementation) support...derived many-to-one relationships?

I have a Response.  It is associated with a Question.  The Question is associated with a NamedAnswerSet, which has a basic "name" identifier.

public class Response {
  @ManyToOne
  private Question question;
}

public class Question {
  @ManyToOne
  private NamedAnswerSet;
}

public class NamedAnswerSet {
  @Id
  private String name;
}

I want to add a @ManyToOne in the Response with an Answer entity, whose primary key is composite: one part is a basic int id, and the other part is the NamedAnswerSet "name" identifier.

public class Response {
  @ManyToOne
  private Question question;

  @ManyToOne
  private Answer answer; // XXX this involves a "double" join; my problem
}

@IdClass(...)
public class Answer {
  @Id
  private int id;

  @Id
  private String answerSetName;
}

I don't know how to annotate the Response's relationship with the Answer such that the following is true:

The portion of Answer's primary key that is the int id field is referred to via a foreign key join from Response's answerId column
The portion of Answer's primary key that is the name field is referred to via a foreign key join from Question's NamedAnswerSet's name column

So, in pseudocode, I want:

  @ManyToOne
  // join columns here should be:
  //   this table's answer_id
  //   namedAnswerSet.name ("reachable" via this.question.namedAnswerSet.name)
  private Answer answer;

I hope I'm being reasonably clear.  My suspicion is that such a "derived" composite join like this is not really supported by JPA 2.0, but I thought I'd ask.  I understand that there are several JPA 2.0 compliant workarounds, but all introduce a duplicate column somewhere; I'm trying to avoid that.  For example, I could introduce an insertable/updatable=false "answerSetName" column in Response and run the join off that, but, again, I'm trying to go with columns I already have.

Thanks as always for an excellent product.

Best,
Laird

Back to the top