Ubuntu JPA With Eclipse

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):

cd ~/glassfish3/
javadb/bin/startNetworkServer

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.

13. 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.

18.
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:
http://adterrasperaspera.com/blog/2009/08/11/eclipselink-jpa-in-eclipse-dumb-error-message

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 🙂

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. Bookmark the permalink.

8 Responses to Ubuntu JPA With Eclipse

  1. I think this is among the such a lot significant info for me. And i am happy reading your article. However wanna statement on some basic things, The web site style is perfect, the articles is in point of fact excellent : D. Excellent job, cheers

  2. Pingback: Installing and running Eclipse, Glassfish and Ubuntu 12.04 Precise for Web Applications | hzhwine

  3. Anonymous says:

    Where is 24?

  4. anthonykitmcinnis says:

    Here is my proof reading so far. I encountered an error at 12 as soon as I pressed the download icon.
    I never saw a list where I could select EclipseLink 2.4 for Juno.

    11a. Enter Employee for the name. Target runtime: none. Configuration: Default configuration. JPA version 2.0. Next.

    11b. Highlight src folder in build path. Next.

    12a. Platform: EclipseLink 2.4.x. Type: User library. (Now click the icon for) Download library. EclipseLink 2.4 for Juno. Start the download.

    12b. Before beginning the download makes sure connection is New Derby. Click Add driver library to build path. Driver is GlassFishSampleDB.

    I received the following error box:

    Unhandled event loop exception
    No more handles [Unknown Mozilla path (MOZILLA_FIVE_HOME not set)]

  5. anthonykitmcinnis says:

    Hi, In this tutorial I am not able to perform Step 3. Can you help me with this please.

    I just completed your previous tutorial:
    Installing and running Eclipse, Glassfish and Ubuntu 12.04 Precise for Web Applications
    Everything worked perfectly.

    Here is Step 3.
    3. In Derby’s bin folder, invoke startNetworkServer by doing this:
    cd ~/glassfish3/
    bin/startNetworkServer

    Below are the steps I ran and the messages:

    mcinnis@ubuntu:~$ cd ~/glassfish3/
    mcinnis@ubuntu:~/glassfish3$ bin/startNetworkServer
    bash: bin/startNetworkServer: No such file or directory
    mcinnis@ubuntu:~/glassfish3$ ls -ltr
    total 5
    drwxr-xr-x 10 mcinnis mcinnis 1024 Jul 11 14:19 glassfish
    drwxr-xr-x 4 mcinnis mcinnis 1024 Jul 11 14:25 pkg
    drwxr-xr-x 2 mcinnis mcinnis 1024 Jul 11 14:25 bin
    drwxr-xr-x 4 mcinnis mcinnis 1024 Jul 11 14:38 javadb
    drwxr-xr-x 5 mcinnis mcinnis 1024 Jul 11 14:42 mq

    • anthonykitmcinnis says:

      Here are the contents of bin:

      mcinnis@ubuntu:~/glassfish3/bin$ pwd
      /home/mcinnis/glassfish3/bin
      mcinnis@ubuntu:~/glassfish3/bin$ ls -ltr
      total 42
      -rwxr-xr-x 1 mcinnis mcinnis 2536 Jul 11 14:25 asadmin.bat
      -rwxr-xr-x 1 mcinnis mcinnis 2262 Jul 11 14:25 asadmin
      -rwxr-xr-x 1 mcinnis mcinnis 9977 Jul 11 14:25 updatetool.bat
      -rwxr-xr-x 1 mcinnis mcinnis 8123 Jul 11 14:25 updatetool
      -rwxr-xr-x 1 mcinnis mcinnis 9977 Jul 11 14:25 pkg.bat
      -rwxr-xr-x 1 mcinnis mcinnis 8123 Jul 11 14:25 pkg

      • anthonykitmcinnis says:

        Sorry, I left the comment above with noticing you had already updated the tutorial. Thanks a million.

  6. Pingback: Installing and running Eclipse, Glassfish and Ubuntu 12.04 Precise for Web Applications | Connected Web

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