<div dir="ltr" style="text-align: justify;">
<h2 class="title">Annotations vs. XML</h2>
<div class="title">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.</div>
<p>I think the decision boils down to two criteria:<br />
1) Can annotations simplify the metadata?<br />
2) Can changes to the metadata break behavior in your application?<br />
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.</p>
<div id="ads-id" align="center"></div>
<p>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.</p>
<p>[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&#8217;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.]</p>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;">
<p><b>Popular Tutorials</b></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/spring-tutorial/"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/spring-web-mvc-framework-chapter-38.html"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/2016/06/introduction-to-spring-boot-a-spring-boot-complete-guide.html"><em><strong>Spring Boot Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/2013/02/spring-security-take-baby-step-to-secure.html"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint.html"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/using-spring-jdbc-framework-chapter-32.html"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/2017/01/spring-hateoas-hypermedia-driven-restful-web-service.html"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/2017/01/microservices-with-spring-boot.html"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/2013/06/jax-rs-web-service-tutorial.html"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/2013/01/core-java-baby-step-to-be-best-java-ian.html"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/2012/03/hibernate-3-on-baby-steps.html"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/spring-batch-process-with-example.html"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<h2>Mapping:</h2>
<ul>
<li>Mapping file is the heart of hibernate application.</li>
<li>Every ORM tool needs this mapping, mapping is the mechanism of placing an object properties into column’s of a table.</li>
<li>Mapping can be given to an ORM tool either in the form of an XML or in the form of the annotations.</li>
<li>The mapping file contains mapping from a pojo class name to a table name and pojo class variable names to table column names.</li>
<li>While writing an hibernate application, we can construct one or more mapping files, mean a hibernate application can contain any number of mapping files.</li>
</ul>
<p>generally an object contains 3 properties like</p>
<ul>
<li>Identity (Object Name)</li>
<li>State (Object values)</li>
<li>Behavior (Object Methods)</li>
</ul>
<p>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,</p>
<ul>
<li>XML</li>
<li>Annotations.</li>
</ul>
<p>Actually annotations are introduced into java from JDK 1.5</p>
<p><b style="color: blue;">Now we will look how to relate XML Mapping to Annotation</b><br />
<b> </b>Mapping a class <b>UserDetails </b>to Table <b>USER_DETAIL</b><b> in XML &#8212; </b></p>
<div style="background-color: #ff99cc; border-width: thin; border: solid;"><;class name=&#8221;com.sdnext.hibernate.tutorial.dto.UserDetails&#8221; table=&#8221;USER_DETAIL&#8221;>;</div>
<p>Now Mapping a class <b>UserDetails </b>to Table <b>USER_DETAIL</b><b> in Annotation &#8212; </b></p>
<div style="background-color: #ff99cc; border-width: thin; border: solid;">@Entity<br />
@Table (name=&#8221;USER_DETAIL&#8221;)<br />
public class UserDetails{}</div>
<p>here<b> @Entity</b> declares the class as an entity (i.e. a persistent POJO class)<br />
<b>@Table</b> is set at the class level; it allows you to define the table, catalog, and schema names for your entity mapping. If no <b>@Table</b> is defined the default values are used: the unqualified class name of the entity.</p>
<p>Mapping <b>primary key</b> <b>USER_ID</b> of table to <b>property userId</b> of class <b>UserDetails in XML</b></p>
<div style="background-color: #ff99cc; border-width: thin; border: solid;"><b> <;id name=&#8221;userId&#8221; type=&#8221;long&#8221; column=&#8221;USER_ID&#8221; >;</b></div>
<p>Mapping <b>primary key USER_</b><b>ID</b> of table to <b>property userId</b> of class <b>UserDetails in Annotation</b></p>
<div style="background-color: #ff99cc; border-width: thin; border: solid;">@Entity<br />
@Table (name=&#8221;USER_DETAILS&#8221;)<br />
public class UserDetails<br />
{<br />
@Id<br />
@Column(name=&#8221;USER_ID&#8221;)<br />
private long userId;<br />
<b>}</b><b><br />
</b></div>
<p>here <b>@Id</b> declares the identifier property of this entity. The class <b>UserDetails </b>is mapped to the <b>USER_TABLE</b> table, using the column <b>USER_ID</b> as its primary key column.<br />
The column(s) used for a property mapping can be defined using the <b>@Column</b> annotation. Use it to override default values .</p>
<p><b>Id </b>Generator Class<b> Mapping </b>in <b>XML</b></p>
<div style="background-color: #ff99cc; border-width: thin; border: solid;"><;id name=&#8221;userId&#8221; type=&#8221;long&#8221; column=&#8221;USER_ID&#8221; >;<br />
<;generator class=&#8221;auto&#8221;/>;<br />
<;/id>;<b> </b></div>
<p><b>Id </b>Generator Class <b>Mapping </b>in <b>Annotation</b></p>
<div style="background-color: #ff99cc; border-width: thin; border: solid;">
<p><b> </b>@Id<br />
@Column(name=&#8221;USER_ID&#8221;)<br />
@GeneratedValue(strategy=GenerationType.AUTO)<br />
private long userId;</p>
<p>Different GenerationType Enum Properties&#8230;.</p>
<div class="itemizedlist">
<ul>
<li>AUTO &#8211; either identity column, sequence or table depending on the underlying DB</li>
<li>TABLE &#8211; table holding the id</li>
<li>IDENTITY &#8211; identity column</li>
<li>SEQUENCE &#8211; sequence</li>
<li>identity copy &#8211; the identity is copied from another entity</li>
</ul>
</div>
<p> ;</p>
</div>
<p><b>@GeneratedValue </b>Provides for the specification of generation strategies for the values of primary keys.<br />
<b style="color: orange;">Enum GenerationType</b> Defines the types of primary key generation strategies.</p>
<p>Mapping <b>Column </b>to the <b>property </b>of class in <b>XML</b></p>
<div style="background-color: #ff99cc; border-width: thin; border: solid;"><b> </b><;property name=&#8221;userName&#8221; column=&#8221;USER_NAME&#8221;>;<b> </b></div>
<p>Mapping <b>Column </b>to the <b>property </b>of class in <b>Annotation</b></p>
<div style="background-color: #ff99cc; border-width: thin; border: solid;">
<p><b>@Column(name=&#8221;USER_NAME&#8221;)<br style="color: black;" /> private String userName;</b></p>
<p><b style="color: red;">@Column</b>&#8211; 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.)<br />
<b>More JPA Annotations for Class Field:</b><br />
<b>@Basic &#8211; </b>The use of the <b>Basic </b>annotation is optional for persistent fields and properties of these types. If the <b> Basic </b>annotation is not specified for such a field or property, the default values of the <b>Basic </b>annotation will apply. Example-</p>
<div style="color: black;"><b>@Basic</b></div>
<p><b>private String userName; </b></p>
<p><b style="color: red;">@Transient</b> &#8211; using when if you want skip any field of entity class to save in the database. Example-<br />
<b>@</b><b>Transient</b><br />
<b>private String middleName;</b></p>
<p><b style="color: red;">@Embedded- </b>using when if property or field of persistence class is Embeddable persistence class.<br />
Example- <b>class Address{ @Column(name=&#8221;STREET&#8221;)</b></p>
<div class="separator" style="clear: both; text-align: center;"></div>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2017/04/AddressClass.png" border="0" /></div>
<div class="separator" style="clear: both; text-align: center;"></div>
<div class="separator" style="clear: both; text-align: center;"></div>
<div class="separator" style="clear: both; text-align: center;"></div>
<p><b> @Embedded</b><br />
<b> private Address address; </b></p>
<p><b>@ElementColllection- </b>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-<br />
<b>@ElementCollection</b><br />
<b>private Collection<;Address>; lisOfAddresses = new ArrayList<;Address>;(); </b></p>
<p><b>@Id- </b>Specifies the primary key of an entity. Example-<br />
<b> @Id</b><br />
<b>private long userId;</b></p>
<p><b>@EmbeddedId- </b>composite primary key of an embeddable class.<b> Example-</b><br />
<b>@Embeddable</b><br />
<b>Class Address{</b><br />
<b> @EmbeddedId</b><br />
<b> private int addressId;</b><br />
<b> &#8212;-</b><br />
<b> &#8212;-</b><br />
<b> }</b></p>
<p><b style="color: red;">@Version- </b>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-<br />
<b> @Version</b><br />
<b>private int addressId;</b></p>
<p><b>@Temporal- </b>This annotation must be specified for persistent fields or properties of type <b>java.util.Date</b> and <b>java.util.Calendar</b>. Example-<br />
<b> @Column(name=&#8221;JOIN_DATE&#8221;)<br />
@Temporal(TemporalType.DATE)<br />
private Date joinDate;</b><br />
<b> </b><br />
<b></b></p>
</div>
<p><b><br />
</b> <b><br />
</b> <b> <;<;<a href="https://dineshonjava.com/p/chapter-6-understanding-hibernate-or.html">Previous Chapter 7</a><;<; >;>;N<a href="https://dineshonjava.com/p/hbm2ddl-configuration-and-name.html">ext Chapter 9</a>>;>;</b><br />
<b><br />
</b> <b><br />
</b> <b><br />
</b> <b><br />
</b></p>
</div>