Skip to main content

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

JPA does not support this, but you could map this in EclipseLink using a
DescriptorCustomizer.

I would recommend that you instead make the id on Answer a unique id
(perhaps generated), then you will not have this issue.

If you don't wish to change your model, then with EclipseLink in a
DescriptorCustomizer you can get the mapping from the ClassDescriptor and
set the selectionSQLString, you will need to use a sub-select or join in the
SQL.

i.e. something like,
public class ResponseCustomizer implements DescriptorCustomizer {
  public void customize(ClassDescriptor descriptor) {
    OneToOneMapping mapping = (OneToOneMapping )
descriptor.getMappingForAttributeName("answer");
    mapping.setSelectionSQLString("Select a.* from ANSWER a where a.ID =
#ANSWER_ID and a.NAME = (Select q.ANSWER_SET_NAME from QUESTION q where q.ID
= #QUESTION_ID)
  }
}


ljnelson wrote:
> 
> 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
> 
> 
> 


-----
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland 
http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink 
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink , 
http://wiki.oracle.com/page/TopLink TopLink 
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , 
http://www.nabble.com/EclipseLink-f26430.html EclipseLink 
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence 
Blog:  http://java-persistence-performance.blogspot.com/ Java Persistence
Performance 
-- 
View this message in context: http://old.nabble.com/JPA-2.0-question%3A-derived-%40ManyToOne--tp30181789p30238346.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top