Database development with HiberObjects
Posted February 7, 2008on:
There are a lot of UML and ER tools out there, but they cost a lot of money, too.
This is a step-by-step to develop your database with Eclipse and HiberObjects for free.
It is a fast way for Java developers to develop database tables.
It uses an UML-ER Model to generate JavaBeans, which can create tables on the database by using Hibernate as persistence manager.
Download and extract the files
Download the last version of Eclipse Java EE Edition (second entry)
You will need a database, I like MySQL but you can use any other Database. Download the proper JDBC Driver
Extract these files where you like but keep in mind not to use blanks ” ” or special characters “áä”. I used “C:\Development” as path. And create a new workspace folder.
Your development folder should look like this.
Next download hiberobjects-all-1.3.18.zip
Extract this file in the Eclipse directory and overwrite all existing folders.
Download and install database
Download the MySQL database from this page
Next we need to get our database up and running.
Run the Setup and make a “typical” installation. At the end of the Setup configure the MySQL server. Choose “Standard Configuration”. Do not change anything in the next screen. Now set root password for the DB. I used “mysql” at this point (very inventive, huh?). Go to the next screen and finish the configuration.
Start Eclipse and create a new project
Start Eclipse and set your workspace to “C:\Development\workspace”.
Close the welcome screen.
Create a new HiberObjects project.
Choose “File” => “new” => “Project…”
Select HiberObjects project. Customize the project name or the root package if you want to and click “Finish”.
Eclipse asks you, if you want to switch to the HiberObjects perspective. Choose “Yes”
Configure the JDBC connection
Check out “hibernate.properties” in the “conf” folder.
This file contains the configuration for the JDBC connection. The file is configured for HSQL database.
Happily there is a note how to configure the MySQL connection.
Comment and comment up the right lines. Enter “root” as username and “mysql” as password. We will use the MySQL “test” database. Change the last line to “hibernate.connection.url=jdbc:mysql://localhost/test”.
The file should now look like this.
Next we need to add the MySQL JDBC driver to the project.
Right click your project and select „Properties“, “Java Build Path” and “Libraries”. Add an external JAR file. And select “mysql-connector-java-5.0.8-bin.jar” from “C:\Development\mysql-connector-java-5.0.8” and click OK.
The JDBC connection to MySQL is finished.
Create the model
Select “Model” in “HiberObjects” => “com.xyz.myproject.model”
That’s our playground.
Next we create a more or less simple model. I thought of a program that helps us with the market research for our super market.
Right click at your empty model and create a new class “Customer”. Right click at the new class and generate new attribute “+String name”. The “+” at the beginning indicates that our attribute is public, but only the getters and setters will be public.
You can get more informations here
If you don’t know what you are doing here, you should look at this.
Right click at the “Costumer” class and select “Show” => ”Id”. The attribute “+Long id <<id>>” appears. This attribute is generated for all classes automatically as primary key. Later you can see the attribute in your JavaBean, too.
Create a new class “Visit”. The visit at our super market hat got a time and a duration. Create the new attributes “+Date time” and “+String duration”.
You can add a JPA annotation to the duration attribute. Right click at “+String duration” and select “New” => “Tag”. A new “@Column” Tag appears. Right click at this one and create a new parameter for this one, enter “nullable=false”.
We will need a class “Product”, too. A product has usually a name and a prize. Make new Attributes “+String name” and “+double prize”.
Now it is time to connect the classes.
Make a bidirectional association between “Costumer” and “Visit” by clicking at the “—” Symbol and connect each class with another.
One costumer can do several visits but one visit can only be made by one costumer. Change the the Multiplicity by right click on it. Change the one at “Visit” from “0..1” to “*” and the one at “Costumer” to “1”.
Now connect the “Visit” class with the “Product” class by a bidirectional association.
The costumer can buy several product at each visit and one product can be bought at different visits. Change both Multiplicities from “0..1” to “*”.
We want to store these informations at the database. Right click at each class and select as Stereotype “Persistent”.
That will do it for now. The model is complete for our purpose.
Generate the JavaBeans
When you are done, right click “HiberObjects” in your Eclipse project and select “Save all”.
HiberObjects has generated three JavaBeans from the Model in the com.xyz.myproject.model” package.
It should look like this.
Create the database tables
In the package “com.xyz.myproject.util” is a file “HibernateHelper.java”. Right click at it and select “Run as” => “Java Application”
The log says everything is all right.
Start your database administration tool to take a look at your database.
(See? Even the duration column is set to NOT NULL)
Everything is there where it should be (after an uncountable number of right clicks)