Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are people continuing to use xml mapping files instead of annotations? [closed]

People also ask

What is an XML mapping?

XML Mapping is defined as an editor used to map one or more XML Document to a common XML file by this, we can generate a transformation document after defining Mapping. Mapping does delete, editing or any persistent work.

Which of the following mapping are usually defined in an XML document?

Object/relational mappings are usually defined in an XML document. The mapping document is designed to be readable and hand-editable. The mapping language is Java-centric, meaning that mappings are constructed around persistent class declarations and not table declarations.

Which XML tag is used to configure column in mapping XML file?

The <property> element is used to map a Java class property to a column in the database table. The name attribute of the element refers to the property in the class and the column attribute refers to the column in the database table.


The domain layer and the persistence layer are considered by some to be separate concerns. Using the pure XML approach keeps the two layers as loosely coupled as possible; using annotations couples the two layers more tightly as you have persistence-related code embedded in the domain code.


  • Lack of overview of what's been mapped. You need to dig in the source code.

people don't know that annotations are a full-featured replacement of xml mapping.

Ah, but they're not. Three cases off the top of my head (there are probably more) you can't do (well) with annotations:

  1. Use formula as part of association key (admittedly, rather esoteric).
  2. Join-via-subselect - @Loader is not an adequate replacement. Not too common but quite useful. Envers provides a viable alternate approach.
  3. Losing column order for schema generation. This one's an absolute killer. I understand why it's done this way, but it still annoys me to no end.

Don't get me wrong, though - annotations are great; doubly so when they're coupled with Validator (though, again, #3 above kills the buzz on this one). They also provide certain aspects of functionality that XML mappings do not.


Using XML to complement the annotations, where environment or system specific configuration is needed.


Some information is carried nicely in annotations, such as the cardinality of relationships between entities. These annotations provide more detail about the model itself, rather than how the model relates to something else.

However, bindings, whether to a persistence store or XML or anything else, are extrinsic to the model. They change depending on the context in which the model is used. Including them in the model is as bad as using inline style definitions in HTML. I use external binding (usually—though not necessarily—XML) documents for the same reasons I reference an external CSS.


I initially found the annotation syntax very weird. It looks like line noise and mixes in with where I usually put comments. It's vastly better than dealing with the XML files though, because all of the changes are in one place, the model file. Perhaps one limitation of annotation is possible collision with other annotations, but I haven't seen that yet.

I think the real reason that it isn't used more is that it isn't really considered the default. You have to use an additional jar file. It should be part of core and the XML approach should be the optional one.


I've switched to annotations, but sometimes I miss the XML mappings, mainly because the documentation was so much more comprehensive, with examples of many scenarios. With annotations, I stick to pretty basic mappings (which is great if you control the data and object model), but I've done some very complex things in the XML that I don't know if I could replicate in the annotations.