This tutorial will create an object called Employee, save it to a database using JPA and Eclipse.
It requires downloads as mentioned in steps 1 and 2, and another download while creating the project, in step 16.
1. Download Eclipse for Java EE Developers” version: It’s the big one: 200+ MB: third one on the list (it’s not the regular Java Developer version): Eclipse IDE for Java EE Developers, 206 MB
2. Download Apache Derby:
3. Unzip them both in the usual way. In Derby’s bin folder, invoke startNetworkServer. This will start up the database server, and it will continue to run; let it.
4. Start up Eclipse by invoking the eclipse executable.
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. On the Specify a Driver and Connection Details dialog, look right beside “Drivers”, there’s something that looks like a * or a +, click it.
10. Select the “Derby Client JDBC Driver” (the highest version that’s there) and navigate to the derby folder, and lib, and select derbyclient.jar
11. On the Jar Tab of that dialog, click on the “clear all” button.
12. Navigate to the derby folder, under lib, for derbyclient.jar, and “add jar” and select that.
13. You will note that it gives you some default database information. It gives you a database named “sample”. Don’t worry about 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.
14. File. New. JPA Project.
15. Enter Employee for the name. Target runtime: none. Configuration: Default configuration.
16. Platform: EclipseLink 2.1.x. Type: User library. Download. EclipseLink 2.1. Start the download.
17. Agree to add driver to library build path (ensure there’s an X in that box). Click finish.
18. File. New. Entity.
19. Java Package: mystuff. class name: Employee. Next
20. On the entity fields, add 3 fields:
type: java.lang.Long name: id (now click on the Key checkbox to ensure an X is on this field only)
type: java.lang.String name: name
type: double name: salary
21. After ensuring that the Long is uppercase, String is uppercase, and double is lowercase, and that id is a “key” field, click finish.
22. 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 META-INF, and then double click on persistence.xml until it shows up in the editor window. I’d suggest this link for ignorable error Schema cannot be resolved for table: http://adterrasperaspera.com/blog/2009/08/11/eclipselink-jpa-in-eclipse-dumb-error-message
23. At the bottom of the editor window, click on “Schema generation” tab.
24. Change DDL generation type: Create Tables
25. 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”. Note that the driver is empty. Oops. Save it for now.
26. We need to add a driver to the project. Yes we already added a driver, and Data Source Explorer uses it, but our project needs to have access to a driver too. Right click on Employee in the Project Explorer (we want to modify the Project Employee). Click properties.
27. Click Java Build Path. Click Libraries tab. Add External Jar. Find the derbyclient.jar for derby (as before).
28. Open up persistence.xml again, redo step 25. (populate from connection). Repeat this procedure 25-28 until a Driver shows up.
29. Right click on Employee, add a new folder called lib. in that folder, add the jar (yet again). once it’s there, right click on derbyclient.jar. Click build path. Add it to the build path.