-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Annotations vs XML vs XDoclet
PostPosted: Tue Feb 24, 2009 9:14 am 
Newbie

Joined: Tue Feb 24, 2009 9:03 am
Posts: 4
Hello,

I am new to Hibernate. I need to develop an application backed by a relational database, and having experienced the headaches of managing this directly with SQL, it seemed this time I should look for an easier way. That is what led me to Hibernate.

It seems there are several ways to specify how Java objects map to tables in the DB: via Annotations in the Java code, via XML files in parallel to the Java source, and via Javadoc annotations (XDoclet) in the source comments. I would prefer to have my mappings stored directly in the Java source files. I lean towards Annotations for aesthetic reasons.

First, can anybody make an argument why any one of these approaches is better than the others?

Second, it isn't clear to me why the XML approach seems to dominate all the documentation and forum posts. I suppose this is more of a legacy thing than anything.

Third, does anybody know of a well-documented example using Annotations? I have found (and attempted to read, before my brain got all melty) several sets of documentation as found in the first few pages of Google results. Some of them are even hosted on hibernate.org. But they all use fairly cryptic or unclear language about the structure of the database.

I spent most of the day yesterday trying to get Hibernate to behave as I expect it should, but there is either something wrong with Hibernate (possible), or with my perception of how it works (more likely). I'd like to figure that out on my own though, but I need a pointer to some better documentation (especially wrt Annotation syntax and semantics).


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 10:08 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Well, I also prefer annotations. One reason is, that most applications, which are using the JPA-standard, are using annotations. Also I do not like too much documents, so annotations are placed directly in the source code.

One argument to use xml is, that you seperate your source from the mapping information. So you do not have to change your code, if your mapping changes, for example when changing the underlying database.

I guess, xml dominates this forum is, as you said for legacy reasons and because the hibernate reference guide is also using xml.

Have you taken a look at the annotation reference for examples? There you'll find a tutorial and many examples.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 11:51 am 
Newbie

Joined: Tue Feb 24, 2009 9:03 am
Posts: 4
Thanks for the quick reply.

I did see that annotations reference guide, and that is actually the one that I found to be lacking. There are way too many details it skips over (like, what is the relevant part of the database schema?).

I am still trying to figure out if Hibernate can do what I need it to do. Part of what I need is for it to be relatively transparent. I don't want to kill too many brain cells on things that should be simple.

For example, I can't seem to get Hibernate to respect my primary key auto_increment property. I have a test class, Person. In my bean I define the identifier thusly:

Code:
  @Id
  @GeneratedValue
  @Column(name="personId")
  private Integer id;


My test application simply creates a Person object, and saves it using session.save(..). My database table explicitly sets the primary key to automatically increment:

Code:
create table Person (
  `personId` int(11) NOT NULL AUTO_INCREMENT,
  ...,
  PRIMARY KEY (`personId`),
)


If I manually issue SQL to add Person rows (insert into Person;) it does so without complaint, and assigns a new row with a new, unique, incremented primary key in the personId slot.

But when I run my Hibernate test app several times it ignores the auto_increment, and just overwrites whatever was there. It always creates a Person with personId=1.

I can't understand why this is the default behavior, or what I am doing wrong.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 12:08 pm 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
I guess it is because @GeneratedValue does not work with a database-generated value. You could either remove the database-id-generator or remove @GeneratedValue (or try another generator strategy).

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 2:29 pm 
Newbie

Joined: Tue Feb 24, 2009 9:03 am
Posts: 4
I discovered the problem. I was using a config file off the web in order to quickly get things working. Unfortunately this Hibernate config file had the following line in it:

Code:
<property name="hibernate.hbm2ddl.auto">create</property>


This meant the schema analyzer would re-build the database each time. I just commented that out and it started working as expected.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.