Difference between revisions of "EclipseLink/Examples/JPA/RCP"

From Eclipsepedia

Jump to: navigation, search
(Documentation is out of date)
Line 1: Line 1:
=== Documentation is out of date ===
= Documentation is out of date =
Also the examples described in the @Deprecated-Section don't work with the current Release -> completelly useless for EclipseLink-Beinners or Non-JPA-Professionals
Also the examples described in the @Deprecated-Section don't work with the current Release -> completelly useless for EclipseLink-Beinners or Non-JPA-Professionals

Revision as of 16:56, 17 September 2008


Documentation is out of date

Also the examples described in the @Deprecated-Section don't work with the current Release -> completelly useless for EclipseLink-Beinners or Non-JPA-Professionals


There is a new RCP demo. Wiki docs are under construction. For current instructions see:



Here's a simple example of how to use EclipseLink JPA in an Eclipse Rich Client Platform (RCP) application. For simplicity the application will be headless--which really makes you wonder how "rich" this client is. ;-) But for those of you building RCP applications who need JPA Persistence I think this will provide the minimum of what you need to get going. I'll keep it pretty high level on the assumption that the reader is familiar with RCP and PDE and just wants to add in some EclipseLink JPA. We'll use Derby for our database.


  1. Using the New Wizard create a Plugin Project targeted at Eclipse 3.3 or 3.4
  2. Do not generate an Activator, do not contribute to the UI, but say yes to "Would you like to create a rich client application?"
  3. Select the "Headless Hello RCP" template.
  4. Drag and drop the following jars into your project:
    • eclipselink.jar
    • persistence.jar
    • derbyclient.jar (or jdbc driver of your choice)
  5. Add these jars to your runtime classpath in your manifest.mf

An Entity

Add a class to your project you want to persist. I'll add "Publisher" and add the necessary JPA annotations for persistence (all two of them).

package eclipselinkrcp;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
public class Publisher implements Serializable {
	private static final long serialVersionUID = -562987599581461681L;
	private int id;
	private String name;
	public Publisher() {
	public Publisher(int id, String name) {
		this.id = id;
		this.name = name;
	public String getName() {
		return name;
	public void setName(String name) {
		this.name = name;
	public int getId() {
		return id;
	public String toString() {
		return "Publisher(" + getId() + ", " + getName() + ")";


I'll need a META-INF\persistence.xml file that defines my persistence unit and it's login information. The persistence.xml lists my single Entity and contains some EclipseLink specific configuration for connection management and logging.

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
	<persistence-unit name="comics">
			<property name="eclipselink.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
			<property name="eclipselink.jdbc.url" value="jdbc:derby://localhost:1527/sample;create=true"/>
			<property name="eclipselink.jdbc.user" value="app"/>
			<property name="eclipselink.jdbc.password" value="app"/>
			<property name="eclipselink.jdbc.read-connections.min" value="1" />
			<property name="eclipselink.jdbc.write-connections.min" value="1" />
			<property name="eclipselink.jdbc.batch-writing" value="JDBC" />
			<!-- Logging -->
			<property name="eclipselink.logging.level" value="FINE" />
			<property name="eclipselink.logging.timestamp" value="false" />
			<property name="eclipselink.logging.session" value="false" />
			<property name="eclipselink.logging.thread" value="false" />

If you want EclipseLink to create the database schema include the following properties in your persistence.xml:

<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="eclipselink.ddl-generation.output-mode" value="database"/>

The Application

And finally the Application. It implements IApplication and I put the code I want to run into the start(..) method. It looks like most Java SE JPA code it: creates an EntityManagerFactory, obtains an EntityManager, performs a query, closes the EntityManager, and in the stop() method it closes the EntityManagerFactory. In a non-headless (it's a double negative but what else can you call it?) application you'd probably move the management of EntityManagerFactory and Entities somewhere else so as to be able to access them from various views.

package eclipselinkrcp;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
public class Application implements IApplication {
	private EntityManagerFactory emf;
	public Object start(IApplicationContext context) throws Exception {
		emf = Persistence.createEntityManagerFactory("comics");
		EntityManager em = emf.createEntityManager();
		Query query = em.createQuery("Select p from Publisher p order by p.name asc");		
		List<Publisher> publishers = query.getResultList();
		for (Publisher publisher : publishers) {
		return EXIT_OK;
	public void stop() {


There you have it. The very basics. I leave it to the reader to create a run configuration and try out the code. I was able to run the code with only 12 plugins so it's reasonably light.