You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by "KARR, DAVID (ATTCINW)" <dk...@att.com> on 2009/12/14 19:11:33 UTC

ordered one-to-many with join-table

I have two entities with a one-to-many association from the first to the
second, and the database uses a join-table to represent this
association.  The join table also has a "SEQUENCE_NUM" column to
represent the required ordering of this element in the collection of
elements.  I understand how to use "join-table" and "join-column" to
describe the basic relationship, but I don't see any way to specify that
the collection should be ordered by a column value in the join table.

RE: ordered one-to-many with join-table

Posted by "KARR, DAVID (ATTCINW)" <dk...@att.com>.
> -----Original Message-----
> From: Craig.Russell@Sun.COM [mailto:Craig.Russell@Sun.COM]
> Sent: Monday, December 14, 2009 10:40 AM
> To: users@openjpa.apache.org
> Subject: Re: ordered one-to-many with join-table
> 
> Hi,
> 
> Take a look at the OrderBy annotation, or the JPA 2.0 OrderColumn
> annotation for the standard way to represent ordered collections.
> 
> Here's the description from 7.6.3 of the OpenJPA user's guide:
> 
> Relational databases do not guarantee that records are returned in
> insertion order. If you want to make sure that your collection
> elements are loaded in the same order they were in when last stored,
> you must declare an order column. An order column can be declared
> using OpenJPA's org.apache.openjpa.persistence.jdbc.OrderColumn
> annotation or the JPA 2.0 javax.persistence.OrderColumn annotation or
> order-column orm element as defined in Section 3, " XML Schema ".
> OpenJPA's org.apache.openjpa.persistence.jdbc.OrderColumn annotation
> has the following properties:
> 
> 	*
> String name: Defaults to the name of the relationship property or
> field of the entity or embeddable class + _ORDER. To use the JPA 1.0
> default order column name ORDR, set the Section 5.7, "
> openjpa.Compatibility " option UseJPA2DefaultOrderColumnName to false.
> 	*
> boolean enabled
> 	*
> int precision
> 	*
> String columnDefinition
> 	*
> boolean insertable
> 	*
> boolean updatable
> Order columns are always in the container table. You can explicitly
> turn off ordering (if you have enabled it by default via your mapping
> defaults) by setting the enabled property to false. All other
> properties correspond exactly to the same-named properties on the
> standard Column annotation, described in Section 3, " Column ".

As 2.0 isn't final yet, I'm only using the 1.2.1 release.  It appears
there is no provision (that I can see) for using provider extensions in
the orm.xml file, only in annotations.  That's unfortunate, because I'm
trying to only use logical annotations, with physical mappings put in
the orm.xml.  I suppose that means I'll likely have to put this one
thing into the Java code.

The other part that I could use more information about is that this
little piece of text you showed here is the entire documentation for
this annotation.  There's no examples that I can see, and this
documentation doesn't say enough, really.  For instance, it says "order
columns are always in the container table".  What specifically does that
mean?  How does it apply to collections that use a join table?

> On Dec 14, 2009, at 10:11 AM, KARR, DAVID (ATTCINW) wrote:
> 
> > I have two entities with a one-to-many association from the first to
> > the
> > second, and the database uses a join-table to represent this
> > association.  The join table also has a "SEQUENCE_NUM" column to
> > represent the required ordering of this element in the collection of
> > elements.  I understand how to use "join-table" and "join-column" to
> > describe the basic relationship, but I don't see any way to specify
> > that
> > the collection should be ordered by a column value in the join
table.
> 
> Craig L Russell
> Architect, Sun Java Enterprise System http://db.apache.org/jdo
> 408 276-5638 mailto:Craig.Russell@sun.com
> P.S. A good JDO? O, Gasp!


Re: ordered one-to-many with join-table

Posted by Craig L Russell <Cr...@Sun.COM>.
Hi,

Take a look at the OrderBy annotation, or the JPA 2.0 OrderColumn  
annotation for the standard way to represent ordered collections.

Here's the description from 7.6.3 of the OpenJPA user's guide:

Relational databases do not guarantee that records are returned in  
insertion order. If you want to make sure that your collection  
elements are loaded in the same order they were in when last stored,  
you must declare an order column. An order column can be declared  
using OpenJPA's org.apache.openjpa.persistence.jdbc.OrderColumn  
annotation or the JPA 2.0 javax.persistence.OrderColumn annotation or  
order-column orm element as defined in Section 3, “ XML Schema ”.  
OpenJPA's org.apache.openjpa.persistence.jdbc.OrderColumn annotation  
has the following properties:

	•
String name: Defaults to the name of the relationship property or  
field of the entity or embeddable class + _ORDER. To use the JPA 1.0  
default order column name ORDR, set the Section 5.7, “  
openjpa.Compatibility ” option UseJPA2DefaultOrderColumnName to false.
	•
boolean enabled
	•
int precision
	•
String columnDefinition
	•
boolean insertable
	•
boolean updatable
Order columns are always in the container table. You can explicitly  
turn off ordering (if you have enabled it by default via your mapping  
defaults) by setting the enabled property to false. All other  
properties correspond exactly to the same-named properties on the  
standard Column annotation, described in Section 3, “ Column ”.

Craig

On Dec 14, 2009, at 10:11 AM, KARR, DAVID (ATTCINW) wrote:

> I have two entities with a one-to-many association from the first to  
> the
> second, and the database uses a join-table to represent this
> association.  The join table also has a "SEQUENCE_NUM" column to
> represent the required ordering of this element in the collection of
> elements.  I understand how to use "join-table" and "join-column" to
> describe the basic relationship, but I don't see any way to specify  
> that
> the collection should be ordered by a column value in the join table.

Craig L Russell
Architect, Sun Java Enterprise System http://db.apache.org/jdo
408 276-5638 mailto:Craig.Russell@sun.com
P.S. A good JDO? O, Gasp!