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

View Comments
Why output tables have been removed? Please add if possible. It is easy for understanding
how can we do the same for hibernate 4.0x
wonderful explanation dinesh and thanks dinesh