How to work Hibernate Many to One?

How to work Hibernate Many to One?
Techiio-author
Written by Sagar RabidasFebruary 15, 2022
7 min read
Hibernate
2 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 2 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

In this blog, we will discuss Hibernate Many to One.

Hibernate Many to One

Hibernate Many to One mapping means that many rows in a table are mapped to one row in another table. This is the association diagram to understand one too many relationships:

Modeling a database involves defining several ones to many relationships or many one relationships. Additionally the equal, whilst you are modeling the entities. Jpa and hibernate are chosen to do the undertaking effortlessly. Annotating a characteristic is taken that represents the institutions one too many and many to one are what one needs. Many to at least one is a unidirectional annotation-based totally configuration.

Syntax:

public class Cart {
//...
@OneToMany(mappedBy="cart")
private Set<Items> items;
//...
}

How Hibernate Many to One Annotation Works?

Hibernate tells us which variable we use to represent the parent class in the child class by the mappedBy property.

Below are the libraries used to develop a sample application that implements one-to-many association:

  • H2 database.
  • Hibernate 5.
  • JDK 1.8 and later.
  • Maven 3 and later.

The below will be showing the relationship between the entities.

Firstly, create a java class with the hibernate annotations to create the address and student table.

Code:

package com.tutorial.student_information;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerateionType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "STUDENT NAME")
public class Student {
private long Id;
private String Name_Of_Student;
private Address Address_Of_Student;
public Student() {
}
public Student(String Name_Of_Student, Address Address_Of_Student) {
this.studentName = Name_Of_Student;
this.Address_Of_Student = Address_Of_Student;
}
@Id
@GeneratedValue
@Column(name = "ID")
public long getId() {
return this.Id;
}
public void setId(long Id) {
this.Id = Id;
}
@Column(name = "NAME_OF_STUDENT", nullable = false, length = 150)
public String getName_Of_Student() {
return this.Name_Of_Student;
}
public void setName_Of_Student(String Name_Of_Student) {
this.Name_Of_Student = Name_Of_Student;
}
@ManyToOne(cascade = CascadeType.ALL) //cascade equals save
public Address getAddress_Of_Student() {
return this.Address_Of_Students;
}
public void setAddress_Of_Student(Address Address_Of_Student) {
this.Address_Of_Student = Address_Of_Student;
}
}

And thus, the database setup is about ready. Now, let’s create an example:

Now, in this example, we will see how to map many-to-one relationships using annotations of hibernate. Considering the relationship between address and students as entities.

cascade is used to cascade the operations with associated entities. For example, if you have an object of a student, the address associated with it will be saved automatically.

package com.tutorial.student_information;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerateionType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "ADDRESS_INFORMATION")
public class Address_Of_Student {
private String zipcode_number;
private String street_name;
private String state_name;
private String city_name;
private long addressId;
public Address_Of_Student() {
}
public Address_Of_Student(String zipcode_number,String state_name,String city_name,String street_name) {
this.street = street_name;
this.city = city_name;
this.state = state_name;
this.zipcode = zipcode_number;
}
@Id
@GeneratedValue
@Column(name = "ID_OF_ADDRESS")
public long getId() {
return this.Id;
}
public void setId(long Id) {
this.Id = Id;
}
@Column(name = "STREE_OF_ADDRESS_T", nullable = false)
public String getStreet_name() {
return this.street_name;
}
public void setStreet_name(String street_name) {
this.street_name = street_name;
}
@Column(name = "CITY_OF_ADDRESS", nullable = false)
public String getCity_name() {
return this.city_name;
}
public void setCity_name(String city_name) {
this.city_name = city_name;
}
@Column(name = "STATE_OF_ADDRESS", nullable = false)
public String getState_name() {
return this.state_name;
}
public void setState_name(String state_name) {
this.state_name = state_name;
}
@Column(name = "ZIPCODE_OF_ADDRESS", nullable = false)
public String getZipcode_number() {
return this.zipcode_number;
}
public void setZipcode_number(String zipcode_number) {
this.zipcode_number = zipcode_number;
} }

In main class:

package com.tutorial.student_information;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.tutorial.util.HibernateUtil;
public class Main {
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
Address_Of_Student address_information = new Address_Of_Student("60097", "TN", "Chennai", "OMR Road");
Student_information first_Student = new Student_information("Eswar", address_information);
Student_information second_Student = new Student_information("Joe", address_information);
session.save(first_Student);
session.save(second_Student);
transaction.commit();
} catch (HibernateException e) {
if (transaction! = null)
transaction.rollback():
e.printStackTrace();
} finally {
session.close();
}
}
HibernateUtil.shutdown():
}

Hibernate
HibernateManytoOne
Java
2 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 2 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Sagar Rabidas
Software Developer
Techiio-followerTechiio-followerTechiio-followerTechiio-followerTechiio-follower
+8 more
300 Blog Posts
14 Discussion Threads
Trending Technologies
15
Software40
DevOps46
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance