For the persistence.xml - no special settings, just a persistence-unit and a
list of classes.  For the Persistence object, I just have normal user and
password with a db url plus the following:
p.put("openjpa.jdbc.SynchronizeMappings","buildSchema");
p.put("openjpa.Log","DefaultLevel=WARN,SQL=TRACE"); //Runtime=TRACE
Nothing else...

Conditions:
DB is Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit
Production, using driver named ojdbc14.jar
OpenJPA is 1.2.0
Running through IntelliJ IDEA 8.0M1 set to java 1.6
On a Mac Pro Intel OS X 10.5.5

If I missed something important let me know.  I'm happy to help and happy
you are helping me ;)

file: Book.java
--------------------------------------------------------------------------
package com.play.cmsdata;

import org.apache.openjpa.persistence.jdbc.OrderColumn;
import org.apache.openjpa.persistence.PersistentCollection;

import javax.persistence.*;
import java.util.List;
import java.util.ArrayList;

@Entity
@SequenceGenerator(name="BookSeq", sequenceName="BOOK_SEQ")
public class Book {
   @Id @GeneratedValue(strategy= GenerationType.TABLE, generator="BookSeq")
   private long id;

   String title;

   @PersistentCollection(elementCascade=CascadeType.ALL)
   @OrderColumn
   private List<PageOfBook> pages = new ArrayList<PageOfBook>();

    public Book() {
    }

    public Book(long id) {
        this.id = id;
    }

    public Book(String title) {
        this.title = title;
    }

    public long getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public List<PageOfBook> getPages() {
        return pages;
    }

    public void setPages(List<PageOfBook> pages) {
        this.pages = pages;
    }
}



file: Book.java
--------------------------------------------------------------------------
package com.play.cmsdata;

import javax.persistence.*;
import javax.persistence.Lob;
import javax.persistence.GeneratedValue;


@Entity
@SequenceGenerator(name="PageOfBookSeq", sequenceName="PAGEOFBOOK_SEQ")
public class PageOfBook {
   @Id @GeneratedValue(strategy= GenerationType.TABLE,
generator="PageOfBookSeq")
   private long id;

   @Lob
   private String data;

    public PageOfBook() {
    }

    public long getId() {
        return id;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }
}



--------------------------------------------------------------------------


I use the following to manipulate the data.  I play with the comments in
removePage() to do the removes and remove/inserts.  Ya crude, I know.

private static void createBook() {
        EntityManager em =MyPersistence.getInstance().createEntityManager();
        em.getTransaction().begin();

        PageOfBook p1 = new PageOfBook();
        p1.setData("This is a page 1 in a book");

        PageOfBook p2 = new PageOfBook();
        p2.setData("This is a page 2 in a book");

        Book b = new Book("My Book Example for Lists");
        b.getPages().add(p1);
        b.getPages().add(p2);

        em.persist(b);
        em.getTransaction().commit();


        em.getTransaction().begin();
        Book book = em.find(Book.class, 1L);

        PageOfBook p3 = new PageOfBook();
        p3.setData("This is a page 3 in a book");
        PageOfBook p4 = new PageOfBook();
        p4.setData("This is a page 4 in a book");
        book.getPages().add(p3);
        book.getPages().add(p4);
        em.getTransaction().commit();

//another tx just because
        em.getTransaction().begin();
        book = em.find(Book.class, 1L);

        PageOfBook p5 = new PageOfBook();
        p5.setData("This is a page 5 in a book");
        book.getPages().add(p5);

        em.getTransaction().commit();
        em.close();
    }
    private static void removePage() {
        EntityManager em =MyPersistence.getInstance().createEntityManager();
        em.getTransaction().begin();

        Book book = em.find(Book.class, 1L);
//uncomment the parts you want to run
        //book.getPages().remove(2); //which is item 3 in the array
        
        //PageOfBook p7 = new PageOfBook();
        //p7.setData("This is a page 7 in a book");
        //book.getPages().add(2,p7);

        em.getTransaction().commit();
        em.close();
    }

Reply via email to