wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Why we are using annotations that already we have an xml configuration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Why we are using annotations that already we have an xml configuration" Watch "Why we are using annotations that already we have an xml configuration" New topic
Author

Why we are using annotations that already we have an xml configuration

Snehitha Prasad
Ranch Hand

Joined: Jul 06, 2012
Posts: 32
Hi ,

Can someone explain what is the purpose of annotations in hibernate.I know that which is another style of configuration.But already we have an xml type configuration and we know that which one is more flexible than annotation.
I requested you to please tell me the when to use annotation and when to use xml file configuration,which one is more reliable in which type of scenario's.

Thanks in advance,
SNEHITHAPRASAD
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

For most use cases it is personal preference. I prefer annotations on my new projects. I do think consistency is good though so I will continue with XML when working on projects that are already using it. I would learn both approaches before making a decision, otherwise your judgement may just be based on the one you are more familiar and comfortable with. In the end they are two means to the same end. In some cases you may find some obscure thing that can be done in XML that does not have an annotation equivalent yet, but most of the time either can be used.


[How To Ask Questions][Read before you PM me]
Snehitha Prasad
Ranch Hand

Joined: Jul 06, 2012
Posts: 32
Hi Bill Gorder,

Thanks for your response.But my doubt is not that.If we use Annotations then in case in future if we want to change the code(like for example database change) then we need to change in the class itself.In this process we should recompile the code and redeployment is necessary for effect those changes.If we use the xml configuration then no need to recompilation and redeployment is required.So, if you observe this concept xml is better.
Finally what my doubt is if we already have an xml configuration, why they invented the concept of annotation.Is there any other features it provides rather than xml.

I requested you to give me a brief clarification on this.

Thanks in advance,
SNEHITHAPRASAD
Nam Ha Minh
Ranch Hand

Joined: Oct 31, 2011
Posts: 498

I agree with Bill Gorder that XML or annotation is just personal preference. Each has its own its advantage and drawback.
In my opinion, changes for XML or annotation happens mostly at development time, so the re-compilation does not matter. In production, it's rarely have to change annotation or XML. With annotation, one can make changes quickly, but he may need to dive into a heavy XML file to find what needs to be updated.


PM Me If You Want to get Urgent Help on Java Programming
Snehitha Prasad
Ranch Hand

Joined: Jul 06, 2012
Posts: 32
Hi,

Thank you.Thank you for your quick response.

SNEHITHAPRASAD
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

Even if it is in XML they are typically stored inside the war or jar so you still have to go through the effort of cracking it open and making changes. My thought is if you are making changes to your relationships there is probably coding changes that go along with it. Also changes like this should be tested not done in this manner in production, so I think re-building is not an issue. Also those XML files are loaded on application startup so for your changes to be reflected you would need to restart the app regardless.

Typically mapping files are named by the entities they map. This is just one more reason I like annotations on the entity it seems like a natural place to define and look for this type of meta data. Also annotations are self documenting and they provide compile-time checking. The less I have to wade through thousands of lines of XML the happier I am. XML still has its uses primarily when you don't have control over the object you are mapping, perhaps because it is coming out of a 3rd party library. Also there are rare cases where something is only supported in XML and has not made its way into annotations yet, but I do not run into that often. I think its safe to say that many are moving towards annotations these days so if you are not familiar with it I would learn it, because its definitely not going away. What I said before still applies they are 2 means to the same end and either one will get the job done, and it really ends up coming down to personal preference.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why we are using annotations that already we have an xml configuration