We’ll use Derby, already installed with Glassfish, and show you how to use EclipseLink to have Eclipse automatically create your tables. This tutorial starts where my previous tutorial on Glassfish installation ended.
This tutorial will create an object called Employee, save it to a database using JPA and Eclipse.
It requires a download while creating the project, in step 12.
1. Assume you have installed “Eclipse for Java EE Developers” version: It’s the big one: 200+ MB
2. No need to install Derby, because it was already installed with Glassfish.
3. In Derby’s bin folder, invoke startNetworkServer by doing this (thanks Anthony for the proof-read):
It should now be saying it is accepting connections. This will start up the database server, and it will continue to run; let it (Control-C is a proper way to shut it down if you ever need to).
4. Start up Eclipse.
5. Window/Open Perspective/ JPA.
6. Window, Show View. Other. Data Management. Data Source Explorer.
7. In the Data Source Explorer window, Right click on Database Connections.
8. New. Derby. Next.
9. You will note that it gives you some default database information. It gives you a database named “sample”. Don’t change the username and password, because Derby will accept any username and password by default… it’s good that way. Click finish to complete the connection to the database.
10. File. New. JPA Project.
11. Enter Employee for the name. Target runtime: none. Configuration: Default configuration. Next/ Next.
12. Platform: EclipseLink 2.4.x. Type: User library. Just under “User library”, and to the right of the white area are two tiny little icons. (Now click the icon for) Download library. EclipseLink 2.4.1 for Juno. Start the download.
Select the connection. Mine said “New Derby”. Agree to add driver to library build path (ensure there’s an X in that box). Click Finish.
14. File. New. JPA Entity.
15. Java Package: mystuff. class name: Employee. Next
16. On the entity fields, (click Add) add 3 fields:
type: java.lang.Long // name: id // OK // (now click on the Key checkbox to ensure an X is on this field only)
(Add) // type: java.lang.String // name: name
(Add) // type: double // name: salary
17. After ensuring that the Long is uppercase, String is uppercase, and double is lowercase, and that id is a “key” field, (be prepared for upcoming errors and then) click finish.
It should now show an error about the schema USER not agreeing with the table Employee. That’s OK. It doesn’t know that we are going to have it create the table automatically too.
In Project Explorer window, expand your project (Employee) , expand JPA content// and then double click on persistence.xml until it shows up in the editor window.
19. At the bottom of the editor window (this is a little hard to explain, but the [center editor] window which has two tabs at the top actually has about 6 at the bottom of it) , click on “Schema generation” tab.
20. Change DDL generation type: Create Tables
21. On the “Connection” tab, select Transaction type: Resource Local. Scroll down on this editor window, and in the “EclipseLink connection pool” section, click on “populate from connection”. If it asks which connection, select the one that you made when you created your first Derby database. It probably says “New Derby”.
22. Save the file persistence.xml that you just edited.
23. If you are not getting errors, go to 24. (If you are getting errors…) Please look at this link , and do what it says:
If you ever run a derby project and it chokes with errors, also try this:
Project/Properties/Libraries/Add external jar (and add derbyclient.jar which should be in ~/glassfish3/javadb/lib
If ya like this article, pass around the luv and say thx 🙂