Hibernate One To One Annotation Tutorial

Add Following Dependency in pom.xml file

<dependencies>

<dependency>
	<groupId>org.hibernate</groupId>
	<artifactId>hibernate-core</artifactId>
	<version>5.4.28.Final</version>
</dependency>

<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
	<version>5.1.6</version>
</dependency>

</dependencies>
  

hibernate.cfg.xml

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>

<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/stds</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>

<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
		
	
</session-factory>
</hibernate-configuration>

Student.java

  
package com.deepsingh44.model;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name = "sttds")
public class Student {
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int roll;
	private String name;
	private float marks;
	@OneToOne(targetEntity = Address.class,
    cascade = { CascadeType.ALL })
	private Address address;

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	public int getRoll() {
		return roll;
	}

	public void setRoll(int roll) {
		this.roll = roll;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public float getMarks() {
		return marks;
	}

	public void setMarks(float marks) {
		this.marks = marks;
	}

}

Address.java

  
package com.deepsingh44.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name = "adds")
public class Address {
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int id;
	private int pincode;
	private String area;
	@OneToOne(targetEntity = Student.class)
	private Student student;

	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	public int getPincode() {
		return pincode;
	}

	public void setPincode(int pincode) {
		this.pincode = pincode;
	}

	public String getArea() {
		return area;
	}

	public void setArea(String area) {
		this.area = area;
	}

}
 
 

Test.java

  
package com.deepsingh44.model;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

public class Test {
	public static void main(String[] args) {

	// Load your configuration file
	StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().
			configure("hibernate.cfg.xml").
			build();
	Metadata meta = new MetadataSources(ssr).
			addAnnotatedClass(Student.class).
			addAnnotatedClass(Address.class).
			getMetadataBuilder().build();

	// Get sessionFactory object
	SessionFactory factory = meta.getSessionFactoryBuilder().
			build();

	// Get session object
	Session session = factory.openSession();

	// Compose Java Object
	Student student = new Student();
	student.setName("Sujeet1");
	student.setMarks(87.8f);

	Address address = new Address();
	address.setArea("gzb");
	address.setPincode(20456);
	address.setStudent(student);

	student.setAddress(address);

	// you need to persist java object in to database
	session.persist(student);

	// Actual transaction here
	session.beginTransaction().commit();

	session.close();
	System.out.println("Successfully Inserted");

}
}
  
  

No comments: