
How does one-to-many relationships works in hibernate?

Hibernate Mapping One-to-Many
Hibernate provides facility of mapping. You can do mapping through hbm.xml or through annotation. Here is an example showing one to many relationship using annotation?
We have two table ?
One author can write more than one book..So here is mapping one author to many books.
Author.java
package net.roseindia.table;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "author")
public class Author {
private int id;
private String name;
private Set<Book> bookSet = new HashSet<Book>();
public Author() {
}
public Author(String name, Set<Book> book) {
this.name = name;
this.bookSet = book;
}
@Id
@GeneratedValue
@Column(name = "id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(cascade = CascadeType.ALL)
public Set<Book> getBookSet() {
return bookSet;
}
public void setBookSet(Set<Book> bookSet) {
this.bookSet = bookSet;
}
}
Book.java
package net.roseindia.table;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "book")
public class Book {
private int id;
private String tile;
public Book() {
}
public Book(String title) {
this.tile = title;
}
@Id
@GeneratedValue
@Column(name = "id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name = "title")
public String getTile() {
return tile;
}
public void setTile(String tile) {
this.tile = tile;
}
}
Continue....

2.
hibernate-cfg.xml is configuration file saved in the same folder where the source code of class file is saved. It creates the connection pool and set-up required environment.
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/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/hibernateRelationShip</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">create-drop</property>
<!-- Mapping files -->
<mapping class="net.roseindia.table.Book" />
<mapping class="net.roseindia.table.Author" />
</session-factory>
</hibernate-configuration>
3.
Now create an util class as HibernateUtil.java.
package net.roseindia.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class HibernateUtil {
private static SessionFactory sessionFactory = null;
static {
sessionFactory = new AnnotationConfiguration().configure()
.buildSessionFactory();
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
4.
Here is your main class-
package net.roseindia.application;
import java.util.HashSet;
import java.util.Set;
import net.roseindia.table.Author;
import net.roseindia.table.Book;
import net.roseindia.util.HibernateUtil;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class OneToManyRelation {
public static void main(String[] args) {
Session session = null;
try {
session = HibernateUtil.getSessionFactory().openSession();
Set<Book> books = new HashSet<Book>();
books.add(new Book("Java"));
books.add(new Book("C++"));
Author author = new Author("Ratna", books);
Transaction tr = session.beginTransaction();
session.save(author);
tr.commit();
System.out.println("Done");
} catch (HibernateException he) {
System.out.println(he.getMessage());
} finally {
session.close();
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.