Simple JPA Project in Eclipse with GlassFish
This document covers creation of a simple JPA project using GlassFish v3 Prelude and its built in EclipseLink implementation. If you have the GlassFish for Eclipse bundle, skip to Configure Preferences
Prerequisites If you have the GlassFish for Eclipse bundle, skip this step
Configure Preferences
Start the GlassFish Server
- In the Servers View, right-click on the new GlassFish v3 Prelude entry and choose Start. This will Start the GlassFish server and the database as well.
Configure DataSource If you have the GlassFish for Eclipse bundle, skip this step
- Register the preconfigured GlasFish v3 Prelude DerbyPool as a Data Source for use within Eclipse. Details available at https://glassfishplugins.java.net/eclipse34/index.html#12.
- Be sure your jdbc connection info in the wizard contains these values:
-
- URL:* jdbc:derby://localhost:1527/sun-appserv-samples;create=true
- Database Name:* sun-appserv-samples
- Password:* APP
- User ID:* APP
Create a New JPA Project
- From the Main Menu, choose File->New->Other...
- In the popup wizard, choose JPA->JPA Project
- Choose a name and target the GlassFish v3 Prelude Server

- On Page 2 in the wizard
- Choose EclipseLink as the Platform and the DB connection you defined above
- Use JPA implementation provided by the server runtime (you can ignore the warning about no EJB 3)
- Leave the default of Annotated classes must be listed in persistence.xml

- Finish the wizard - you can say yes to the question of whether it is ok to switch to the JPA perspective
Create a New Entity Class
- From the Main Menu, choose File->New->Other...
- In the popup wizard, choose JPA->Entity
- Enter a name and package name
- On page 2, add 2 fields, one of which is a key field

- Finish the wizard
Generate DDL
- Right click on project node, choose JPA Tools->Generate DDL...
- Say ok to the warning that you are about to create new tables
Verify Creation of New Table
- Expand the node in the data source explorer and see the new table

Troubleshooting
- Make sure your GlassFish server is running. If it is not, you will see lots of exceptions in the generation process, but it may not be apparent that the failure is due to the fact that the server is not running.
- Make sure your JavaDB database server is running. If it is not, you will see lots of connection exceptions or password exceptions, but you may not realize that it is because the server is not running.
- Specification of classes in the persistence.xml is required. If they are not specified there, generation will go through without errors, but no tables will be generated. (EclipseLink team is looking at improving this)
- Specification of other generation properties in persistence.xml is mostly ignored when doing generation in this way (From EclipseLink team - Once we have a wizard for DDL Generation, this can be made more clear. Text could also be added to the Persistence XML Editor Schema Generation tab.)
- Populating the provider in persistence.xml with the EclipseLink values when a user chooses that platform in Eclipse does not happen today (EclipseLink team will work on improving this)
- As of today, there is no comparable integration for TopLink Essentials with these generation tools as there is only the Generic Platform and the EclipseLink Platform
- As of today, the EclipseLink jars must be provided by the user (or in our case, by the GlassFish server). Even though EclipseLink Platform is built into Eclipse, the runtime for that platform must be supplied separately. (From EclipseLink team: We looked at doing this in our upcoming 2.1 release, but it turned out to be a non-trivial effort, so will probably be done for our next major release in June.)
|