-->
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.  [ 4 posts ] 
Author Message
 Post subject: hbm.xml vs annotations
PostPosted: Wed Jul 23, 2008 10:28 am 
Newbie

Joined: Fri Apr 04, 2008 9:12 am
Posts: 5
Is there a "rule of thumb" on when to use hbm.xml files vs when to use annotations? I can see good arguments for each approach. Is it strictly a matter of user preference?

Thanks,

Dave Oppenheim


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 24, 2008 12:04 pm 
Newbie

Joined: Thu Mar 17, 2005 2:06 pm
Posts: 12
It is strictly a matter of user preference.

However, this is often influenced by the tools you are using. If you are using a tool to reverse engineer your database, like middlegen, it will likely create hbm's for you.

If you are using a tool that analyzes your java objects and creates your sql and hbms, like http://www.javatosql.com, then you might not need to create /edit either annotations or hbm's.

So it is up to you, as a developer, to decide how much control you need vs. how much time you really want to spend perfecting your database.


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

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
You might find this information, from my website, to be interesting on the topic:


A Quick Discussion: What is Hibernate and Annotations? and How Does it Work?

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:

Code:
<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:

Code:
@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: What is Hibernate and Annotations? and How Does it Work?

_________________
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: Re: hbm.xml vs annotations
PostPosted: Tue Jun 09, 2009 9:40 am 
Newbie

Joined: Fri Apr 10, 2009 3:53 pm
Posts: 6
I liked the idea of annotations but I am running into some road blocks (example: a ManyToMany mapping when you want to delete the non owning side). I am finding there are much less books/tutorials/reference materials for annotations. If you stick to configuration files, there is a wealth of knowledge out there to lean on. So if you are like me and tend to struggle a bit more than others with new concepts, it seems with configuration files you have more opportunity to teach yourself.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.