@OneToMany in Hibernate Annotation
February 06, 2013
@OneToMany in hibernate annotation is used for mapping two tables. @OneToMany associates a parent table and one child table in the way that a parent can have more than one child. @JoinColumn joins the parent table and child table by a column. We have two entities Country and State. We are mapping these two tables. Here Country is parent and State is child. One country can have more than states.
Country.java
package com.concretepage.persistence; import java.io.Serializable; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToMany; @Entity public class Country implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name="id") private int id; @Column(name="name") private String name; @OneToMany(cascade=CascadeType.ALL) @JoinColumn(name="country_id") private Set<State> states; public Country(int id,String name,Set<State> states){ this.id=id; this.name=name; this.states=states; } public Country(){ } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set<State> getStates() { return states; } public void setStates(Set<State> states) { this.states = states; } }
package com.concretepage.persistence; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "state") public class State { @Id @Column(name = "id") private int id; @Column(name = "name") private String name; public State(int id,String name){ this.id=id; this.name=name; } public State(){} public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } }
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.url"> jdbc:mysql://localhost:3306/hibernate</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password"></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">update</property> <property name="hibernate.cache.use_second_level_cache">true</property> <mapping class="com.concretepage.persistence.State"/> <mapping class="com.concretepage.persistence.Country"/> </session-factory> </hibernate-configuration>
package com.concretepage.util; import java.util.HashSet; import java.util.Set; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import com.concretepage.persistence.Country; import com.concretepage.persistence.State; public class HibernateUtil { private static final SessionFactory concreteSessionFactory; static { try { concreteSessionFactory = new AnnotationConfiguration() .configure().buildSessionFactory(); } catch (Throwable ex) { throw new ExceptionInInitializerError(ex); } } public static Session getSession() throws HibernateException { return concreteSessionFactory.openSession(); } public static void main(String... args){ Session session=getSession(); session.beginTransaction(); State s1= new State(1,"UP"); State s2= new State(2,"MP"); Set<State> states= new HashSet<State>(); states.add(s1); states.add(s2); Country country= new Country(1,"India",states); session.persist(country); session.getTransaction().commit(); session.close(); } }
Hibernate: insert into Country (name, id) values (?, ?) Hibernate: insert into state (name, id) values (?, ?) Hibernate: insert into state (name, id) values (?, ?) Hibernate: update state set country_id=? where id=? Hibernate: update state set country_id=? where id=?