Software Developer
In easy terms, “one to many” is a name given to a relationship among tables described in a database. If any entry in table a is associated with a couple of access in desk b, then that type of relationship is called “one to many”. This dating can be completed with the help of a hibernate platform. Hibernate can hyperlink java-primarily based the front-cease programs to any database with the assist of JPA annotations or XML. It comes preloaded with many functions, which may be referred to as with the aid of adding related libraries in your task.
Syntax of Hibernate One to Many mapping is similar to one-to-one mapping but with a difference that there is more than one entry linked to one column rather than just one column mapping with another one.
There are certain syntaxes to be followed to establish one too many mapping using hibernate successfully, and those are:
The one to many mapping annotations is used in the table, which has many entities to be mapped.
It looks like:
Cascade rules are already defined in persistence libraries and have to be just called at the time of implementing the example. This table now should be linked with another table that has multiple entries. In this case, we are taking another table as “Marks”.
It looks like:
There are a lot of coded logics and functions in the background under libraries which are called by the compiler with the help of these annotations.
Here is the example of “One to Many” mapping using hibernate. You would require Eclipse, JPA libraries related RAR files, Maven, and database installed in your system to run Hibernate-related programs.
We are taking the example of a student and subject marks. There are two tables created in a database named “student” and “marks”. The relation between these two tables is one too many as one student can give multiple tests and get marks.
S_id
S_name
S_phno
S_id
M_marks
M_testid
package test
import java.util.Set;
import javax.persistence.*;
@Entity
@Table(name = "Student")
public class Student {
@Id
@GeneratedValue
@Column(name = "S_id")
private int id;
@Column(name = "S_name")
private String Name;
@Column(name = "S_phno")
private String Phone;
@OneToMany(mappedBy = "Student", cascade = CascadeType.ALL)
private Set marksDetails;
public Student() { }
public int getId() {
return S_id;
}
public void setId(long S_id) {
this.S_id = S_id;
}
public String getName() {
return Name;
}
public void setName(String Name) {
this.Name = Name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Set getMarksDetails() {
return marksDetails;
}
public void setMarksDetails(Set marksDetails) {
this.marksDetails = marksDetails;
}
}
package test;
import javax.persistence.*;
@Entity
@Table(name = "Marks")
public class Marks {
@Id
@GeneratedValue
@Column(name = "M_testid")
private int M_testid;
@Column(name = "M_marks")
private String M_marks;
@ManyToOne
@JoinColumn(name = "S_id")
private Student student;
public Marks() { }
public int getTestId() {
return M_testid;
}
public void setTestId(int M_testid) {
this.M_testid = M_testid;
}
public String getMarks() {
return M_marks;
}
public void setMarks(String M_marks) {
this.M_marks = M_marks;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
}
package test;
import org.hibernate.*;
public class Test {
static Session sO;
static SessionFactory sF;
private static SessionFactory buildSessionFactory() {
Configuration cO = new Configuration();
cO.configure("hibernate.cfg.xml");
ServiceRegistry sRO = new StandardServiceRegistryBuilder().applySettings(cO.getProperties()).build();
sFO = cO.buildSessionFactory(sRO);
return sFO;
}
public static void main(String[] args) {
System.out.println("...Example of hibernate one to many mapping...");
try {
sO = buildSessionFactory().openSession();
sO.beginTransaction();
Student studentObj = new Student("Name1", "0123456789");
sessionObj.save(studentObj);
Marks mObj = new Marks ("20", "90");
mObj.setStudent(studentObj);
sO.save(mObj);
Marks mObj = new Marks ("10", "100");
mObj.setStudent(studentObj);
sO.save(mObj);
sO.getTransaction().commit();
System.out.println("\n...Records successfully registered in the database...");
} catch(Exception sqlException) {
if(null != sO.getTransaction()) {
System.out.println("\n.......error......");
sO.getTransaction().rollback();
}
sqlException.printStackTrace();
} finally {
if(sO != null) {
sO.close();
}
}
}
}
Hibernate is a version to hold item family members from front-stop programs evolved on java to back-cease databases. One of the important relation kinds we've got often labored on in databases without delay is “one to many”. It has to turn out to be easy to create “one to many” members of the family in tables using hibernate one to many mapping annotations. It's far a clean and clutter-unfastened technique of operating with database-intensive applications. It enables builders to focus on writing business logic impartial of any database. It's far one of the cool pieces of equipment in java international to work on databases.