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.]
Popular Tutorials
generally an object contains 3 properties like
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
@Id
@Column(name=”USER_ID”)
@GeneratedValue(strategy=GenerationType.AUTO)
private long userId;
Different GenerationType Enum Properties….
@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
@Column(name=”USER_NAME”)
private String userName;
@Column– provides the name of the column in a table if it is different from the attribute name. (By default, the two names are assumed to be the same.)
More JPA Annotations for Class Field:
@Basic – The use of the Basic annotation is optional for persistent fields and properties of these types. If the Basic annotation is not specified for such a field or property, the default values of the Basic annotation will apply. Example-
private String userName;
@Transient – using when if you want skip any field of entity class to save in the database. Example-
@Transient
private String middleName;
@Embedded- using when if property or field of persistence class is Embeddable persistence class.
Example- class Address{ @Column(name=”STREET”)
@Embedded
private Address address;
@ElementColllection- Defines a collection of instances of a basic type or embeddable class. Must be specified if the collection is to be mapped by means of a collection table. Example-
@ElementCollection
private Collection<Address> lisOfAddresses = new ArrayList<Address>();
@Id- Specifies the primary key of an entity. Example-
@Id
private long userId;
@EmbeddedId- composite primary key of an embeddable class. Example-
@Embeddable
Class Address{
@EmbeddedId
private int addressId;
—-
—-
}
@Version- Specifies the version field or property of an entity class that serves as its optimistic lock value. The version is used to ensure integrity when performing the merge operation and for optimistic concurrency control. Example-
@Version
private int addressId;
@Temporal- This annotation must be specified for persistent fields or properties of type java.util.Date and java.util.Calendar. Example-
@Column(name=”JOIN_DATE”)
@Temporal(TemporalType.DATE)
private Date joinDate;
<<Previous Chapter 7<< >>Next Chapter 9>>