2010-06-22

Eclipse JPA with Hibernate & HSQLDB. Part 1

In this tutorial I will show you how to create an Eclipse JPA project for a desktop application, avoiding some common pitfalls. You're going to need Eclipse Galileo 3.5.2 for this (the JEE edition) but other versions should work too (the UI may look different). I am also going to use HSQLDB, Java-based database. Note that my application doesn't use Maven.


Step #1: Open the Eclipse JPA project wizard


Open the Eclipse JPA project wizard

The first step is to open the Eclipse JPA project wizard. If it doesn't show up in the menu (as shown in the screenshot above), simply go to File -> New -> Other... and then under JPA there should be a "JPA Project" to select. If it isn't, you probably aren't using the correct version of Eclipse.


Step #2: Project name and runtime configuration


Project name and runtime configuration

Give the project a name (I called mine "Dogs"). I did not select a target runtime because the application is supposed to be a desktop one. I also selected the Default Configuration.


Step #3: Source folders


Source folders

We're going to leave this with the default value.


Step #4: Platform, JPA implementation


Platform, JPA implementation

This part gave me a bit of a headache. As a long time Maven user I could not agree to referencing project libraries from my local disk ("C:\Michal\Development\Libs\...") - it would never work for anyone else and not even for me on a different computer. And that's exactly what the wizard suggests you do!

So just select the "Hibernate" platform and "Disable Library Configuration" for JPA implementation. Eclipse displays a neat warning as if it suspected we didn't know what we are doing.

Now it's time to set up the connection.


Step #5: Adding the connection


Adding the connection

Click "Add connection...". Give it a name and click next.

Adding the connection

Fill in the data. We are using HSQLDB in the Server mode, so the DB location is hsql://localhost/dogsdb and it makes the URL become jdbc:hsqldb:hsql://localhost/dogsdb.

Note: HSQLDB in Server mode means that you need to explicitly start the server, it won't work if you just run the application (unlike the non-server file based and memory modes).

The server isn't up yet, so you will get an error when trying to connect:

Connection error

I would not check "Add driver library to build path".

Click "Finish".


Step #6: The HSQLDB


If the connection error annoyed you, no worries. We will take care of that. Create a folder called "lib" in your project and drop hsqldb-1.8.0.7.jar in there. You can get it from HSQLDB Download site (a newer version like 1.8.1.12 is fine, an older one should be OK too).

Now, let's add two batch files (or shell files; I am Windows based here at the moment) to the project root. Let's call them startDb.bat (startDb.sh for Unix based systems) and startManager.bat (startManager.sh). Here's the content.

startDb.bat
java -cp lib/hsqldb-1.8.0.7.jar
org.hsqldb.Server
-database.0
file:db\dogsdb -dbname.0 DOGSDB
[Line breaks added for readability] See how the database name corresponds with what you provided earlier?

If you run this, a folder called db will be created and database files put there. The java command must be in system PATH.

Note: You should run it from your system explorer (Windows Explorer, Finder etc.) and not from Eclipse. If you run it from Eclipse it is bound to fail because Eclipse runs it from its own root folder (where it is installed) and not from the project root.

HSQLDB server running

If you test the connection from Eclipse, it's going to be just fine.

Ping succeeded

You should also connect now from the Eclipse Data Source Explorer.

Connecting from Eclipse Data Source Explorer

startManager.bat
java -classpath lib/hsqldb-1.8.0.7.jar
org.hsqldb.util.DatabaseManagerSwing
[Line breaks added for readability] Now, this is for the Database Manager from HSQLDB. If you run this, you should set connection parameters as follows and it will let you in.

HSQLDB Database Manager


Step #7: Project classpath


You must add some libs to the project - namely Hibernate libs with certain dependencies. You can get them from the official distribution. Just drop them in the lib folder where hsqldb already is. Select them all (except for hsqldb) and right click them. Then Build path -> Add to build path.

Dependencies

You need the following jar files in the classpath:
  • antlr-2.7.6.jar
  • commons-collections-3.1.jar
  • dom4j-1.6.1.jar
  • hibernate3.jar
  • hibernate-jpa-2.0-api-1.0.0.Final.jar
  • javassist-3.9.0.jar
  • jta-1.1.jar
  • slf4j-api-1.5.8.jar

Now, you might get the following warning:

Classpath entry /Dogs/lib/dom4j-1.6.1.jar will not be exported or published. Runtime ClassNotFoundExceptions may result
Classpath entry /Dogs/lib/dom4j-1.6.1.jar
will not be exported or published.
Runtime ClassNotFoundExceptions may result
[Line breaks added for readability]

The only thing I could do about it was to click each of those issues (one per jar in classpath) and go for the quick fix (right click it and select "Quick Fix"; or select it and hit Ctrl+1). Then I would select "Exclude the associated raw classpath entry from the set of potential publish/export dependencies." Selecting the other option ("Mark the associated raw classpath entry as a publish/export dependency") would give me another warning that I couldn't fix.

Exclude the associated raw classpath entry from the set of potential publish/export dependencies.

If you plan to export your application with the Eclipse Export tool you might want to add hsqldb-1.8.0.7.jar to the build path too and it will not be exported.

Summary


In this tutorial we created a simple Eclipse JPA project thanks to which we can benefit from Eclipse JPA support. What is important, the project itself has all the dependencies it needs, so they are not referenced from an external location. In the next part we will create some simple JPA code.

Download source code for this article

78 comments:

  1. Hello, Im looking forward the next part.
    Im interested in what is the main goal of an JPA project.

    ReplyDelete
    Replies
    1. Amazing & Great informative blog,it gives very useful practical information to developer like me. Besides that Wisen has established as Best Hibernate Training in Chennai . or learn thru Online Training mode Hibernate Online Training | Java EE Online Training. Nowadays Hibernate ORM has tons of job opportunities on various vertical industry.

      Delete
    2. Great Article android based projects

      Java Training in Chennai

      Project Center in Chennai

      Java Training in Chennai

      projects for cse

      The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

      Delete
  2. Really cool blogentry ... I had one problem however folowing your guide, when attempting to "ping", the connection was failing because eclipse could not find the hsqldb.jar.
    Adding it to the list of jars in the connection definition solved the problem - but this was exactly what you argued against ?

    Agata

    ReplyDelete
  3. @cscsaba242
    I don't really like Eclipse's JPA support so I wouldn't recommend using it. Maybe the support will be better in future - for now I'm not planning writing more on it.

    @themathmagician
    I think you're fine this way. I'd just test if you can export your app and whether it still works (as opposed to falling over on classes not found).

    ReplyDelete
  4. Hello, how I can use the HSQLDB as configuration database, for a simple java SE project in netbeans. Thanks.

    ReplyDelete
  5. This is such a good post. One of the best posts that I\'ve read in my whole life. I am so happy that you chose this day to give me this. Please, continue to give me such valuable posts. Cheers!
    Data Science Training in Chennai
    Data Science course in anna nagar
    Data Science course in chennai
    Data science course in Bangalore
    Data Science course in marathahalli
    Data science course in bangalore

    ReplyDelete
  6. Nice blog,I understood the topic very clearly,And want to study more like this.
    Data Scientist Course

    ReplyDelete
  7. I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page!

    data science course

    ReplyDelete
  8. I thought I was strong with my ideas on this, but with your writing expertise you have managed to convert my beliefs your new ideas. Mallorca is fun
    Best Data Science training in Mumbai

    Data Science training in Mumbai

    ReplyDelete
  9. Reading this article gave me many things to think about. You have some quality information here that any reader would enjoy. I share many of your views in this article.
    SAP training in Mumbai
    Best SAP training in Mumbai
    SAP training institute Mumbai

    ReplyDelete
  10. So this is what happens when a writer does the homework needed to write quality material. Thank you very much for sharing this wonderful content.
    SAP training in Kolkata
    Best SAP training in Kolkata
    SAP training institute in Kolkata

    ReplyDelete
  11. It's so great to know you are a writer that cares about the information you provide. This is smartly done and well-written in my opinion.
    SAP training in Kolkata
    SAP training Kolkata
    Best SAP training in Kolkata
    SAP course in Kolkata
    SAP training institute Kolkata

    ReplyDelete
  12. Attend The Data Analyst Course From ExcelR. Practical Data Analyst Course Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Analyst Course.
    Data Analyst Course

    ReplyDelete
  13. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
    Correlation vs Covariance
    Simple linear regression
    data science interview questions

    ReplyDelete
  14. I have to search sites with relevant information on given topic and provide them to teacher our opinion and the article.

    data science interview questions

    ReplyDelete
  15. Attend online training from one of the best training institute Data Science Training in Hyderabad

    ReplyDelete

  16. Easily, the article is actually the best topic on this registry related issue. I fit in with your conclusions and will eagerly look forward to your next updates.
    data science courses

    ReplyDelete
  17. Very nice blogs!!! i have to learning for lot of information for this sites…Sharing for wonderful information.Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing, data sciecne course in hyderabad

    ReplyDelete
  18. Incredibly all around intriguing post. I was searching for such a data and completely appreciated inspecting this one. Continue posting. A commitment of gratefulness is all together for sharing.business analytics course in Hyderabad

    ReplyDelete
  19. Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one. Keep posting. Thanks for sharing.
    business analytics course

    ReplyDelete
  20. I’m excited to uncover this page. I need to to thank you for ones time for this particularly fantastic read!! I definitely really liked every part of it and i also have you saved to fav to look at new information in your site.
    Best Data Science Courses in Hyderabad

    ReplyDelete
  21. All things considered I read it yesterday however I had a few considerations about it and today I needed to peruse it again on the grounds that it is very elegantly composed.
    data scientist course

    ReplyDelete
  22. I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Definitely a great post. Hats off to you! The information that you have provided is very helpful.
    Best Data Science Courses in Hyderabad

    ReplyDelete
  23. Extremely overall quite fascinating post. I was searching for this sort of data and delighted in perusing this one. Continue posting. A debt of gratitude is in order for sharing.data science courses in Hyderabad

    ReplyDelete
  24. Truly overall quite fascinating post. I was searching for this sort of data and delighted in perusing this one. Continue posting. Much obliged for sharing.business analytics course

    ReplyDelete
  25. You completed certain reliable points there. I did a search on the subject and found nearly all persons will agree with your blog.

    Best Institute for Data Science in Hyderabad

    ReplyDelete
  26. I at last discovered extraordinary post here.I will get back here. I just added your blog to my bookmark locales. thanks.Quality presents is the pivotal on welcome the guests to visit the website page, that is the thing that this page is giving.data science training Hyderabad

    ReplyDelete
  27. Hello, I have browsed most of your posts. This post is probably where I got the most useful information for my research. Thanks for posting, we can see more on this. Are you aware of any other websites on this subject.
    Best Data Science Courses in Hyderabad


    ReplyDelete
  28. I've read this post and if I could I desire to suggest you some interesting things or suggestions. Perhaps you could write next articles referring to this article. I want to read more things about it!
    data science training

    ReplyDelete
  29. Took me time to understand all of the comments, but I seriously enjoyed the write-up. It proved being really helpful to me and Im positive to all of the commenters right here! Its constantly nice when you can not only be informed, but also entertained! I am certain you had enjoyable writing this write-up.
    data science training in Hyderabad

    ReplyDelete


  30. I am glad that i found this page ,Thank you for the wonderful and useful articles with lots of information.
    Data Science Course in Mumbai

    ReplyDelete
  31. Very informative content and intresting blog post.Data science course in Nashik

    ReplyDelete
  32. Fantastic blog extremely good well enjoyed with the incredible informative content which surely activates the learners to gain the enough knowledge. Which in turn makes the readers to explore themselves and involve deeply in to the subject. Wish you to dispatch the similar content successively in future as well.

    Data Science training in Raipur

    ReplyDelete
  33. Truly incredible blog found to be very impressive due to which the learners who ever go through it will try to explore themselves with the content to develop the skills to an extreme level. Eventually, thanking the blogger to come up with such an phenomenal content. Hope you arrive with the similar content in future as well.

    Digital Marketing Course

    ReplyDelete
  34. Highly appreciable regarding the uniqueness of the content. This perhaps makes the readers feels excited to get stick to the subject. Certainly, the learners would thank the blogger to come up with the innovative content which keeps the readers to be up to date to stand by the competition. Once again nice blog keep it up and keep sharing the content as always.

    Data Science certification in Bhilai

    ReplyDelete
  35. Wonderful blog found to be very impressive to come across such an awesome blog. I should really appreciate the blogger for the efforts they have put in to develop such an amazing content for all the curious readers who are very keen of being updated across every corner. Ultimately, this is an awesome experience for the readers. Anyways, thanks a lot and keep sharing the content in future too.

    Digital Marketing Course in Bhilai

    ReplyDelete


  36. I was taking a gander at some of your posts on this site and I consider this site is truly informational! Keep setting up..
    Data Science Training

    ReplyDelete
  37. Very informative content and intresting blog post.Data science course in Nashik

    ReplyDelete
  38. Through this post, I realize that your great information in playing with all the pieces was useful. I inform that this is the primary spot where I discover issues I've been looking for. You have a cunning yet alluring method of composing. data scientist training and placement

    ReplyDelete
  39. Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more.
    Data Science Course in Bangalore

    ReplyDelete
  40. Thanks for posting the information and the blog is very informative.Data science course in Faridabad

    ReplyDelete
  41. Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more.
    Data Science Course in Bangalore

    ReplyDelete
  42. Thanks for posting the best information and the blog is very informative.Data science course in Faridabad

    ReplyDelete

  43. wow, great, I was wondering how to cure acne naturally. and found your site by google, learned a lot, now i’m a bit clear. I’ve bookmarked your site and also added rss. keep us updated.

    business analytics course

    ReplyDelete
  44. Excellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well. I wanted to thank you for this websites! Thanks for sharing. Great websites!
    Data Science Training in Bangalore

    ReplyDelete
  45. Thanks for posting the best information and the blog is very informative.Data science course in Faridabad

    ReplyDelete
  46. I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page!
    data analytics course in bangalore

    ReplyDelete
  47. I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page!
    data analytics course in bangalore

    ReplyDelete
  48. I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work.
    artificial intelligence course in pune

    ReplyDelete
  49. I am glad to discover this page. I have to thank you for the time I spent on this especially great reading !! I really liked each part and also bookmarked you for new information on your site.
    Data Science Training in Chennai

    ReplyDelete

  50. I see the greatest contents on your blog and I extremely love reading them.
    Best Institute for Data Science in Hyderabad

    ReplyDelete
  51. Thanks for posting the best information and the blog is very informative.Data science course in Faridabad

    ReplyDelete
  52. Excellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well. I wanted to thank you for this websites! Thanks for sharing. Great websites!
    Data Science Training in Bangalore

    ReplyDelete
  53. Thanks for posting the best information and the blog is very infData science course in Varanasi

    ReplyDelete
  54. I am a new user of this site, so here I saw several articles and posts published on this site, I am more interested in some of them, hope you will provide more information on these topics in your next articles.
    data analytics training in bangalore

    ReplyDelete
  55. I am a new user of this site, so here I saw several articles and posts published on this site, I am more interested in some of them, hope you will provide more information on these topics in your next articles.
    data analytics training in bangalore

    ReplyDelete