Using Hibernate With Eclipse
Using Hibernate With Eclipse
In-Memory Database
Thomas Engelin Itancan Consulting AB 2008
1. Terminology
1.1. Abbreviations
ORM PK POJO RCP Object-Relational Mapping Primary Key Plain Old Java Object Rich Client Platform
1.2. Definitions
Bundling Hibernate HSQLDB Creating an Eclipse plug-in from third-party JARs Open source ORM framework Open source relational database
2. Abstract
It is pretty common to find programmers struggling with setting up and using Hibernate to access a database from within Eclipse RCP applications. This document is intended as a quick guide with the basic steps on how to make this work. The main software used is in this document is: SuSE Linux (SLES9) Eclipse SDK 3.4.1 Eclipse RCP SDK 3.4 Hibernate 3.3.1.GA HSQLDB 1.8.0.10
3. Problem Statement
Eclipse RCP (or plug-in) developers sometimes need to use a database for storing applicationor domain information. A common way forward is to use an ORM framework such as JDO, iBatis, ObJectRelationalBridge or Hibernate, which is the ORM choice in this document. An ORM framework takes care of the mapping between plain Java objects (POJOs) and the relational database model. Once up and running, the ORM framework simplifies the parts of the coding that has to do with persistence. The hard part is to get things up and running! What follows is a brief step-by-step description on how to access a database (HSQLDB) from within an Eclipse RCP application using Hibernate as ORM framework.
4. Data Model
Just to have something concrete to work with, assume were developing an application that stores questions together with correct answer(s) and hints (suggestions). The problem domain architecture could look like this (white boxes indicate domain classes, shaded boxes indicate infrastructure classes):
Database
QuestionBean
Connection
Question
1..*
Answer
*
Suggestion
The database will store a number of Questions, where each Question has one or several correct Answers and any (including zero) number of Suggestions. These classes will be used in the actual application, and when we store this model as a data model we end up with only one table:
Multiple answers and suggestions will be stored as one string in the database, separated with a delimiter character (very simple solution). The QuestionBean class will be the glue between the Eclipse RCP application and Hibernate, but more about that later.
To set up the database schema, create a file setup.sql containing: CREATE TABLE QUESTIONS ( ID INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1), QUESTION VARCHAR NOT NULL, ANSWERS VARCHAR NOT NULL, SUGGESTIONS VARCHAR NOT NULL, PRIMARY KEY (ID) ) In the database manager, select File > Open Script, select your setup.sql file and press OK, then hit the Execute button. Select View > Refresh Tree to see the result. The database setup is now finished. Close the database manager but leave the database server running.
Uncheck the option to unzip the JAR archives into the project. Click Finish. To install this plug-in at the target location, it must be exported. Right-click the project and select Export > Plug-in Development > Deployable plug-ins and fragments > Next. Select the new plug-in in the list of available Plug-ins. In the Destination tab, check Directory and set its value to the target installation location (the goal is to export the plug-in to a directory named org.hibernate.core_1.0.0 in the targets plugins directory). In the Options tab, uncheck all options. Click Finish. Go to Window > Preferences > Plug-in Development > Target Platform, click Reload, make sure the newly added plug-in is selected, then press OK.
Select the Hello RCP template, click Next and then Finish. In the manifest editor, select the Dependencies tab and add org.hibernate.core to the list of Required Plug-ins.
Enter the following values on the Arguments tab: Program arguments: Working directory: In the Plug-ins tab, enter: Launch with: plug-ins selected below only -os ${target.os} -ws ${target.ws} -arch ${target.arch} nl ${target.nl} -consoleLog ${workspace_loc}/../runtime-com.itancan.quiz
Press Deselect All, then select com.itancan.quiz and press Add Required Plug-ins. Deselect the org.hibernate.core plug-in from the Workspace and select it in the Target Platform instead. Save the run configuration by pressing Apply. It should now be possible to launch the RCP application, which of course is empty and can do nothing.
10
11
12
13
13. References
http://www.hibernate.org/hib_docs/reference/en-US/html/tutorial.html Eclipse Rich Client Platform (McAffer, Lemieux) ISBN 0-321-33461-2 Eclipse Building Commercial-Quality Plug-ins (Clayberg, Rubel) ISBN 0-321-42672-X Agile Java Development with Spring, Hibernate and Eclipse (Hemrajani) ISBN 0-672-32896-8
14