openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andy Schlaikjer <ha...@cs.cmu.edu>
Subject Re: How to annotate a positional List
Date Wed, 08 Oct 2008 16:09:34 GMT
Hi Kevin,

 From your example table schema, it looks as though you would like to be 
able to reference the same page from more than one book. I had a similar 
problem ages ago, but found that it wasn't possible to do this easily 
with JPA because there is no way to have the @OrderBy annotation target 
a column within a @JoinTable. The column which @OrderBy points to must 
be present in the target entity's table, which can't be the case if the 
intended relation is a true many-to-many. My prior solution was to 
pollute my domain model with an extra "join" entity class which would 
map to the desired join table directly (including the sequence column 
explicitly).

However, with OpenJPA's @OrderColumn extension, I believe this is now 
possible:

@Entity
public class Book {
   @Id
   private long id;

   @PersistentCollection
   @OrderColumn
   private List<Page> pages;
}

@Entity
public class Page {
   @Id
   private long id;

   @Lob
   private string data;
}

The generated DDL using OpenJPA 1.2.0 and MySQL DBDictionary is:

CREATE TABLE Book (id BIGINT NOT NULL, PRIMARY KEY (id))
CREATE TABLE Book_Page (BOOK_ID BIGINT, PAGES_ID BIGINT, ordr INTEGER)
CREATE TABLE Page (id BIGINT NOT NULL, data TEXT, PRIMARY KEY (id))

Cheers,
Andy

Kevin Cox wrote:
> Hi,
> I have been searching the manual, examples, and mail lists for an
> implementation of a List, where the items in the list are positional stable
> after the commit.  I¹ve had no luck and I am now wondering if it exists.  I
> imagine it would need to otherwise nobody could properly store an ArrayList
> using OpenJPA!  (should be basic stuff) Hibernate does this using
> <list-index column="position"/>
> 
> Here¹s the problem.  Let¹s say I have a contrived Book object which has a
> List (ArrayList) of pages (Page objects).  The pages in the List need to
> maintain their order. Page 1,2,3,4 etc.  The order is maintained by using a
> position or sequence number which is used to sort the list.
> 
> The db scheme would be something like
> 
> Create table book (
>     book_id number,
>     title varchar
> )
> 
> Create table page (
>     page_id number,
>     page_data clob
> )
> 
> And here¹s the link table
> 
> Create table book_page (
>     book_id number,
>     page_id number,
>     sequence number  <<<<--- the position or sequence number 1,2,3,4,5,etc
> ) -- all not null
> 
> The Collection annotations for List need to automatically insert and update
> the sequence numbers to make the ArrayList maintain stable order upon
> commits.  The sequence needs to be incremented and decremented for List adds
> and removes and renumbered for mid-list insertions and removes.
> 
> Does anyone know how to implement this in OpenJPA?  Is it supported?  It¹s
> really just maps a java List to a db schema, keeping the list items order
> stable during persistence.
> 
> Thanks,
> Kevin
> 


Mime
View raw message