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

102 comments:

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

    ReplyDelete
  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. 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
  7. 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
  8. 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

  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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


  16. 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
  17. 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
  18. Thanks for posting the best information and the blog is very informative.Data science course in Faridabad

    ReplyDelete
  19. 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

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

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

    ReplyDelete
  22. Thanks for posting the best information and the blog is very helpful.Data science course in Varanasi

    ReplyDelete
  23. 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
  24. I Want to leave a little comment to support and wish you the best of luck.we wish you the best of luck in all your blogging endeavors.
    data science in bangalore

    ReplyDelete

  25. 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. keep us updated.


    Data Science Training

    ReplyDelete
  26. Impressive. Your story always bring hope and new energy. Keep up the good work.
    business analytics course

    ReplyDelete
  27. Really wonderful blog completely enjoyed reading and learning to gain the vast knowledge. Eventually, this blog helps in developing certain skills which in turn helpful in implementing those skills. Thanking the blogger for delivering such a beautiful content and keep posting the contents in upcoming days.

    data science in bangalore

    ReplyDelete
  28. Thanks for posting the best information and the blog is very helpful.artificial intelligence course in hyderabad

    ReplyDelete
  29. This is an excellent post I saw thanks for sharing it. It is really what I wanted to see. I hope in the future you will continue to share such an excellent post.

    business analytics course

    ReplyDelete
  30. Thanks for posting the best information and the blog is very helpful.digital marketing institute in hyderabad

    ReplyDelete
  31. 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 scientist course in bangalore

    ReplyDelete
  32. I like viewing web sites which comprehend the price of delivering the excellent useful resource free of charge. I truly adored reading your posting. Thank you!
    best data science institute in Hyderabad

    ReplyDelete
  33. Hi to everybody, here every one is sharing such knowledge, so it’s fastidious to see this site, and I used to visit this blog daily

    business analytics course

    ReplyDelete
  34. Thanks for posting the best information and the blog is very important.data science institutes in hyderabad

    ReplyDelete
  35. 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 course

    ReplyDelete
  36. 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
  37. This is a very nice one and gives in-depth information. I am really happy with the quality and presentation of the article. I’d really like to appreciate the efforts you get with writing this post. Thanks for sharing.
    Python classes in Ahmednagar

    ReplyDelete
  38. I wanted to leave a little comment to support you and wish you a good continuation. Wishing you the best of luck for all your blogging efforts.
    Best Data Science courses in Hyderabad

    ReplyDelete
  39. You completed a number of nice points there. I did a search on the issue and found nearly all people will have the same opinion with your blog.
    data scientist course in hyderabad

    ReplyDelete
  40. Fantastic article I ought to say and thanks to the info. Instruction is absolutely a sticky topic. But remains one of the top issues of the time. I love your article and look forward to more.
    Data Science Course in Bangalore

    ReplyDelete
  41. I feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it.
    best data science institute in hyderabad

    ReplyDelete
  42. Just the way I have expected. Your website really is interesting.
    Best Data Science courses in Hyderabad

    ReplyDelete
  43. 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 amazing content for all the curious readers who are very keen on being updated across every corner. Ultimately, this is an awesome experience for the readers. Anyways, thanks a lot and keep sharing the content in the future too.

    Digital Marketing Training in Bangalore

    ReplyDelete
  44. Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post.
    digital marketing courses in hyderabad with placement

    ReplyDelete
  45. I was basically inspecting through the web filtering for certain data and ran over your blog. I am flabbergasted by the data that you have on this blog. It shows how well you welcome this subject. Bookmarked this page, will return for extra. data science course in jaipur

    ReplyDelete
  46. A good blog always comes-up with new and exciting information and while reading I have feel that this blog is really have all those quality that qualify a blog to be a one.
    Best Data Science courses in Hyderabad

    ReplyDelete
  47. I will really appreciate the writer's choice for choosing this excellent article appropriate to my matter.Here is deep description about the article matter which helped me more.
    data science course

    ReplyDelete
  48. I finally found great post here.I will get back here. I just added your blog to my bookmark sites. thanks.Quality posts is the crucial to invite the visitors to visit the web page, that's what this web page is providing.data science course
    data scientist training in hyderabad

    ReplyDelete
  49. A good blog always comes-up with new and exciting information and while reading I have feel that this blog is really have all those quality that qualify a blog to be a one.
    data science training in malaysia

    ReplyDelete
  50. I finally found great post here.I will get back here. I just added your blog to my bookmark sites. thanks.Quality posts is the crucial to invite the visitors to visit the web page, that's what this web page is providing.data scientist training in hyderabad

    ReplyDelete
  51. 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 scientist course in delhi

    ReplyDelete
  52. Hi, I log on to your new stuff like every week. Your humoristic style is witty, keep it up
    data scientist training and placement

    ReplyDelete
  53. I was actually browsing the internet for certain information, accidentally came across your blog found it to be very impressive. I am elated to go with the information you have provided on this blog, eventually, it helps the readers whoever goes through this blog. Hoping you continue the spirit to inspire the readers and amaze them with your fabulous content.

    Data Science Course in Faridabad

    ReplyDelete
  54. Mind-blowing went amazed with the content posted. Containing the information in its unique format with fully loaded valid info, which ultimately grabs the folks to go through its content. Hope you to keep up maintaining the standards in posting the content further too.

    Data Science Course in Bangalore

    ReplyDelete
  55. Thanks for bringing such an innovative content which truly attracts the readers towards you. Certainly, your blog competes with your co-bloggers to come up with the newly updated info. Finally, kudos to your efforts.

    Data Science Course in Varanasi

    ReplyDelete
  56. I want you to thank for your time of this wonderful read!!! I definately enjoy every little bit of it and I have you bookmarked to check out new stuff of your blog a must read blog!
    data scientist course in malaysia

    ReplyDelete
  57. You actually make it look so easy with your performance but I find this matter to be actually something which I think I would never comprehend. It seems too complicated and extremely broad for me. I'm looking forward for your next post, I’ll try to get the hang of it!
    data scientist training in hyderabad

    ReplyDelete
  58. This is really very nice post you shared, i like the post, thanks for sharing..
    data science training

    ReplyDelete
  59. 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 noida

    ReplyDelete
  60. Thanks for sharing this information. I really like your blog post very much. You have really shared a informative and interesting blog post .
    data scientist training in hyderabad

    ReplyDelete
  61. Impressive blog to be honest definitely this post will inspire many more upcoming aspirants. Eventually, this makes the participants to experience and innovate themselves through knowledge wise by visiting this kind of a blog. Once again excellent job keep inspiring with your cool stuff.

    Data Science Training in Bhilai

    ReplyDelete
  62. It is a very helpful and very informative blog. I really learned a lot from it thanks for sharing.
    Data Analytics Course

    ReplyDelete
  63. Fantastic article I ought to say and thanks to the info. Instruction is absolutely a sticky topic. But remains one of the top issues of the time. I love your article and look forward to more.
    Data Science Course in Bangalore

    ReplyDelete


  64. I am impressed by the information that you have on this blog. It shows how well you understand this subject. ethical hacking course fees in jaipur


    ReplyDelete
  65. Amazingly by and large very interesting post. I was looking for such an information and thoroughly enjoyed examining this one. Keep posting. An obligation of appreciation is all together for sharing.data analytics course delhi

    ReplyDelete
  66. It is late to find this act. At least one should be familiar with the fact that such events exist. I agree with your blog and will come back to inspect it further in the future, so keep your performance going.

    Best Institute for Cloud Computing in Bangalore

    ReplyDelete
  67. Your content is nothing short of brilliant in many ways. I think this is engaging and eye-opening material. Thank you so much for caring about your content and your readers.
    ai training in hyderabad

    ReplyDelete
  68. Very good points you wrote here..Great stuff...I think you've made some truly interesting points.Keep up the good work.
    best aws training in hyderabad

    ReplyDelete
  69. Amazingly by and large very interesting post. I was looking for such an information and thoroughly enjoyed examining this one. Keep posting. An obligation of appreciation is all together for sharing.data science course in bhubaneswar

    ReplyDelete
  70. 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.data science course in bhopal

    ReplyDelete
  71. Really awesome blog, useful information. If you want to become a data scientist, here is the best option for you. For more information click the below link.
    Data Science Course in Hyderabad

    ReplyDelete
  72. Thanks for the informative and helpful post, obviously in your blog everything is good..
    cyber security course in malaysia

    ReplyDelete
  73. Hey There. I found your blog using msn. This is a very well written article. I’ll be sure to bookmark it and come back to read more of your useful info. Thanks for the post. I’ll definitely return. business analytics course in kanpur

    ReplyDelete
  74. These thoughts just blew my mind. I am glad you have posted this. data analytics course in surat

    ReplyDelete
  75. You finished certain solid focuses there. I did a pursuit regarding the matter and discovered almost all people will concur with your blog.

    ReplyDelete
  76. 360DigiTMG, the top-rated organisation among the most prestigious industries around the world, is an educational destination for those looking to pursue their dreams around the globe. The company is changing careers of many people through constant improvement, 360DigiTMG provides an outstanding learning experience and distinguishes itself from the pack. 360DigiTMG is a prominent global presence by offering world-class training. Its main office is in India and subsidiaries across Malaysia, USA, East Asia, Australia, Uk, Netherlands, and the Middle East.

    ReplyDelete
  77. I am truly getting a charge out of perusing your elegantly composed articles. It would seem that you burn through a ton of energy and time on your blog. I have bookmarked it and I am anticipating perusing new articles. Keep doing awesome. business analytics course in kanpur

    ReplyDelete
  78. Thanks for the best blog. it was very useful for me.keep sharing such ideas in the future as well. data analytics course in surat

    ReplyDelete
  79. Actually I read it yesterday but I had some thoughts about it and today I wanted to read it again because it is very well written.
    data science training in hyderabad

    ReplyDelete
  80. Great post I would like to thank you for the efforts you have made in writing this interesting and knowledgeable article.
    data science training institute in hyderabad

    ReplyDelete
  81. This is a smart blog. I mean it. You have so much knowledge about this issue, and so much passion. You also know how to make people rally behind it, obviously from the responses. business analytics course in surat

    ReplyDelete
  82. 360DigiTMG provides exceptional training in the Data Science course with placements. Learn the strategies and techniques from the best industry experts and kick start your career.data analytics course in ranchi

    ReplyDelete
  83. It will also let you know that is the data scientist's jobs are at risk with this automation. So let's get started.

    ReplyDelete
  84. Quite an informative blog! I have found interesting details to read and provide immense knowledge about pursuing a data science certification program by 360DigiTMG. Becoming a certified data science professional will help to add more value to your CV and stay ahead of the competition. Thanks for making such an informative and valuable article for readers.data science course in mumbai

    ReplyDelete
  85. Grab the top data science education online in the convenience of your own home. Flexible schedules, top-notch industry instructors, and painstakingly designed curriculum. Now available!data science course institute in nagpur

    ReplyDelete
  86. あなたのライティングスキルは素晴らしかったです。記事をポイントごとに簡単に説明していただきました。本当に役に立ちました。貴重な記事を共有していただきありがとうございます。

    インスタグラムリールのダウンロード

    ReplyDelete
  87. An amazing web journal I visit this blog, it's unbelievably wonderful. Discover the gateway to success with Ziyyara Edutech's prestigious online English tuition in Qatar.
    For more info visit Online Tuition for english language in Qatar

    ReplyDelete