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 (Create a JPA Entity)
m
 
(23 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Under Construction!=
+
The OSGi support provided by EclipseLink is deprecated and has been replaced by the Gemini JPA project.  See the [[Gemini/JPA/Documentation|Gemini documentation]] for examples.
 
+
= Developing with EclipseLink OSGi in PDE =
+
 
+
<pre>
+
NOTE: These instructions apply to the Galileo release.
+
</pre>
+
 
+
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 [[Examples/OSGi/Equinox_Byte_Code_Weaving|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 [http://www.eclipse.org/galileo 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.<br/>[[Image: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.<br/>[[Image:EclipseLinkExamplesOSGiDeveloping_with_EclipseLink_OSGi_in_PDE-2.png]]
+
** Review the features you're installing to ensure you're getting both EclipseLink JPA and Derby.<br/>[[Image: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.<br/>[[Image: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.<br/>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.<br/>IMAGE
+
=== Create a JPA Entity ===
+
* Create a new "hello.jpa.Person" class as illustrated below.
+
<source lang="java">
+
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;
+
}
+
}
+
</source>
+
* 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".<br/>IMAGE
+
* Now we can add the JPA annotations we need to our Person.
+
<source lang="java">
+
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;
+
}
+
}
+
</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 verbosityThese 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>
+

Latest revision as of 13:45, 24 October 2012

The OSGi support provided by EclipseLink is deprecated and has been replaced by the Gemini JPA project. See the Gemini documentation for examples.

Back to the top