JPA Eclipse Tutorial using EclipseLink

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:
db-derby-10.6.2.1-bin.zip

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.

Advertisements

About SunKing2

Former web developer, guitar player, started Piano 2012 -yes still doing it! Recovery, sobriety, mental health advocate.
This entry was posted in Uncategorized and tagged , , , , . Bookmark the permalink.

4 Responses to JPA Eclipse Tutorial using EclipseLink

  1. uzerzero says:

    Serious kudos man! I love you style of explanation, really clear easy to follw, worked first shot.
    Thanks a mil, and absolutley following your blog!

  2. Arindam says:

    Hi,

    I have attached Derby now with my project.
    Now how do I actually create these tables ?

    Arindam.

    • Anonymous says:

      With Derby, the tables get created automatically. It sounds unbelievable, but it’s true. It gleans enough information about data types, table names, etc from the .xml file and from the field names.

  3. Arindam says:

    Hi,

    Thanks for your nice post.
    I have done all the steps you mentioned, but with a MySQL, instead of Derby.
    My persistence.xml looks fine, but I am not able to create the DDL from this. I dont know what’s wrong.
    In the Data Source Explorer, I am able to see the existing tables in the database. But how do I create the tables from my enitity classes ?

    Thanks,
    Arindam.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s