Annotations vs Configuration in Hibernate

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<h2 class&equals;"title">Annotations vs&period; XML<&sol;h2>&NewLine;<div class&equals;"title">Since Java adopted annotations&comma; I’ve heard a lot of discussion around whether to prefer annotations over external configuration files &lpar;often XML&rpar; and vice versa&period;<&sol;div>&NewLine;<p>I think the decision boils down to two criteria&colon;<br &sol;>&NewLine;1&rpar; Can annotations simplify the metadata&quest;<br &sol;>&NewLine;2&rpar; Can changes to the metadata break behavior in your application&quest;<br &sol;>&NewLine;If annotations do not reduce the amount of metadata that you have to provide &lpar;in most cases they do&rpar;&comma; then you shouldn’t use annotation&period;<&sol;p>&NewLine;<div id&equals;"ads-id" align&equals;"center"><&sol;div>&NewLine;<p>For example&comma; Hibernate mappings are often in XML&comma; but annotations often provide the ability to specify the same mappings with significantly less metadata&period; Any changes you make to your mappings &lpar;whether in XML or annotations&rpar; could potentially be behavior breaking&period; You’re not going to change your mappings dynamically at run-time&comma; are you&quest; So&comma; annotations seem like a much better choice&period;<&sol;p>&NewLine;<p>&lbrack;Note&colon; I might consider using XML metadata for Hibernate when mapping to a legacy database because the annotations can often become bulky&period; Also&comma; you are forced to use XML if you want to map the classes against two different databases with different schema&period; I haven&&num;8217&semi;t heard of a way to specify two mutually exclusive sets of Hibernate annotations on my classes&period; Even if this did exist it would be complex&comma; which violates my first criterion for selecting annotations over XML&period;&rsqb;<&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border&colon; 1px solid &num;c9e6f2&semi; border-radius&colon; 3px&semi; padding&colon; 16px&semi; line-height&colon; 1&period;45&semi;">&NewLine;<p><b>Popular Tutorials<&sol;b><&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-tutorial&sol;"><em><strong>Spring Tutorial<&sol;strong> <&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2012&sol;12&sol;spring-web-mvc-framework-chapter-38&period;html"><strong><em>Spring MVC Web Tutorial <&sol;em><&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2016&sol;06&sol;introduction-to-spring-boot-a-spring-boot-complete-guide&period;html"><em><strong>Spring Boot Tutorial<&sol;strong> <&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2013&sol;02&sol;spring-security-take-baby-step-to-secure&period;html"><em>Spring Security Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2017&sol;03&sol;spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint&period;html"><em>Spring AOP Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2012&sol;12&sol;using-spring-jdbc-framework-chapter-32&period;html"><em>Spring JDBC Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2017&sol;01&sol;spring-hateoas-hypermedia-driven-restful-web-service&period;html"><em><strong>Spring HATEOAS <&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2017&sol;01&sol;microservices-with-spring-boot&period;html"><em><strong>Microservices with Spring Boot<&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2013&sol;06&sol;jax-rs-web-service-tutorial&period;html"><strong><em>REST Webservice<&sol;em> <&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2013&sol;01&sol;core-java-baby-step-to-be-best-java-ian&period;html"><em><strong>Core Java <&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2012&sol;03&sol;hibernate-3-on-baby-steps&period;html"><em><strong>Hibernate Tutorial<&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2012&sol;12&sol;spring-batch-process-with-example&period;html"><strong><em>Spring Batch<&sol;em> <&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;div>&NewLine;<h2>Mapping&colon;<&sol;h2>&NewLine;<ul>&NewLine;<li>Mapping file is the heart of hibernate application&period;<&sol;li>&NewLine;<li>Every ORM tool needs this mapping&comma; mapping is the mechanism of placing an object properties into column’s of a table&period;<&sol;li>&NewLine;<li>Mapping can be given to an ORM tool either in the form of an XML or in the form of the annotations&period;<&sol;li>&NewLine;<li>The mapping file contains mapping from a pojo class name to a table name and pojo class variable names to table column names&period;<&sol;li>&NewLine;<li>While writing an hibernate application&comma; we can construct one or more mapping files&comma; mean a hibernate application can contain any number of  mapping files&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>generally an object contains 3 properties like<&sol;p>&NewLine;<ul>&NewLine;<li>Identity &lpar;Object Name&rpar;<&sol;li>&NewLine;<li>State &lpar;Object values&rpar;<&sol;li>&NewLine;<li>Behavior &lpar;Object Methods&rpar;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>But while storing an object into the database&comma; we need to store only the values&lpar;State&rpar; right &quest; but how to avoid identity&comma; behavior&period;&period; its not possible&period; In order to inform what value of an object has to be stored in what column of the table&comma; will be taking care by the mapping&comma;  actually mapping can be done using 2 ways&comma;<&sol;p>&NewLine;<ul>&NewLine;<li>XML<&sol;li>&NewLine;<li>Annotations&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>Actually annotations are introduced into java from JDK 1&period;5<&sol;p>&NewLine;<p><b style&equals;"color&colon; blue&semi;">Now we will look how to relate  XML Mapping to Annotation<&sol;b><br &sol;>&NewLine;<b> <&sol;b>Mapping a class <b>UserDetails <&sol;b>to Table <b>USER&lowbar;DETAIL<&sol;b><b> in XML &&num;8212&semi; <&sol;b><&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;ff99cc&semi; border-width&colon; thin&semi; border&colon; solid&semi;">&lt&semi;class name&equals;&&num;8221&semi;com&period;sdnext&period;hibernate&period;tutorial&period;dto&period;UserDetails&&num;8221&semi; table&equals;&&num;8221&semi;USER&lowbar;DETAIL&&num;8221&semi;&gt&semi;<&sol;div>&NewLine;<p>Now Mapping a class <b>UserDetails <&sol;b>to Table <b>USER&lowbar;DETAIL<&sol;b><b> in Annotation &&num;8212&semi; <&sol;b><&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;ff99cc&semi; border-width&colon; thin&semi; border&colon; solid&semi;">&commat;Entity<br &sol;>&NewLine;&commat;Table &lpar;name&equals;&&num;8221&semi;USER&lowbar;DETAIL&&num;8221&semi;&rpar;<br &sol;>&NewLine;public class UserDetails&lbrace;&rcub;<&sol;div>&NewLine;<p>here<b> &commat;Entity<&sol;b> declares the class as an entity &lpar;i&period;e&period; a persistent POJO class&rpar;<br &sol;>&NewLine;<b>&commat;Table<&sol;b> is set at the class level&semi; it allows you to define the table&comma; catalog&comma; and schema names for your entity mapping&period; If no <b>&commat;Table<&sol;b> is defined the default values are used&colon; the unqualified class name of the entity&period;<&sol;p>&NewLine;<p>Mapping <b>primary key<&sol;b> <b>USER&lowbar;ID<&sol;b> of table to <b>property userId<&sol;b> of class <b>UserDetails in XML<&sol;b><&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;ff99cc&semi; border-width&colon; thin&semi; border&colon; solid&semi;"><b> &lt&semi;id name&equals;&&num;8221&semi;userId&&num;8221&semi; type&equals;&&num;8221&semi;long&&num;8221&semi; column&equals;&&num;8221&semi;USER&lowbar;ID&&num;8221&semi; &gt&semi;<&sol;b><&sol;div>&NewLine;<p>Mapping <b>primary key USER&lowbar;<&sol;b><b>ID<&sol;b> of table to <b>property userId<&sol;b> of class <b>UserDetails in Annotation<&sol;b><&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;ff99cc&semi; border-width&colon; thin&semi; border&colon; solid&semi;">&commat;Entity<br &sol;>&NewLine;&commat;Table &lpar;name&equals;&&num;8221&semi;USER&lowbar;DETAILS&&num;8221&semi;&rpar;<br &sol;>&NewLine;public class UserDetails<br &sol;>&NewLine;&lbrace;<br &sol;>&NewLine;&commat;Id<br &sol;>&NewLine;&commat;Column&lpar;name&equals;&&num;8221&semi;USER&lowbar;ID&&num;8221&semi;&rpar;<br &sol;>&NewLine;private long userId&semi;<br &sol;>&NewLine;<b>&rcub;<&sol;b><b><br &sol;>&NewLine;<&sol;b><&sol;div>&NewLine;<p>here <b>&commat;Id<&sol;b> declares the identifier property of this entity&period; The class <b>UserDetails <&sol;b>is mapped to the <b>USER&lowbar;TABLE<&sol;b> table&comma; using the column <b>USER&lowbar;ID<&sol;b> as its primary key column&period;<br &sol;>&NewLine;The column&lpar;s&rpar; used for a property mapping can be defined using the <b>&commat;Column<&sol;b> annotation&period; Use it to override default values &period;<&sol;p>&NewLine;<p><b>Id <&sol;b>Generator Class<b> Mapping <&sol;b>in <b>XML<&sol;b><&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;ff99cc&semi; border-width&colon; thin&semi; border&colon; solid&semi;">&lt&semi;id name&equals;&&num;8221&semi;userId&&num;8221&semi; type&equals;&&num;8221&semi;long&&num;8221&semi; column&equals;&&num;8221&semi;USER&lowbar;ID&&num;8221&semi; &gt&semi;<br &sol;>&NewLine;&lt&semi;generator class&equals;&&num;8221&semi;auto&&num;8221&semi;&sol;&gt&semi;<br &sol;>&NewLine;&lt&semi;&sol;id&gt&semi;<b> <&sol;b><&sol;div>&NewLine;<p><b>Id <&sol;b>Generator Class <b>Mapping <&sol;b>in <b>Annotation<&sol;b><&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;ff99cc&semi; border-width&colon; thin&semi; border&colon; solid&semi;">&NewLine;<p><b>    <&sol;b>&commat;Id<br &sol;>&NewLine;&commat;Column&lpar;name&equals;&&num;8221&semi;USER&lowbar;ID&&num;8221&semi;&rpar;<br &sol;>&NewLine;&commat;GeneratedValue&lpar;strategy&equals;GenerationType&period;AUTO&rpar;<br &sol;>&NewLine;private long  userId&semi;<&sol;p>&NewLine;<p>Different GenerationType Enum Properties&&num;8230&semi;&period;<&sol;p>&NewLine;<div class&equals;"itemizedlist">&NewLine;<ul>&NewLine;<li>AUTO &&num;8211&semi; either identity column&comma; sequence or table depending on the underlying DB<&sol;li>&NewLine;<li>TABLE &&num;8211&semi; table holding the id<&sol;li>&NewLine;<li>IDENTITY &&num;8211&semi; identity column<&sol;li>&NewLine;<li>SEQUENCE &&num;8211&semi; sequence<&sol;li>&NewLine;<li>identity copy &&num;8211&semi; the identity is copied from another entity<&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<&sol;div>&NewLine;<p><b>&commat;GeneratedValue <&sol;b>Provides for the specification of generation strategies for the values of primary keys&period;<br &sol;>&NewLine;<b style&equals;"color&colon; orange&semi;">Enum GenerationType<&sol;b> Defines the types of primary key generation strategies&period;<&sol;p>&NewLine;<p>Mapping <b>Column <&sol;b>to the <b>property <&sol;b>of class in <b>XML<&sol;b><&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;ff99cc&semi; border-width&colon; thin&semi; border&colon; solid&semi;"><b> <&sol;b>&lt&semi;property name&equals;&&num;8221&semi;userName&&num;8221&semi; column&equals;&&num;8221&semi;USER&lowbar;NAME&&num;8221&semi;&gt&semi;<b> <&sol;b><&sol;div>&NewLine;<p>Mapping <b>Column <&sol;b>to the <b>property <&sol;b>of class in <b>Annotation<&sol;b><&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;ff99cc&semi; border-width&colon; thin&semi; border&colon; solid&semi;">&NewLine;<p><b>&commat;Column&lpar;name&equals;&&num;8221&semi;USER&lowbar;NAME&&num;8221&semi;&rpar;<br style&equals;"color&colon; black&semi;" &sol;>  private String userName&semi;<&sol;b><&sol;p>&NewLine;<p><b style&equals;"color&colon; red&semi;">&commat;Column<&sol;b>&&num;8211&semi; provides the name of the column in a table if it is different from the attribute name&period; &lpar;By default&comma; the two names are assumed to be the same&period;&rpar;<br &sol;>&NewLine;<b>More JPA  Annotations for Class Field&colon;<&sol;b><br &sol;>&NewLine;<b>&commat;Basic &&num;8211&semi;  <&sol;b>The use of the <b>Basic <&sol;b>annotation is optional for persistent fields and properties of these types&period; If the <b> Basic <&sol;b>annotation is not specified for such a field or property&comma; the default values of the <b>Basic <&sol;b>annotation will apply&period; Example-<&sol;p>&NewLine;<div style&equals;"color&colon; black&semi;"><b>&commat;Basic<&sol;b><&sol;div>&NewLine;<p><b>private String userName&semi; <&sol;b><&sol;p>&NewLine;<p><b style&equals;"color&colon; red&semi;">&commat;Transient<&sol;b> &&num;8211&semi; using when if you want skip any field of entity class to save in the database&period; Example-<br &sol;>&NewLine;<b>&commat;<&sol;b><b>Transient<&sol;b><br &sol;>&NewLine;<b>private String middleName&semi;<&sol;b><&sol;p>&NewLine;<p><b style&equals;"color&colon; red&semi;">&commat;Embedded- <&sol;b>using when if property or field of persistence class is Embeddable  persistence class&period;<br &sol;>&NewLine;Example- <b>class Address&lbrace;  &commat;Column&lpar;name&equals;&&num;8221&semi;STREET&&num;8221&semi;&rpar;<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><&sol;div>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2017&sol;04&sol;AddressClass&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><&sol;div>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><&sol;div>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><&sol;div>&NewLine;<p><b> &commat;Embedded<&sol;b><br &sol;>&NewLine;<b> private Address address&semi; <&sol;b><&sol;p>&NewLine;<p><b>&commat;ElementColllection- <&sol;b>Defines a collection of instances of a basic type or embeddable class&period; Must be specified if the collection is to be mapped by means of a collection table&period; Example-<br &sol;>&NewLine;<b>&commat;ElementCollection<&sol;b><br &sol;>&NewLine;<b>private Collection&lt&semi;Address&gt&semi; lisOfAddresses &equals; new ArrayList&lt&semi;Address&gt&semi;&lpar;&rpar;&semi; <&sol;b><&sol;p>&NewLine;<p><b>&commat;Id- <&sol;b>Specifies the primary key of an entity&period; Example-<br &sol;>&NewLine;<b> &commat;Id<&sol;b><br &sol;>&NewLine;<b>private long userId&semi;<&sol;b><&sol;p>&NewLine;<p><b>&commat;EmbeddedId- <&sol;b>composite primary key of an embeddable class&period;<b> Example-<&sol;b><br &sol;>&NewLine;<b>&commat;Embeddable<&sol;b><br &sol;>&NewLine;<b>Class Address&lbrace;<&sol;b><br &sol;>&NewLine;<b>                         &commat;EmbeddedId<&sol;b><br &sol;>&NewLine;<b>                          private int addressId&semi;<&sol;b><br &sol;>&NewLine;<b>                         &&num;8212&semi;-<&sol;b><br &sol;>&NewLine;<b>                         &&num;8212&semi;-<&sol;b><br &sol;>&NewLine;<b>             &rcub;<&sol;b><&sol;p>&NewLine;<p><b style&equals;"color&colon; red&semi;">&commat;Version- <&sol;b>Specifies the version field or property of an entity class that serves as its optimistic lock value&period; The version is used to ensure integrity when performing the merge operation and for optimistic concurrency control&period; Example-<br &sol;>&NewLine;<b> &commat;Version<&sol;b><br &sol;>&NewLine;<b>private int addressId&semi;<&sol;b><&sol;p>&NewLine;<p><b>&commat;Temporal- <&sol;b>This annotation must be specified for persistent fields or properties of type <b>java&period;util&period;Date<&sol;b> and <b>java&period;util&period;Calendar<&sol;b>&period; Example-<br &sol;>&NewLine;<b>    &commat;Column&lpar;name&equals;&&num;8221&semi;JOIN&lowbar;DATE&&num;8221&semi;&rpar;<br &sol;>&NewLine;&commat;Temporal&lpar;TemporalType&period;DATE&rpar;<br &sol;>&NewLine;private Date joinDate&semi;<&sol;b><br &sol;>&NewLine;<b> <&sol;b><br &sol;>&NewLine;<b><&sol;b><&sol;p>&NewLine;<&sol;div>&NewLine;<p><b><br &sol;>&NewLine;<&sol;b> <b><br &sol;>&NewLine;<&sol;b> <b>                       &lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;p&sol;chapter-6-understanding-hibernate-or&period;html">Previous Chapter 7<&sol;a>&lt&semi;&lt&semi;    &gt&semi;&gt&semi;N<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;p&sol;hbm2ddl-configuration-and-name&period;html">ext Chapter 9<&sol;a>&gt&semi;&gt&semi;<&sol;b><br &sol;>&NewLine;<b><br &sol;>&NewLine;<&sol;b> <b><br &sol;>&NewLine;<&sol;b> <b><br &sol;>&NewLine;<&sol;b> <b><br &sol;>&NewLine;<&sol;b><&sol;p>&NewLine;<&sol;div>&NewLine;