This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Annotation or XML mapping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Annotation or XML mapping" Watch "Annotation or XML mapping" New topic
Author

Annotation or XML mapping

Nitin Gaurz
Greenhorn

Joined: Oct 09, 2007
Posts: 4
Probably this question has been asked before. Please give me reference if someone knows about the thread.

If domain class is annotated with meta data, it contains details about data model i.e. table and columns names. I can't pass this model to other layers because I don't want to expose my data model to rest of the code in application. To prevent this I have to create another java bean and populate it from annotated persistent object and return plain bean to other layers. This is additional work. Moreover, if I were to change any column name, I have to edit java source of annotated object.

So, why shall I use annotation over separate XML mapping files that do not have above issues?
In general, what are pros/cons of annotated metadata over XML mappings?

Any thoughts

Thanks in advance
-Nitin
Ashutosh Gawande
Greenhorn

Joined: Aug 19, 2009
Posts: 11
I can see where you are coming from but I don't think there is any harm in exposing them through annotations. There is no way to influence the data structure declared by annotations, so it’s perfectly safe. On other hand you will not be able to hide the data structure even by using the XMLs.

Creating another POJO for a POJO (domain object) just to hide annotations is I think is an overhead without significant gains.
If you change any column name and you are using XML’s then also you will need to edit your "XML source" so there is no "bad" thing by touching java source.

So, why shall I use annotation over separate XML mapping files that do not have above issues?
-- Well you have given the answer, it’s very easy to manage and understand them from domain point of view as the information you needed is already there.

Well at the moment I think there are no cons to it.
Nitin Gaurz
Greenhorn

Joined: Oct 09, 2007
Posts: 4
On other hand you will not be able to hide the data structure even by using the XMLs.

Other components won't be looking at the XML because they don't need to.

there is no "bad" thing by touching java source.

This actually brings up another debate. Earlier better coding practice was to put any configurable item out in XML or properties. With Java 5 annotation do the reverse of it.

Use of annotation gives another option to specify metadata and expert coders will love it. But I still think XML approach is more cleaner unless someone can point any real benefit of annotations other than just to eliminate the XMLs.

Thanks
-Nitin

Ashutosh Gawande
Greenhorn

Joined: Aug 19, 2009
Posts: 11
Other components won't be looking at the XML because they don't need to.
-- Well other components will be looking at your domain object only not the annotations (structure of them) because "they don't need to"

Can you give me example of how you think other layers can access (or exploit it) your structure in case of annotations?
I am probably not able to understand your concern when you mean exposing, but I am assuming it’s about reading your structure out. I think the only way to read out (which should not do any harm anyways) complete annotations on field-by-field class-by-class basis. Now do you think reading all domain objects annotation is easy or reading out an XML file with Java? i.e. any ways your structure can be accessed.

Earlier better coding practice was to put any configurable item out in XML or properties
-- Yes earlier it was like that, now it changed a bit because changing property is same as changing the code what is the difference between them?
Does that mean you should not have properties files .? NO... properties files should still be there to give you a ease of overriding the default behaviour mentioned in the code at instantiation time. If some property is never going to be changed then what’s the point in putting it into XML file or properties file instead of java class? And I think in this case once your table structure is final you will be extremely rarely change the column name, if that’s your main concern.

But I still think XML approach is more cleaner unless someone can point any real benefit of annotations other than just to eliminate the XMLs.
-- The number of different XML files in system are certainly reduced by annotations so I feel annotations are more cleaner because you know then and there how its is configured.

I think it boils down to choice between when browsing code, searching through code as well as xml files or just code. I prefer 2nd option.. There is no wrong or right here ..
And I am not on mission to eliminate XMLs .. , but I am open to new changes that can improve code readability and ease of configuration, if tomorrow this is achieved through XMLs I am with it.. but at the moment it seems annotations are doing good job in achieving that
Nitin Gaurz
Greenhorn

Joined: Oct 09, 2007
Posts: 4
By passing entities you yourself are providing them class references that can be read (Annotations can be read by apt) but not the XML. You do not pass on XML references.
Further questions:
Are these eligible to pass those to presentation layer? Can entities work as model in Spring MVC? For Struts, these have to converted into action forms?

I bought your other arguement regarding having configuration at one place for better readability
stanislav bashkirtsev
Ranch Hand

Joined: Aug 17, 2009
Posts: 75
Annotations make code dirty and hard-reading. I hate annotations.
Ashutosh Gawande
Greenhorn

Joined: Aug 19, 2009
Posts: 11
By passing entities you yourself are providing them class references
-- Annotations are read by reflection and not by instance/reference you can not get annotations with instances as these belong to class. As long as you know the class you can read annotations. Hence your other solution will not work either (i.e. copying values in to another pojo) as long as other layer knows the domain class name they can anyways read it.

You do not pass on XML references.
-- As mentioned above neither we pass annotations, But if some one wants to read and parse your xmls you will not be able to stop them. as long as they know your configuration file path

Are these eligible to pass those to presentation layer?
-- Yes those are pojos, that is why in fact those are pojos as after instantiation those can be passed around as vos

Can entities work as model in Spring MVC?
-- yes they can work and in fact are model regardless of you decided to use any or none of the mvc frameworks.
Christian Dillinger
Ranch Hand

Joined: Jul 20, 2009
Posts: 189
stanislav bashkirtsev wrote:Annotations make code dirty and hard-reading. I hate annotations.


Putting your metadata into xml-files that can be stored anywhere without even knowing, that they exist makes code more readable?!? They are a good way to make code clearer. And if they can be overwritten by xml they simplify testing.
stanislav bashkirtsev
Ranch Hand

Joined: Aug 17, 2009
Posts: 75
Putting your metadata into xml-files that can be stored anywhere without even knowing, that they exist
It all depends on conventions of the company: where you should put thing, how should it be named and what view should it have. And one more: if you want to look at hibernate side of the class, you can look only at xml, if you want to see class, you can look only at class. And nothing is mixed.
Annotations is a good thing in testing, but putting them everywhere is a bad habit.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Annotation or XML mapping