Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: for the plan.

Jump to: navigation, search


< EclipseLink‎ | Examples‎ | JPA
Revision as of 13:06, 23 November 2010 by (Talk | contribs) (@Deprecated)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


The information below is out of date and will be removed

Please see the more recent EclipseLink RCP demo.


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

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 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) { = id; = name;
	public String getName() {
		return name;
	public void setName(String 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"
	<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="" 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;
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 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.

Back to the top