Annotations vs. XML
Since Java adopted annotations, I’ve heard a lot of discussion around whether to prefer annotations over external configuration files (often XML) and vice versa.
I think the decision boils down to two criteria:
1) Can annotations simplify the metadata?
2) Can changes to the metadata break behavior in your application?
If annotations do not reduce the amount of metadata that you have to provide (in most cases they do), then you shouldn’t use annotation.
For example, Hibernate mappings are often in XML, but annotations often provide the ability to specify the same mappings with significantly less metadata. Any changes you make to your mappings (whether in XML or annotations) could potentially be behavior breaking. You’re not going to change your mappings dynamically at run-time, are you? So, annotations seem like a much better choice.
[Note: I might consider using XML metadata for Hibernate when mapping to a legacy database because the annotations can often become bulky. Also, you are forced to use XML if you want to map the classes against two different databases with different schema. I haven't heard of a way to specify two mutually exclusive sets of Hibernate annotations on my classes. Even if this did exist it would be complex, which violates my first criterion for selecting annotations over XML.]
- Mapping file is the heart of hibernate application.
- Every ORM tool needs this mapping, mapping is the mechanism of placing an object properties into column’s of a table.
- Mapping can be given to an ORM tool either in the form of an XML or in the form of the annotations.
- The mapping file contains mapping from a pojo class name to a table name and pojo class variable names to table column names.
- While writing an hibernate application, we can construct one or more mapping files, mean a hibernate application can contain any number of mapping files.
- Identity (Object Name)
- State (Object values)
- Behavior (Object Methods)
Now we will look how to relate XML Mapping to Annotation
Mapping a class UserDetails to Table USER_DETAIL in XML --
Now Mapping a class UserDetails to Table USER_DETAIL in Annotation --
@Entitydeclares the class as an entity (i.e. a persistent POJO class)
@Tableis set at the class level; it allows you to define the table, catalog, and schema names for your entity mapping. If no
@Tableis defined the default values are used: the unqualified class name of the entity.
Mapping primary key USER_ID of table to property userId of class UserDetails in XML
Mapping primary key USER_ID of table to property userId of class UserDetails in Annotation
@Iddeclares the identifier property of this entity. The class UserDetails is mapped to the USER_TABLE table, using the column USER_ID as its primary key column.
The column(s) used for a property mapping can be defined using the
@Columnannotation. Use it to override default values .
Id Generator Class Mapping in XML
Id Generator Class Mapping in Annotation
@GeneratedValue Provides for the specification of generation strategies for the values of primary keys.
Enum GenerationType Defines the types of primary key generation strategies.
Mapping Column to the property of class in XML
Mapping Column to the property of class in Annotation
<<Previous Chapter 7<< >>Next Chapter 9>>