Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

EclipseLink/Examples/JPA/RCP

@Deprecated

The information below is out of date and will be removed

Please see the more recent EclipseLink RCP demo.




Example

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.

Setup

  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;
 
@Entity
public class Publisher implements Serializable {
	private static final long serialVersionUID = -562987599581461681L;
 
	@Id
	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() + ")";
	}
}

Persistence.xml

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"
	xmlns="http://java.sun.com/xml/ns/persistence"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
	<persistence-unit name="comics">
		<class>eclipselinkrcp.Publisher</class>
		<properties>
			<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" />
		</properties>
 
	</persistence-unit>
</persistence>

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;
 
	@SuppressWarnings("unchecked")
	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) {
			System.out.println(publisher.getName());
		}
		em.close();
		return EXIT_OK;
	}
 
	public void stop() {
		emf.close();
	}
}

Summary

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