One to One Mapping in Hibernate Example

<div dir&equals;"ltr" style&equals;"text-align&colon; left&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; left&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; left&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; left&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; left&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; left&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; left&semi;">&NewLine;<div style&equals;"color&colon; &num;20124d&semi;">&NewLine;<p>In the previous tutorial we learned about the <b>entity class<&sol;b> has the field of the <b>value type object<&sol;b> and also has the <b>collection <&sol;b>of the <b>value type objects<&sol;b>&period;<&sol;p>&NewLine;<div id&equals;"ads-id" align&equals;"center"><&sol;div>&NewLine;<&sol;div>&NewLine;<div style&equals;"color&colon; &num;20124d&semi;">In this tutorial of One to One Mapping in Hibernate Example we will learning what happens when an <b>entity class<&sol;b> has the field of the <b>entity type object<&sol;b>&period; Means that <b>one entity is inside the one entity<&sol;b> known as One-2-One Mapping&period;<&sol;div>&NewLine;<div style&equals;"color&colon; &num;20124d&semi;">&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;i1&period;wp&period;com&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2017&sol;04&sol;one-to-one&period;jpg" border&equals;"0" &sol;><&sol;div>&NewLine;<h2><b>Hibernate Mapping One-to-One<&sol;b><&sol;h2>&NewLine;<p>In this example you will learn how to map one-to-one relationship using Hibernate&period; Consider the following relationship between <b><i>UserDetails<&sol;i> <&sol;b>and <b><i>Vehicle<&sol;i> <&sol;b>entity&period;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;i1&period;wp&period;com&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2017&sol;04&sol;121&period;jpg" border&equals;"0" &sol;><&sol;div>&NewLine;<p>According to the relationship each user should have a unique vehicle&period;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;i1&period;wp&period;com&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2017&sol;04&sol;121map&period;jpg" border&equals;"0" &sol;><&sol;div>&NewLine;<&sol;div>&NewLine;<&sol;div>&NewLine;<p>For that we will use the following annotation&period;<br &sol;>&NewLine;<b style&equals;"color&colon; &num;20124d&semi;">&commat;OneToOne&colon;<&sol;b><br &sol;>&NewLine;<b>Target&colon;<&sol;b><br &sol;>&NewLine;Fields &lpar;including property get methods&rpar;Defines a single-valued association to another entity that has one-to-one multiplicity&period; It is not normally necessary to specify the associated target entity explicitly since it can usually be inferred from the type of the object being referenced&period; If the relationship is bidirectional&comma; the non-owning side must use the <b>mappedBy<&sol;b><b> <&sol;b>element of the <b style&equals;"color&colon; &num;20124d&semi;">&commat;OneToOne <&sol;b>annotation to specify the relationship field or property of the owning side&period; The <b style&equals;"color&colon; &num;20124d&semi;">&commat;OneToOne <&sol;b><b><&sol;b>annotation may be used within an embeddable class to specify a relationship from the embeddable class to an entity class&period;<&sol;p>&NewLine;<&sol;div>&NewLine;<p>Now we look the following Example related to the One to One mapping&period;<&sol;p>&NewLine;<p><b>1&period; First Create Vehicle Class<&sol;b><br &sol;>&NewLine;<b><br &sol;>&NewLine;<&sol;b> <b>Vehicle&period;java<&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>package com&period;sdnext&period;hibernate&period;tutorial&period;dto&semi;<&sol;p>&NewLine;<p>import javax&period;persistence&period;Column&semi;<br &sol;>&NewLine;import javax&period;persistence&period;Entity&semi;<br &sol;>&NewLine;import javax&period;persistence&period;GeneratedValue&semi;<br &sol;>&NewLine;import javax&period;persistence&period;GenerationType&semi;<br &sol;>&NewLine;import javax&period;persistence&period;Id&semi;<br &sol;>&NewLine;import javax&period;persistence&period;Table&semi;<&sol;p>&NewLine;<p><b>&commat;Entity<br style&equals;"color&colon; &num;20124d&semi;" &sol;>&commat;Table&lpar;name&equals;&&num;8221&semi;VEHICLE&&num;8221&semi;&rpar;<&sol;b><br &sol;>&NewLine;public class Vehicle<br &sol;>&NewLine;&lbrace;<br &sol;>&NewLine;<b style&equals;"color&colon; &num;20124d&semi;">&commat;Id<br &sol;>&NewLine;&commat;GeneratedValue&lpar;strategy&equals;GenerationType&period;AUTO&rpar;<br &sol;>&NewLine;&commat;Column&lpar;name&equals;&&num;8221&semi;VEHICLE&lowbar;ID&&num;8221&semi;&rpar;<&sol;b><br &sol;>&NewLine;private int vehicleId&semi;<&sol;p>&NewLine;<p><b>  &commat;Column&lpar;name&equals;&&num;8221&semi;VEHICLE&lowbar;NAME&&num;8221&semi;&rpar;<&sol;b><br &sol;>&NewLine;private String vehicleName&semi;<&sol;p>&NewLine;<p>public int getVehicleId&lpar;&rpar; &lbrace;<br &sol;>&NewLine;return vehicleId&semi;<br &sol;>&NewLine;&rcub;<br &sol;>&NewLine;public void setVehicleId&lpar;int vehicleId&rpar; &lbrace;<br &sol;>&NewLine;this&period;vehicleId &equals; vehicleId&semi;<br &sol;>&NewLine;&rcub;<br &sol;>&NewLine;public String getVehicleName&lpar;&rpar; &lbrace;<br &sol;>&NewLine;return vehicleName&semi;<br &sol;>&NewLine;&rcub;<br &sol;>&NewLine;public void setVehicleName&lpar;String vehicleName&rpar; &lbrace;<br &sol;>&NewLine;this&period;vehicleName &equals; vehicleName&semi;<br &sol;>&NewLine;&rcub;<br &sol;>&NewLine;&rcub;<&sol;p>&NewLine;<&sol;div>&NewLine;<&sol;div>&NewLine;<h2><b>2&period; Create the User Class<&sol;b><br &sol;>&NewLine;<b><br &sol;>&NewLine;<&sol;b> <b>UserDetails&period;java<&sol;b><&sol;h2>&NewLine;<div style&equals;"background-color&colon; &num;ff99cc&semi; border-width&colon; thin&semi; border&colon; solid&semi;">&NewLine;<p>package com&period;sdnext&period;hibernate&period;tutorial&period;dto&semi;<&sol;p>&NewLine;<p>import javax&period;persistence&period;Column&semi;<br &sol;>&NewLine;import javax&period;persistence&period;Entity&semi;<br &sol;>&NewLine;import javax&period;persistence&period;GeneratedValue&semi;<br &sol;>&NewLine;import javax&period;persistence&period;GenerationType&semi;<br &sol;>&NewLine;import javax&period;persistence&period;Id&semi;<br &sol;>&NewLine;import javax&period;persistence&period;JoinColumn&semi;<br &sol;>&NewLine;import javax&period;persistence&period;OneToOne&semi;<br &sol;>&NewLine;import javax&period;persistence&period;Table&semi;<&sol;p>&NewLine;<p><b style&equals;"color&colon; &num;20124d&semi;">&commat;Entity<br &sol;>&NewLine;&commat;Table &lpar;name&equals;&&num;8221&semi;USER&lowbar;DETAIL&&num;8221&semi;&rpar;<&sol;b><br &sol;>&NewLine;public class UserDetails<br &sol;>&NewLine;&lbrace;<br &sol;>&NewLine;<b style&equals;"color&colon; &num;20124d&semi;">&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;<&sol;b><br &sol;>&NewLine;private int    userId&semi;<&sol;p>&NewLine;<p><b> &commat;Column&lpar;name&equals;&&num;8221&semi;USER&lowbar;NAME&&num;8221&semi;&rpar; <&sol;b><br &sol;>&NewLine;private String userName&semi;<&sol;p>&NewLine;<p><b>&commat;OneToOne<br style&equals;"color&colon; &num;20124d&semi;" &sol;>    &commat;JoinColumn&lpar;name&equals;&&num;8221&semi;VEHICLE&lowbar;ID&&num;8221&semi;&rpar;<&sol;b><br &sol;>&NewLine;private Vehicle vehicle&semi;<&sol;p>&NewLine;<p>public Vehicle getVehicle&lpar;&rpar; &lbrace;<br &sol;>&NewLine;return vehicle&semi;<br &sol;>&NewLine;&rcub;<br &sol;>&NewLine;public void setVehicle&lpar;Vehicle vehicle&rpar; &lbrace;<br &sol;>&NewLine;this&period;vehicle &equals; vehicle&semi;<br &sol;>&NewLine;&rcub;<br &sol;>&NewLine;public int getUserId&lpar;&rpar; &lbrace;<br &sol;>&NewLine;return userId&semi;<br &sol;>&NewLine;&rcub;<br &sol;>&NewLine;public void setUserId&lpar;int userId&rpar; &lbrace;<br &sol;>&NewLine;this&period;userId &equals; userId&semi;<br &sol;>&NewLine;&rcub;<br &sol;>&NewLine;public String getUserName&lpar;&rpar; &lbrace;<br &sol;>&NewLine;return userName&semi;<br &sol;>&NewLine;&rcub;<br &sol;>&NewLine;public void setUserName&lpar;String userName&rpar; &lbrace;<br &sol;>&NewLine;this&period;userName &equals; userName&semi;<br &sol;>&NewLine;&rcub;<br &sol;>&NewLine;&rcub;<&sol;p>&NewLine;<&sol;div>&NewLine;<&sol;div>&NewLine;<h2><b>3&period; Create the hibernate configuration file&period;<&sol;b><br &sol;>&NewLine;<b><br &sol;>&NewLine;<&sol;b> <b>hibernate&period;cfg&period;xml<&sol;b>&colon;<&sol;h2>&NewLine;<div style&equals;"background-color&colon; &num;ff99cc&semi; border-width&colon; thin&semi; border&colon; solid&semi;">&NewLine;<p>&lt&semi;&quest;xml version&equals;&&num;8221&semi;1&period;0&&num;8243&semi; encoding&equals;&&num;8221&semi;UTF-8&&num;8243&semi;&quest;&gt&semi;<&sol;p>&NewLine;<p>&lt&semi;&excl;DOCTYPE hibernate-configuration PUBLIC<br &sol;>&NewLine;&&num;8220&semi;-&sol;&sol;Hibernate&sol;Hibernate Configuration DTD 3&period;0&sol;&sol;EN&&num;8221&semi;<br &sol;>&NewLine;&&num;8220&semi;http&colon;&sol;&sol;hibernate&period;sourceforge&period;net&sol;hibernate-configuration-3&period;0&period;dtd&&num;8221&semi;&gt&semi;<&sol;p>&NewLine;<p>&lt&semi;hibernate-configuration&gt&semi;<br &sol;>&NewLine;&lt&semi;session-factory&gt&semi;<br &sol;>&NewLine;&lt&semi;&excl;&&num;8211&semi; Database connection settings &&num;8211&semi;&gt&semi;<br &sol;>&NewLine;&lt&semi;property name&equals;&&num;8221&semi;connection&period;driver&lowbar;class&&num;8221&semi;&gt&semi;com&period;mysql&period;jdbc&period;Driver&lt&semi;&sol;property&gt&semi;<br &sol;>&NewLine;&lt&semi;property name&equals;&&num;8221&semi;connection&period;url&&num;8221&semi;&gt&semi;jdbc&colon;mysql&colon;&sol;&sol;localhost&colon;3306&sol;hibernateDB&lt&semi;&sol;property&gt&semi;<br &sol;>&NewLine;&lt&semi;property name&equals;&&num;8221&semi;connection&period;username&&num;8221&semi;&gt&semi;root&lt&semi;&sol;property&gt&semi;<br &sol;>&NewLine;&lt&semi;property name&equals;&&num;8221&semi;connection&period;password&&num;8221&semi;&gt&semi;root&lt&semi;&sol;property&gt&semi;<&sol;p>&NewLine;<p>&lt&semi;&excl;&&num;8211&semi; JDBC connection pool &lpar;use the built-in&rpar; &&num;8211&semi;&gt&semi;<br &sol;>&NewLine;&lt&semi;property name&equals;&&num;8221&semi;connection&period;pool&lowbar;size&&num;8221&semi;&gt&semi;1&lt&semi;&sol;property&gt&semi;<&sol;p>&NewLine;<p>&lt&semi;&excl;&&num;8211&semi; SQL dialect &&num;8211&semi;&gt&semi;<br &sol;>&NewLine;&lt&semi;property name&equals;&&num;8221&semi;dialect&&num;8221&semi;&gt&semi;org&period;hibernate&period;dialect&period;MySQLDialect&lt&semi;&sol;property&gt&semi;<&sol;p>&NewLine;<p>&lt&semi;&excl;&&num;8211&semi; Enable Hibernate&&num;8217&semi;s automatic session context management &&num;8211&semi;&gt&semi;<br &sol;>&NewLine;&lt&semi;property name&equals;&&num;8221&semi;current&lowbar;session&lowbar;context&lowbar;class&&num;8221&semi;&gt&semi;thread&lt&semi;&sol;property&gt&semi;<&sol;p>&NewLine;<p>&lt&semi;&excl;&&num;8211&semi; Disable the second-level cache &&num;8211&semi;&gt&semi;<br &sol;>&NewLine;&lt&semi;property name&equals;&&num;8221&semi;cache&period;provider&lowbar;class&&num;8221&semi;&gt&semi;org&period;hibernate&period;cache&period;NoCacheProvider&lt&semi;&sol;property&gt&semi;<&sol;p>&NewLine;<p>&lt&semi;&excl;&&num;8211&semi; Echo all executed SQL to stdout &&num;8211&semi;&gt&semi;<br &sol;>&NewLine;&lt&semi;property name&equals;&&num;8221&semi;show&lowbar;sql&&num;8221&semi;&gt&semi;true&lt&semi;&sol;property&gt&semi;<&sol;p>&NewLine;<p>&lt&semi;&excl;&&num;8211&semi; Drop and re-create the database schema on startup &&num;8211&semi;&gt&semi;<br &sol;>&NewLine;&lt&semi;property name&equals;&&num;8221&semi;hbm2ddl&period;auto&&num;8221&semi;&gt&semi;create&lt&semi;&sol;property&gt&semi;<&sol;p>&NewLine;<p>&lt&semi;mapping class&equals;&&num;8221&semi;com&period;sdnext&period;hibernate&period;tutorial&period;dto&period;UserDetails&&num;8221&semi;&sol;&gt&semi;<br &sol;>&NewLine;&lt&semi;mapping class&equals;&&num;8221&semi;com&period;sdnext&period;hibernate&period;tutorial&period;dto&period;Vehicle&&num;8221&semi;&sol;&gt&semi;<&sol;p>&NewLine;<p>&lt&semi;&sol;session-factory&gt&semi;<br &sol;>&NewLine;&lt&semi;&sol;hibernate-configuration&gt&semi;<&sol;p>&NewLine;<&sol;div>&NewLine;<&sol;div>&NewLine;<h2><b>4&period; Create Test Demo class for run this code&period;<&sol;b><br &sol;>&NewLine;<b><br &sol;>&NewLine;<&sol;b> <b>HibernateTestDemo&period;java<&sol;b><&sol;h2>&NewLine;<div style&equals;"background-color&colon; &num;ff99cc&semi; border-width&colon; thin&semi; border&colon; solid&semi;">&NewLine;<p>package com&period;sdnext&period;hibernate&period;tutorial&semi;<&sol;p>&NewLine;<p>import org&period;hibernate&period;Session&semi;<br &sol;>&NewLine;import org&period;hibernate&period;SessionFactory&semi;<br &sol;>&NewLine;import org&period;hibernate&period;cfg&period;AnnotationConfiguration&semi;<&sol;p>&NewLine;<p>import com&period;sdnext&period;hibernate&period;tutorial&period;dto&period;UserDetails&semi;<br &sol;>&NewLine;import com&period;sdnext&period;hibernate&period;tutorial&period;dto&period;Vehicle&semi;<&sol;p>&NewLine;<p>public class HibernateTestDemo &lbrace;<br &sol;>&NewLine;&sol;&ast;&ast;<br &sol;>&NewLine;&ast; &commat;param args<br &sol;>&NewLine;&ast;&sol;<br &sol;>&NewLine;public static void main&lpar;String&lbrack;&rsqb; args&rpar;<br &sol;>&NewLine;&lbrace;<br &sol;>&NewLine;UserDetails user &equals; new UserDetails&lpar;&rpar;&semi; &sol;&sol;create the user entity<br &sol;>&NewLine;Vehicle vehicle &equals; new Vehicle&lpar;&rpar;&semi; &sol;&sol;create the vehicle entity<&sol;p>&NewLine;<p>vehicle&period;setVehicleName&lpar;&&num;8220&semi;BMW Car&&num;8221&semi;&rpar;&semi; &sol;&sol;set vehicle name<&sol;p>&NewLine;<p>user&period;setUserName&lpar;&&num;8220&semi;Dinesh Rajput&&num;8221&semi;&rpar;&semi; &sol;&sol;set the user name<br &sol;>&NewLine;user&period;setVehicle&lpar;vehicle&rpar;&semi; &sol;&sol;set the vehicle entity to the field of the user entity i&period;e&period; vehicle entity inside the user entity<&sol;p>&NewLine;<p>SessionFactory sessionFactory &equals; new AnnotationConfiguration&lpar;&rpar;&period;configure&lpar;&rpar;&period;buildSessionFactory&lpar;&rpar;&semi; &sol;&sol;create session factory object<br &sol;>&NewLine;Session session &equals; sessionFactory&period;openSession&lpar;&rpar;&semi; &sol;&sol;create the session object<br &sol;>&NewLine;session&period;beginTransaction&lpar;&rpar;&semi;&sol;&sol;create the transaction from the session object<&sol;p>&NewLine;<p>session&period;save&lpar;vehicle&rpar;&semi; &sol;&sol; save the vehicle entity to the database<br &sol;>&NewLine;session&period;save&lpar;user&rpar;&semi; &sol;&sol; save the user entity to the database<&sol;p>&NewLine;<p>session&period;getTransaction&lpar;&rpar;&period;commit&lpar;&rpar;&semi; &sol;&sol;close the transaction<br &sol;>&NewLine;session&period;close&lpar;&rpar;&semi; &sol;&sol;close the session<br &sol;>&NewLine;&rcub;<br &sol;>&NewLine;&rcub;<&sol;p>&NewLine;<p><b>OUTPUT&colon;<&sol;b><br &sol;>&NewLine;log4j&colon;WARN No appenders could be found for logger &lpar;org&period;hibernate&period;cfg&period;annotations&period;Version&rpar;&period;<br style&equals;"color&colon; red&semi;" &sol;>log4j&colon;WARN Please initialize the log4j system properly&period;<br &sol;>&NewLine;Hibernate&colon; insert into VEHICLE &lpar;VEHICLE&lowbar;NAME&rpar; values &lpar;&quest;&rpar;<br &sol;>&NewLine;Hibernate&colon; insert into USER &lpar;USER&lowbar;NAME&comma; VEHICLE&lowbar;ID&rpar; values &lpar;&quest;&comma; &quest;&rpar;<&sol;p>&NewLine;<&sol;div>&NewLine;<&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;i1&period;wp&period;com&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2017&sol;04&sol;121output&period;jpg" border&equals;"0" &sol;><&sol;div>&NewLine;<p>Now we look the created tables for that&period;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;i1&period;wp&period;com&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2017&sol;04&sol;Untitled&period;jpg" border&equals;"0" &sol;><&sol;div>&NewLine;<p><b>In <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;p&sol;hibernate-one-to-many-mapping-tutorial&period;html">Next Chapter<&sol;a> we will discuss about <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;p&sol;hibernate-one-to-many-mapping-tutorial&period;html">One to Many Mapping<&sol;a>&period;<&sol;b><br &sol;>&NewLine;<b><br &sol;>&NewLine;<&sol;b> <b>              &lt&semi;&lt&semi;<a style&equals;"font-family&colon; Times&comma; 'Times New Roman'&comma; serif&semi;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;p&sol;proxy-objects-and-eager-and-lazy-fetch&period;html">Previous Chapter 16<&sol;a>&lt&semi;&lt&semi;    &gt&semi;&gt&semi;N<a style&equals;"font-family&colon; Times&comma; 'Times New Roman'&comma; serif&semi;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;p&sol;hibernate-one-to-many-mapping-tutorial&period;html">ext Chapter18<&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> <b><br &sol;>&NewLine;<&sol;b><&sol;p>&NewLine;<&sol;div>&NewLine;

View Comments