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

Difference between revisions of "EclipseLink/Examples/OSGi/Developing with EclipseLink OSGi in PDE"

m
m (Create a JPA Entity)
Line 83: Line 83:
 
}
 
}
 
</source>
 
</source>
 +
== Create a persistence.xml ==
 +
* In your source folder, create a META-INF folder and in that folder create a new persistence.xml file.  The persistence.xml below defines a persistence unit called "hello.jpa" with a single Person Entity along with an Apache Derby embedded database connection.  It instructs EclipseLink to drop and create the tables implied by the Entities in the project and it tweaks the logging level and verbosity.  These and other EclipseLink persistence unit properties are documented on the [[Using_EclipseLink_JPA_Extensions_(ELUG)|Using EclipseLink JPA Extensions]] page of the [[EclipseLink/UserManual|EcilpseLink User Guide]].
 +
<source lang="xml">
 +
<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="hello.jpa">
 +
        <class>hello.jpa.Person</class>
 +
        <properties>
 +
            <!-- Embedded Derby Login -->
 +
            <property name="eclipselink.target-database" value="Derby"/>
 +
            <property name="eclipselink.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
 +
            <property name="eclipselink.jdbc.url" value="jdbc:derby:comics;create=true"/>
 +
            <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"/>
 +
 +
            <!-- Database Schema Creation -->
 +
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
 +
            <property name="eclipselink.ddl-generation.output-mode" value="database"/>
 +
 +
            <!-- Logging Settings -->
 +
            <property name="eclipselink.logging.level" value="FINE" />
 +
            <property name="eclipselink.logging.thread" value="false" />
 +
            <property name="eclipselink.logging.session" value="false" />
 +
            <property name="eclipselink.logging.exceptions" value="true" />
 +
            <property name="eclipselink.logging.timestamp" value="false"/>           
 +
        </properties>
 +
    </persistence-unit>
 +
</persistence>
 +
        </source>

Revision as of 13:41, 29 May 2009

Under Construction!

Developing with EclipseLink OSGi in PDE

NOTE: These instructions apply to the Galileo release.

EclipseLink is the the only comprehensive Java persistence framework that is designed for OSGi. EclipseLink is available as a set of OSGi bundles that can be used with any OSGi framework. Support is also available for byte code weaving of Entities when using Equinox.

This example illustrates how to develop a simple OSGi hello database application using Eclipse's Plug-in Development Environment (PDE) and EclipseLink.

Setup

  • Download Eclipse IDE with PDE
    • A number of distributions include PDE including the Java EE, RCP, and Classic distributions.
    • In Galileo, EclipseLink is included in the Eclipse IDE for Java EE Developers distribution. If you download this distribution you do not need to download EclipseLink; the bundles are in the plugins folder of your Eclipse install.


  • Install Required Bundles: EclipseLink JPA and JDBC. For the purposes of this example, we'll use the Apache Derby embedded database because the embedded database driver bundle is available along with EclipseLink from the Galileo update site.
    • Select Help>Install New Software.. and in the Install dialog select the Galileo update site.
    • Select EclipseLink JPA which is in the EclipseRT Target Platform Components category.
      EclipseLinkExamplesOSGiDeveloping with EclipseLink OSGi in PDE-1.png
    • Select Apache Derby Core Feature. You may have to uncheck the "Group items by category" option to see this feature.
      EclipseLinkExamplesOSGiDeveloping with EclipseLink OSGi in PDE-2.png
    • Review the features you're installing to ensure you're getting both EclipseLink JPA and Derby.
      EclipseLinkExamplesOSGiDeveloping with EclipseLink OSGi in PDE-3.png
    • After downloading, Eclipse will prompt you to restart. Restart to complete the install.
    • You can confirm that EclipseLink JPA and Derby are installed using the Help>About Eclipse SDK menu and clicking the Installation Details button on the About dialog.
      EclipseLinkExamplesOSGiDeveloping with EclipseLink OSGi in PDE-4.png

Building a JPA application in OSGi

Now that the environment is setup, you can proceed to building an application.

Create a Plug-in Project

  • Use the New>Project dialog to select a new "Plug-in Project" and call it "hello.jpa". For the target platform select "an OSGi Framework" and "standard" and click Next.
    IMAGE
  • On the Content panel of the New Project wizard set the Execution Environment to "J2SE-1.5" as JPA requires at least Java 5.
  • Select "Generate an Activator.." and accept the default Activator class name "hello.jpa.Activator" and click Finish.
    IMAGE

Create a JPA Entity

  • Create a new "hello.jpa.Person" class as illustrated below.
package hello.jpa;
 
public class Person {
	private int id;
	private String name;
 
	public int getId() {
		return id;
	}
 
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}
  • We can make Person a JPA Entity by adding few annotations but first we need to adjust the hello.jpa bundle manifest to import JPA's javax.persistence packages. Open the Manifest.mf and go to the Dependencies tab and add imported package "javax.persistence".
    IMAGE
  • Now we can add the JPA annotations we need to our Person.
package hello.jpa;
 
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
 
@Entity
public class Person {
	@Id
	@GeneratedValue
	private int id;
	private String name;
 
	public int getId() {
		return id;
	}
 
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

Create a persistence.xml

  • In your source folder, create a META-INF folder and in that folder create a new persistence.xml file. The persistence.xml below defines a persistence unit called "hello.jpa" with a single Person Entity along with an Apache Derby embedded database connection. It instructs EclipseLink to drop and create the tables implied by the Entities in the project and it tweaks the logging level and verbosity. These and other EclipseLink persistence unit properties are documented on the Using EclipseLink JPA Extensions page of the EcilpseLink User Guide.
<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="hello.jpa">
        <class>hello.jpa.Person</class>
        <properties>
            <!-- Embedded Derby Login -->
            <property name="eclipselink.target-database" value="Derby"/>
            <property name="eclipselink.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
            <property name="eclipselink.jdbc.url" value="jdbc:derby:comics;create=true"/>
            <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"/>
 
            <!-- Database Schema Creation -->
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
            <property name="eclipselink.ddl-generation.output-mode" value="database"/>
 
            <!-- Logging Settings -->
            <property name="eclipselink.logging.level" value="FINE" />
            <property name="eclipselink.logging.thread" value="false" />
            <property name="eclipselink.logging.session" value="false" />
            <property name="eclipselink.logging.exceptions" value="true" />
            <property name="eclipselink.logging.timestamp" value="false"/>            
        </properties>
    </persistence-unit>
</persistence>

Back to the top