Return-Path: Delivered-To: apmail-openjpa-users-archive@locus.apache.org Received: (qmail 23642 invoked from network); 15 Jan 2008 00:49:31 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 15 Jan 2008 00:49:31 -0000 Received: (qmail 27177 invoked by uid 500); 15 Jan 2008 00:49:20 -0000 Delivered-To: apmail-openjpa-users-archive@openjpa.apache.org Received: (qmail 27164 invoked by uid 500); 15 Jan 2008 00:49:20 -0000 Mailing-List: contact users-help@openjpa.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@openjpa.apache.org Delivered-To: mailing list users@openjpa.apache.org Received: (qmail 27155 invoked by uid 99); 15 Jan 2008 00:49:20 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 14 Jan 2008 16:49:20 -0800 X-ASF-Spam-Status: No, hits=-0.0 required=10.0 tests=SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: local policy) Received: from [128.2.185.41] (HELO chokecherry.srv.cs.cmu.edu) (128.2.185.41) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 15 Jan 2008 00:49:08 +0000 Received: from [128.2.180.53] (SEWARD.LTI.CS.CMU.EDU [128.2.180.53]) (authenticated bits=0) by chokecherry.srv.cs.cmu.edu (8.13.6/8.13.6) with ESMTP id m0F0mx2Y011903 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Mon, 14 Jan 2008 19:48:59 -0500 (EST) Message-ID: <478C02FA.7000901@cs.cmu.edu> Date: Mon, 14 Jan 2008 19:48:58 -0500 From: Andy Schlaikjer User-Agent: Thunderbird 1.5.0.4 (X11/20060614) MIME-Version: 1.0 To: users@openjpa.apache.org Subject: question on orderby and manytomany Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Checked: Checked by ClamAV on apache.org Hi all, I've got two classes A and B which have a bidirectional many-to-many relationship. However, references to B's from A's must be ordered while references to A's from B's should be unordered. Is there a clean way to represent this accurately with JPA? I think what I'm looking for is allowing @OrderBy to reference a special index column within the join table itself.. if that makes sense. Here's an outline: @Entity public class A { ... @ManyToMany @OrderBy(???) protected List bs; ... } @Entity public class B { ... @ManyToMany(mappedBy="bs") protected Collection as; ... } I can't use an explicit index field within B because different A's might reference the same B's, but require different ordering of those B's with their respective A.bs fields. Here's an alternative: @Entity public class A { ... @OneToMany(mappedBy="a") @OrderBy("index") protected List brefs; ... } @Entity public class BRef { ... @ManyToOne protected A a; @ManyToOne protected B b; @Basic protected int index; ... } @Entity public class B { ... @ManyToMany protected Collection as; ... } But here we see that the declaration of B.as will result in a separate join table "B_A". Can we instead reuse the table supporting BRef?: @Entity public class B { ... @ManyToMany() @JoinTable( name="BRef", joinColumns=@JoinColumn(name="b_id"), inverseJoinColumns=@JoinColumn(name="a_id") ) protected Collection as; ... } Okay, but this still means that we have an explicit level of indirection from A's to B's through BRef's. I'd rather the A to B relation be taken care of transparently-- I want to be able to reference the BRef.index field somehow to constrain the ordering of A.bs, perhaps like this: @Entity public class A { ... @ManyToMany() @JoinTable( name="BRef", joinColumns=@JoinColumn(name="a_id"), inverseJoinColumns=@JoinColumn(name="b_id"), orderBy="index" ) protected List bs; ... } @Entity public class BRef { ... @ManyToOne protected A a; @ManyToOne protected B b; @Basic protected int index; ... } @Entity public class B { ... @ManyToMany(mappedBy="bs") protected Collection as; ... } Thoughts? Andy