File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Mappings vs Annotations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Mappings vs Annotations" Watch "Mappings vs Annotations" New topic

Mappings vs Annotations

Jonathan Huang
Ranch Hand

Joined: Jun 23, 2006
Posts: 35
Is there any significant improvement in SessionFactory Startup in using annotations over mappings?

I know there are a lot of factors that effect startup time such as complexity and length of the mapping, but because hibernate doesn't have to read in XML to map with annotations, I would guess that annotations are quicker overall.

Is this a false statement? Im trying to do some tests to find out, but because of WTP 0.7 and HibTools, im having difficulty right now. Just thought I'd ask the community.

Thanks in advance!
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

I don't think there is much of a difference in speed. I think it is more of whether you want to manually make seperate XML mapping files, or include that with Annotations. Annotations use Inspection of the Java classes to make Objects that get created at startup, where of course XML uses an IO Reader to load the XML and create the objects.


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
Jonathan Huang
Ranch Hand

Joined: Jun 23, 2006
Posts: 35
I did some tests, (finally got WTP and hibtools working).

The more mapping files you have the time it takes to startup is linear. Depending on the size and complexity of your mapping files.

In my tests I used 2 mappings. A small mapping file (25 lines), then a large one (125 lines). They weren't complex, just property tags mostly.

My annotated class was a rewrite of the small mapping file. I'd show you the code, but I am not permited to do so. Just think of them as <id> and <property> tags. No sets or collections.

As you can see below the mappings take a bit longer, but not by very much. But their times are a more steep startup.

Large Mappings:
Loading 1 file averaged 1850 millis
Loading 5 files averaged 2150 millis
Loading 10 files averaged 2550 millis
Loading 20 files averaged 3200 millis

Annotated Class:
Loading 1 file:
1) 2172 millis
2) 1906 millis
3) 1875 millis
4) 1750 millis
5) 2005 millis

Loading 5 files:
1) 1874 millis
2) 1922 millis
3) 2172 millis
4) 1905 millis
5) 2020 millis

Loading 10 files:
1) 2625 millis
2) 1938 millis
3) 1937 millis
4) 1937 millis
5) 2101 millis

Loading 20 files:
1) 2266 millis
2) 2093 millis
3) 2156 millis
4) 2109 millis
5) 2102 millis
[ July 24, 2006: Message edited by: Jonathan Huang ]
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

Thanks for the information about startup, it is good information to know.

[ July 28, 2006: Message edited by: Mark Spritzler ]
Venkat Srinivas

Joined: Aug 02, 2006
Posts: 5
Annotations and Mappings both have their own disadvantages

annotations when the table names and field names have to be changed need

recompilation...when Mappings don't need that...
I agree. Here's the link:
subject: Mappings vs Annotations
It's not a secret anymore!