Hibernate One to One Example using Annotation

Here, we will perform balanced mapping by coordinated component utilizing explanation. In such case, no outside key is made in the essential table.

In this model, one representative can have one location and one location has a place with one worker as it were. Here, we are utilizing bidirectional affiliation. How about we take a gander at the relentless classes.

1) Persistent classes for balanced mapping

There are two industrious classes Employee.java and Address.java. Worker class contains Address class reference and bad habit versa.

Employee.java

  1. package com.javacodegeeks;  
  2. import javax.persistence.*;  
  3.   
  4. @Entity  
  5. @Table(name="emp220")  
  6. public class Employee {    
  7.       
  8.     @Id  
  9.     @GeneratedValue(strategy=GenerationType.AUTO)  
  10.     @PrimaryKeyJoinColumn  
  11. private int employeeId;    
  12. private String name,email;    
  13. @OneToOne(targetEntity=Address.class,cascade=CascadeType.ALL)  
  14. private Address address;  
  15. public int getEmployeeId() {  
  16.     return employeeId;  
  17. }  
  18. public void setEmployeeId(int employeeId) {  
  19.     this.employeeId = employeeId;  
  20. }  
  21. public String getName() {  
  22.     return name;  
  23. }  
  24. public void setName(String name) {  
  25.     this.name = name;  
  26. }  
  27. public String getEmail() {  
  28.     return email;  
  29. }  
  30. public void setEmail(String email) {  
  31.     this.email = email;  
  32. }  
  33. public Address getAddress() {  
  34.     return address;  
  35. }  
  36. public void setAddress(Address address) {  
  37.     this.address = address;  
  38. }    
  39.   
  40. }    

Address.java

  1. package com.javacodegeeks;  
  2. import javax.persistence.*;  
  3.   
  4. @Entity  
  5. @Table(name="address220")  
  6. public class Address {    
  7.       
  8.     @Id  
  9.     @GeneratedValue(strategy=GenerationType.AUTO)  
  10. private int addressId;    
  11. private String addressLine1,city,state,country;    
  12. private int pincode;    
  13.   
  14. @OneToOne(targetEntity=Employee.class)  
  15. private Employee employee;  
  16. public int getAddressId() {  
  17.     return addressId;  
  18. }  
  19. public void setAddressId(int addressId) {  
  20.     this.addressId = addressId;  
  21. }  
  22. public String getAddressLine1() {  
  23.     return addressLine1;  
  24. }  
  25. public void setAddressLine1(String addressLine1) {  
  26.     this.addressLine1 = addressLine1;  
  27. }  
  28. public String getCity() {  
  29.     return city;  
  30. }  
  31. public void setCity(String city) {  
  32.     this.city = city;  
  33. }  
  34. public String getState() {  
  35.     return state;  
  36. }  
  37. public void setState(String state) {  
  38.     this.state = state;  
  39. }  
  40. public String getCountry() {  
  41.     return country;  
  42. }  
  43. public void setCountry(String country) {  
  44.     this.country = country;  
  45. }  
  46. public int getPincode() {  
  47.     return pincode;  
  48. }  
  49. public void setPincode(int pincode) {  
  50.     this.pincode = pincode;  
  51. }  
  52. public Employee getEmployee() {  
  53.     return employee;  
  54. }  
  55. public void setEmployee(Employee employee) {  
  56.     this.employee = employee;  
  57. }    
  58. }  

2) Add project information and configuration in pom.xml file.

Open pom.xml file and click source. Now, add the below dependencies between <dependencies>....</dependencies> tag. These dependencies are used to add the jar files in Maven project.

  1. <dependency>    
  2.     <groupId>org.hibernate</groupId>    
  3.     <artifactId>hibernate-core</artifactId>    
  4.     <version>5.3.1.Final</version>    
  5. </dependency>    
  1. <dependency>    
  2.     <groupId>com.oracle</groupId>    
  3.     <artifactId>ojdbc14</artifactId>    
  4.     <version>10.2.0.4.0</version>    
  5. </dependency>    

3) Configuration file

This file contains information about the database and mapping file.

hibernate.cfg.xml

  1. <?xml version='1.0' encoding='UTF-8'?>    
  2. <!DOCTYPE hibernate-configuration PUBLIC    
  3.           "-//Hibernate/Hibernate Configuration DTD 5.3//EN"    
  4.           "http://hibernate.sourceforge.net/hibernate-configuration-5.3.dtd">    
  5.      
  6. <hibernate-configuration>    
  7.     
  8.     <session-factory>    
  9.         <property name="hbm2ddl.auto">update</property>    
  10.         <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>    
  11.         <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>    
  12.         <property name="connection.username">system</property>    
  13.         <property name="connection.password">jtp</property>    
  14.         <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>    
  15.     <mapping class="com.javacodegeeks.Address"/>    
  16.     <mapping class="com.javacodegeeks.Employee"/>    
  17.     </session-factory>    
  18.     
  19. </hibernate-configuration>    

4) User classes to store and fetch the data

Store.java

  1. package com.javacodegeeks;    
  2.     
  3. import org.hibernate.*;  
  4. import org.hibernate.boot.Metadata;  
  5. import org.hibernate.boot.MetadataSources;  
  6. import org.hibernate.boot.registry.StandardServiceRegistry;  
  7. import org.hibernate.boot.registry.StandardServiceRegistryBuilder;    
  8.     
  9. public class Store {    
  10. public static void main(String[] args) {    
  11.       
  12.     StandardServiceRegistry ssr=new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();  
  13.     Metadata meta=new MetadataSources(ssr).getMetadataBuilder().build();  
  14.       
  15.     SessionFactory factory=meta.getSessionFactoryBuilder().build();  
  16.     Session session=factory.openSession();  
  17.       
  18.     Transaction t=session.beginTransaction();   
  19.       
  20.     Employee e1=new Employee();    
  21.     e1.setName("reena Malik");    
  22.     e1.setEmail("reena@gmail.com");    
  23.         
  24.     Address address1=new Address();    
  25.     address1.setAddressLine1("G-21,Lohia nagar");    
  26.     address1.setCity("Ghaziabad");    
  27.     address1.setState("UP");    
  28.     address1.setCountry("India");    
  29.     address1.setPincode(201301);    
  30.         
  31.     e1.setAddress(address1);    
  32.     address1.setEmployee(e1);    
  33.         
  34.     session.persist(e1);    
  35.     t.commit();    
  36.         
  37.     session.close();    
  38.     System.out.println("success");    
  39. }    
  40. }    





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition