Persistence with XDoclet and Hibernate

Posted Tue, 07 Mar 2006 20:17:00 GMT

Java 5 annotations are all very nice, but most of us, especially in larger companies, are still stuck with Java 1.4 and even 1.3.

In the meantime we can get some of that goodness and develop our persistence layer quickly and almost transparently by using XDoclet and Hibernate. This will not deal with the transactional and session management issues solved by EJB3, but it will give you a very fast and highly maintainable approach for generating your persistence layer.

This is achieved by using XDoclet and Hibernate’s hbm2ddl utility to eliminate the need for maintaining XML mapping files and keeping your development database up to date.

You still need to know Hibernate to use it. There are some quite intricate areas there such as session management, transaction management, object identity, lazy loading, etc, which are not considered here. Here’s a good book - Hibernate In Action by Gavin King and Christian Bauer.

The whole example is available for download as a zipped Eclipse workspace from here. Extract this in an existing workspace and map the eclipse variables.

What we’ll do here

  • Download XDoclet and Hibernate.
  • Write our model classes.
  • Annotate them using XDoclet tags
  • Write the ant script to build the Hibernate mappings and the database and DDL
  • Write tests
Read more...

Posted in Tips and Hints, Technical articles | 1 comment

Use getters in equals() methods in Hibernate, not properties

Posted Mon, 06 Mar 2006 21:31:00 GMT

This took me some time to work out, so I’m writing it down now…

In Hibernate it is important to write the equals methods if using HashMaps. This is because the get(Object) of a hashmap uses the equals or the hashCode methods to match the value of a key against the table.

Well, I had writen my equals() method to use direct access to properties. like this:

  ...
  return this.name.equals(other.name);
  ...

Unfortunately, this won’t work with freshly retrieved objects. The reason seems to be that Hibernate builds proxies, rather than the full objects. It seems to somehow replace the methods with db lookup methods, so when you do a other.name on a freshly built proxy, that gives you null, but when you do an other.getName() that invokes the method which reads the value from somewhere (I wish I could say the database, but not sure at all).

So, put shortly, the conclusion is that it makes sense to access properties in Hibernate-bound objects through the getters rather thatn directly.

Posted in Tips and Hints | no comments

Little things that are just great in Eclipse

Posted Sat, 25 Feb 2006 22:21:00 GMT

  • When you click on a Java variable it highlights all uses of that variable in the current file.
  • In an ant file editor (I don’t even know how it found it was an ant file) it automatically shows the actual path constituents as soon as you hover your mouse over the path definition!

no comments

Small problem with the Spring MVC tutorial

Posted Sun, 19 Feb 2006 18:02:00 GMT

While doing this absolutely fantastic Spring MVC tutorial - http://www.springframework.org/docs/MVC-step-by-step/ Spring-MVC-step-by-step-Part-2.html (note this is page 2 in it), I stumbled upon a small problem. In this jsp:

...
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>  
...

It's now <c:out value="${now}" />

...

The c:out tag caused this error: org.apache.jasper.JasperException: /WEB-INF/jsp/springstart.jsp(16,13) According to TLD or attribute directive in tag file, attribute value does not accept any expressions

I had to change http://java.sun.com/jstl/core to http://java.sun.com/jstl/core_rt in order for it to work.

This happened in Tomcat 5.5, I thinl the tutorial is aimed at Tomcat 5.0, may be that’s why….

Posted in Tips and Hints | 1 comment

About high expectations

Posted Sat, 18 Feb 2006 22:17:00 GMT

I broke this site yesterday. In case it’s not obvious, it is based on Typo. I wanted to add the “recent headlines” section that is seen in the right panel (at the moment). Adding the feature was quite easy - I added a new static method in Articles, called recentArticles, created a new subview, called recentheadlines.rb, and changed the default layout for my theme to include that subview. Two sections in the CSS and it was done. Ruby/rails app seem quite easy to hack.

What was a bit more difficult, was deploying the site in the production environment. Mainly because Ruby on Rails was quite difficult for me to debug. In fact, not so difficult to debug the language itself, but Rails, and not because it’s not well written, but because it is not small, nor simple. In fact it’s pretty big. And I have not even started thinking about the more complex issues, such as transactionality, performance, conurrency, performance, etc… When dealing with J2EE, which I know well, things seem quite straightforward and easy. With Rails, at least for some time, it will be more difficult. When I learn it proper, I expect it will be just as easy.

The problem here is in the expectations, that the multitude of articles and the promises of the creators build up of Rails being something that you can just pick up in two lunch breaks. It’s a full-blown app framework and requires quite a lot of learning to leverage in full. As with most other things, the trivial tasks are just that - trivial, but the more specific problems can drive you crazy.

So unless somebody actually compares the investment required to perfect Rails with the one for J2EE, all the productivity claims are futile. After all, IBM claims that you can pull out someone from the street and get them writing J2EE apps in minutes, if only you use their Rational Application Developer.

Non-functional issues aside….

no comments

Older posts: 1 ... 9 10 11 12 13 14

Home

Who's George?

Recent entries