-->
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.  [ 14 posts ] 
Author Message
 Post subject: Hbm vs Annotations
PostPosted: Thu Jul 24, 2008 7:30 pm 
Newbie

Joined: Thu Jul 24, 2008 7:25 pm
Posts: 3
I prefer Annotations, and my coworker prefers hbm

I am curious if there is a list of the differences and benefits of one over the other?

I don't want to propose switching if it is just because I prefer annotations, without a more compelling reason.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 25, 2008 4:17 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Here's a little excerpt from my website. I think it addresses the hbm vs. annotations issue:

A Quick Discussion: Hibernate Annotations vs. Hibernate Mapping Files

Quote:
JPA annotations greatly simplify persistence programming with Hibernate, but to understand why they're so great, it helps to understand what we needed to do before the introduction of annotations.
Back to the Future: This Historical hibernate-mapping.xml File

Hibernate makes persisting the state of your Java objects incredibly simple. However, in order for Hibernate to know where to story your JavaBeans, or how to map the property of a JavaBean to a database column, the developer has to provide a bit of direction to the Hibernate framework. As such, people developing Hibernate based applications had to maintain an unweildly, monolithic mapping file that described how to save a given Java object to the database.

So, for example, if you had a class named Event that had three properties, one called id, one called birthday, and another property called title, you would have to add the following segment to a hibernate-mapping file:

<hibernate-mapping>

<class name="events.Event" table="EVENTS">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="birthday" type="timestamp"/>
<property name="title"/>
</class>

</hibernate-mapping>

What's wrong with an XML mapping file?

There is nothing inherently wrong, with a mapping file, and in fact, thousands of very salacious hibernate applications that are in production use an XML mappings file, but having a big XML mapping file presents a variety of non-lethal, but certainly annoying problems, including the following:

* information about the Java class must be maintained in an external file
* XML isn't always easy to write
* with lots of classes, the XML file can become unweildly and massive
* errors in one part of the XML file can ricochet all over your Java program

Anyways, Java 5 introducted a new Java based artifact - that annotation. Basically, an annotation allows you to add detail an information about a Java class, without damaging, disturbing or changing any of the code that is actually found inside a Java class or a Java method. So, instead of using a monolithic mappings file, Hibernate with JPA annotations allows you to completely rid applications of a mapping file, and instead, you can annotate your Java classes like so:

@Entity
public class Event {
private Long id;
private String title;
private Date date;
@Id
@GeneratedValue
public Long getId() { return id; }
private void setId(Long id) {this.id = id;}
public Date getDate() {return date;}
public void setDate(Date date) {this.date = date;}
public String getTitle() {return title;}
public void setTitle(String title) {this.title = title;}
}

The @Entity, @Id and @GeneratedValue tags you see in the Event class are the JPA annotations, and they replace the neeed to describe how to persist your Java classes in an external hibernate-mappings.xml file. Instead of using an external file, each Java class maintains its own mapping information, which is much more natural, and much easier to maintain on a class by class basis. Furthermore, it makes introducing new classes, or even removing persistent classes from your domain model, much much easier.

If you're using Hibernate, and you have the ability to choose between using annotations or using a hibernate-mapping file, well, it's really not much of a choice. Always use JPA annotations if you have a choice. JPA annotations are much easier to use, easy to maintain, and will help to make your Hibernate development experience a real pleasure. :)


A Quick Discussion: Hibernate Annotations vs. Hibernate Mapping Files

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 25, 2008 5:14 am 
Regular
Regular

Joined: Wed Apr 09, 2008 10:28 am
Posts: 52
I prefer Annotations too, i think they are great. You don't have to make XML File's for each of your Entitys which makes your project a lot more compact and personally i think it is less error-prone.

Maintenance can be done much more faster. I think if your using tools to generate your mappings you probably not see the advantage clearly but when you have to edit something "by hand" XML gets kinda messy.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 29, 2008 7:44 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Indeed...Annotations seem much more natural. One of the best Java language features to come out in a while. If you're using Java 5 or better, JPA annotations are definitely the way to go.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 29, 2008 1:03 pm 
Newbie

Joined: Thu Jul 24, 2008 7:25 pm
Posts: 3
I am trying to work on converting the classes.
Do you know if you can do them one at a time?

Also we have one base class that has

private long id

and then all of the subclasses extend that base class and map id differently in the xml.

I was planning on using annotations on the field, so how do I do that?

I assume it is some kind of override, but I am not sure how to override an Id?
And some of the classes use different generators.

I assume I would add
@AttributeOverride( name="id", column = @Column(name="audit_id") )


Here is the class
@Entity
@Table(name="audit")
public class Audit extends BaseObject {

}

<class name="Audit" table="audit">
<id name="id" type="long" column="audit">
<generator class="native" />
</id>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 29, 2008 5:10 pm 
Newbie

Joined: Sun Apr 27, 2008 3:30 pm
Posts: 7
Using hibernate mapping files is so much easier and cleaner in IntelliJ 7 with its Hibernate facade.


Top
 Profile  
 
 Post subject: Mapping file for hibernate in Intellij
PostPosted: Fri Mar 13, 2009 3:30 am 
Newbie

Joined: Sat Mar 07, 2009 5:20 am
Posts: 7
Hi,

I am new to Hibernate and Intellij. After loading Hibernate components, i have given Datasource and refreshed tables.

After genereating mapping.xml and java files with this datasource, i am getting java files and mapping.xml for 2 tables only. Actually there are 750 tables have been loaded.

What mistake i have done? Could you please help me out on this?

I am freezing on this. Thanks in advance

Sridhar


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 13, 2009 4:03 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
+1 to annotation.
By using annotation, you can reduce the amount of your files.

In annotation you cannot define the default value for the field unless you use columnDefinition, but by using columnDefinition, you should define the database data type for particular field. It can make your hibernate class depend on the database.


Top
 Profile  
 
 Post subject: Mapping files for hibernate in Intellij
PostPosted: Fri Mar 13, 2009 4:15 am 
Newbie

Joined: Sat Mar 07, 2009 5:20 am
Posts: 7
Hi SIau_Tie,

Thanks for your reply. But i did not get this properly. Where i have to change these things in Intellij to get the mapping java files of 750 tables.

Still i am getting the mapping files for 4 tables only.

Thanks for your help..


Top
 Profile  
 
 Post subject: Re: Mapping files for hibernate in Intellij
PostPosted: Fri Mar 13, 2009 4:28 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
sridhar1985 wrote:
Hi SIau_Tie,

Thanks for your reply. But i did not get this properly. Where i have to change these things in Intellij to get the mapping java files of 750 tables.

Still i am getting the mapping files for 4 tables only.

Thanks for your help..


Hi sridhar1985,
Sorry, I don't understand what you mean. I use Eclipse instead.

Code:
hbm.xml:
...
<property name="address">
   <column name="address" default="unknown"/>
</property>

annotation

@Column(name="address" , columnDefinition="varchar(100) default 'unknown'")
public String getAddress() {
   return address;
}


Top
 Profile  
 
 Post subject: Mapping files for hibernate in Intellij
PostPosted: Fri Mar 13, 2009 5:29 am 
Newbie

Joined: Sat Mar 07, 2009 5:20 am
Posts: 7
Hi,

Thanks for your reply. I am not using Eclipse. I am using IntelliJ. In Intellij there is option for getting mapping java files for our Database connection. But i am not getting java files for all tables in the Database connection. For few tables only i am getting.


Top
 Profile  
 
 Post subject: Re: Hbm vs Annotations
PostPosted: Sat May 19, 2012 3:26 am 
Newbie

Joined: Sat May 19, 2012 3:16 am
Posts: 1
I have a doubt that while using annotation we need to make change in the java file for any mapping change which actually should not be the case with hbm.xml file, then why annotation?


Top
 Profile  
 
 Post subject: Re: Hbm vs Annotations
PostPosted: Mon May 21, 2012 1:43 pm 
Newbie

Joined: Wed Jan 12, 2011 1:15 pm
Posts: 11
I prefer Annotations for several reasons:

- smaller number of files
- code more legible
- easier error´s code detection
- easier app´s error detection
- code more flexible exchange
- I write less code in the file
- I prefer java class to xml file

But really it's all about personal preferences and tastes.

Regards.


Top
 Profile  
 
 Post subject: Re: Hbm vs Annotations
PostPosted: Thu Jul 26, 2012 4:11 am 
Newbie

Joined: Thu Jul 26, 2012 4:04 am
Posts: 1
Hi all,

And what about column order? I have noticed that with annotations hibernate generate columns in alphabetical order, and with hbm.xml it generates them as they are line-by-line what is better in my opinion.

I admit that annotations are pretty cool and still - I am using it each day, but is there posibility to handle column order with annotations? :)

eNJac


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 14 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.