HomeEMF HibernateEMF JDO/JPOXWeb App GenerationServices
 

Step 3: Retrieve and add EMF Objects

In this step the library is retrieved from the backend and a few checks are done on the retrieved EMF objects. In addition a new book and writer are added to the library.

Retrieve Library EMF Object

In this part of the tutorial the library object is retrieved.

// at this point the database contains one library, one book and one writer
// Reopen the transaction and query for the library objects
pm = jpoxDataStore.getPersistenceManager();
tx = pm.currentTransaction(); 
tx.begin();
	
// NOTE: for querying you require the concrete implementation instead of the
// interface!
// retrieve all LibraryImpl classes and subclasses.
Extent e = pm.getExtent(LibraryImpl.class, true);
Query q = pm.newQuery(e);

// there is only one library
Collection c = (Collection)q.execute();
lib = (Library)c.iterator().next();

Do some checks

The library object has been retrieved now a few checks are done to ensure that the correct information was retrieved.

// read the writer and book
writer = (Writer)lib.getWriters().get(0);
System.out.println(writer.getName());
book = (Book)lib.getBooks().get(0);
System.out.println(book.getTitle());

// show that the container is set
System.out.println(book.eContainer() == lib);
System.out.println(writer.getBooks().get(0) == book);
	

Add a new book and writer

Here a new book and writer are added to the library.

// Now add a new writer and book
Writer george = LibraryFactory.eINSTANCE.createWriter();
george.setName("G. Orwell");

// create a new book and set the writer and library
Book georgesBook = LibraryFactory.eINSTANCE.createBook();
georgesBook.setPages(250);
georgesBook.setTitle("1984");
georgesBook.setCategory(BookCategory.SCIENCE_FICTION);
georgesBook.setAuthor(george);

lib.getBooks().add(georgesBook);
lib.getWriters().add(george);

// and close of
tx.commit();
pm.close();
	

Next Step

The final step in the tutorial shows some JDO queries to retrieve EMF objects from the library.

Please click here to go to the next step.