Moving your spring-roo hibernate project from Hypersonic to Postgresql
I’ve always been an ORM hater. However, recently the decision to use ORM on a project I am working on was made for me. So I borrowed a pair of my pro-ORM’s colleague’s moccasins so I could walk a few miles in them. To throw me further out of my comfort zone, this project is being done in java, a language I am quite rusty in.
The first thing I learned about ORMs is that at least in java, you don’t pick your database right away. You start off using an in-memory database like Hypersonic until your data model is fleshed out a bit. Our project is not quite there yet, but we wanted to get familiar with the procedure for doing that. I made an attempt to use SQL Server Denali CTP1 as our database. However, that was proving difficult and another colleague suggested I use a database that might be more “java friendly.” I decided to go with my first true love Postgres.
Our ORM was hibernate. We were using the SpringSource Tool Suite or STS. This includes the development tool spring roo. Amongst many other things, roo manages your maven dependencies, and configures your hibernate persistance layer. These are the particular tasks I will concentrate on for this howto.
Installing postgres
How to do this on your particular operating system is beyond the scope of this document. I went with postgres 9.1 beta 1 just because. The lastest version of postgres 8 should be fine as well. In the examples below I am assuming your database server is running on localhost, and the user name and password you use to connect to it are both postgres. Never do this on anything besides your local laptop, and don’t even do that on your laptop if your postgres port (5432 TCP) is opened to the world.
Installing the driver
According to a comment on this stackoverflow answer, You have to use the jdbc3 postgres driver and not the jdbc4 version for hibernate to be able to generate the DDL to make the tables. My experiments have confirmed this. To install it, startup the roo shell. The roo shell can either be started from your operating system shell via roo.sh/roo.bat, or from inside the SpringSource IDE, which is a very polished eclipse distro. If you are starting up the roo shell from a terminal or command prompt, first cd to the folder where your project is located. The command to install the postgres jdbc driver is as follows:
roo> dependency add --groupId postgresql --artifactId postgresql --version 9.0-801.jdbc3
Updated ROOT\pom.xml [added dependency postgresql:postgresql:9.0-801.jdbc3]
roo>
This will take care of fetching the dependency and editing the pom.xml for you. If you want to attempt with a different version of the postgres driver, browse the different versions on mvnrepository.
Switching the persistence layer
Persistence layer is hibernate speak for database. This makes sense, because data persists in your database, as opposed to ram, cache, etc which is all meant to be temporary. Switching persistence layers in hibernate requires editing two files, persistence.xml and database.info. Editing these two by hand would violate the don’t repeat yourself rule. Luckily we can edit both with one command in the roo shell.
roo> persistence setup --database POSTGRES --provider HIBERNATE --databaseName myproject --userName postgres --password postgres
Updated ROOT\pom.xml [added dependency postgresql:postgresql:8.4-701.jdbc3]
roo>
One thing to be aware of here. The database name needs to be lower case. If you used uppercase characters, your app will not be able to connect to it.
Your persistance.xml file will now look like this:
And your database.properties will look like this:
One manual step
I have not figured out how to get hibernate to execute CREATE DATABASE if the database does not exist. So connect to postgres, and execute the following:
CREATE DATABASE myproject;
Conclusion
Hopefully, all you will need to do is debug your project as a web applicatin and the tables in your application get built. Happy coding!
If you have too much free time, use Java.
Now, I am no superstar programmer, this I admit. However, I do believe I am fairly intelligent, and have a decent breadth of knowledge and ability when it comes not just to programming, but IT as a whole.
This weekend, I decided to spend a little time attempting to re-invent an intranet app I created using Java in Applet form, as opposed to the present PHP/Ajax solution. To provide a little background, the present application is basically an administrative tool which keeps track of computers & users. It presents several tables identifying e-mail accounts, Southware users, PCs, and the relationships of these various items to each other. For now, I use PHP5 with Smarty templating, and use XMLHttpRequest calls to update just certain portions of the page instead of reloading the whole thing each time you want to sort a table, or edit a field.
I am pretty pleasd with the solution thus far, however I like to experiment. Previously, I tried using Flex — and did OK until the point that I hit what I believe was a limitation of Flex itself, having to do with binding controls to the gridview and having changes propagate back to the control that the edit controls were bound to. So this time around, I said to myself “Hey, I took Java back in college and even did my senior project in that!”.
Can’t be too hard right? Taking some data, displaying it on a grid, edits, etc. Boy was I wrong!
CHALLENGE #1 — Finding a decent IDE
Good luck! I tried probably 5 different IDEs, and ditched them all for various reasons — mostly because they weren’t free. Finally I ended up with Netbeans, which I happened upon completely by accident, as I was browsing around Sun’s website. It’s actually a pretty nice IDE. It has several built in project templates, a drag & drop WYSIWYG designer, code completion, etc. This IDE also had a cool feature where you can easily set up a JDBC connection to several database servers, in my case PostgreSQL. I was able to connect, browse around the tables (however, you must have a primary key before any tables can be used!?), and bind the tables to various controls for example a list or a data grid. I had some problems with the IDE though — I had a tough time wrestling for control. It wouldn’t let me delete function declarations, variables, etc.. They were just simply a different shade of gray, and somehow untouchable.
CHALLENGE #2 — Getting anything done
Once I had finally gotten everything set up with my little project, I tried to compile it. UHOH it seems it couldn’t find javax.persistence. WTF is that you may ask? Well, apparently it’s necessary (at least with this IDE) to bind the database to the control. After another half an hour of searching, I resigned myself to having to download the enterprise edition of the SDK (100+ megs..) and then trying again. After that was finally done, I tried to run it again… It compiled, but now it was giving exceptions on start, and it STILL wouldn’t work..
CHALLENGE #3 — Debugging!
There may be a nice quick solution for this (if there is, please let me know). If you’re writing an applet you should be debugging in your browser right? Well if you do, then your browser will cache the Applet, and your subsequent rebuilds will not be represented.. I am using firefox, and to counter this, it is suggested you load up your Tools -> Java Console, and hit ‘x’ to clear the java cache.. So I did it. And did it again. And again and again. NO EFFECT! I discovered I had to exit firefox in its entirety after clearing the cache, then re-launching, and THEN it would show the new applet. SO, what kind of way to debug is that?? As I said above, i’m no superstar.. I like to code a little, then check it. Then code, then check. I just can’t work like that. Yes, you can run the program as a regular application then switch to be an applet, but then you have various security permissions issues which may come back to bite you if you’re not careful no?
The above may not seem particularly frustrating, but all of this came after several hours of playing around with SOAP. I had gotten one of the functions of my app set up via SOAP, and was going to test that with java. I was looking around the web, and all the samples I saw were horrible… They were hand-crafting the request XML, OR using some 3rd party framework to get the job done. I didn’t want to do either of those things, so eventually I gave up on SOAP, and that is when I tried the direct database connection.
In closing, those of you out there who are PROs at Java will read this and laugh — no problem there. What should be taken away from this though, is that while Java may be perfectly good once you immerse yourself in the intricacies, it is not for the faint of heart, and in the age of integration, polished languages & IDEs, it’s just not up to the hype.