Annotations vs. XML
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.
generally an object contains 3 properties like
- Identity (Object Name)
- State (Object values)
- Behavior (Object Methods)
But while storing an object into the database, we need to store only the values(State) right ? but how to avoid identity, behavior.. its not possible. In order to inform what value of an object has to be stored in what column of the table, will be taking care by the mapping, actually mapping can be done using 2 ways,
Actually annotations are introduced into java from JDK 1.5
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 —
here @Entity declares the class as an entity (i.e. a persistent POJO class)
@Table is set at the class level; it allows you to define the table, catalog, and schema names for your entity mapping. If no @Table is 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
here @Id declares 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 @Column annotation. 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