show
hide

Java Hibernate Tutorial Part 5

In this tutorial I am going to explain how to get one to many relationship in the hibernate framework we take two database tables states and cities.

So one state may contain many cities which is an example of one to many relationship. Now in our program


We use the concept of collection object called List .List belongs to Java. util package you can learn more about it here.


The list is an indexed collection and thus we have had the column index in the Cities table for the index of the list.


Having the index column is compulsory since we have to specify it in our mapping file.


Also I have explained the concept of lazy by giving a beautiful explanation. So to get started watching the tutorial below.


States.java


import java.io.Serializable;
import java.util.List;
public class States implements Serializable{
    private int id;
    private String name;
    private List city;
    public States(){}
    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 List getCity() {
        return city;
    }
    public void setCity(List city) {
        this.city = city;
    }
Cities.java
import java.io.Serializable;
public class Cities implements Serializable{
    private int id;
    private String name;
    public Cities(){}
    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;
    }
    }
States.hbm.xml









    
    
    
    


 







    
Insertr.java
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Insertr {
     /**
     * @param args
     */
    public static void main(String[] args) {
         // TODO Auto-generated method stub
        Configuration cfg = new Configuration();
        cfg.configure(“States.cfg.xml”);
        SessionFactory sf = cfg.buildSessionFactory();
        Session s = sf.openSession();
        Transaction tx = s.beginTransaction();
        States st = new States();
        st.setId(2);
        st.setName(“ap”);
         Cities ct1 = new Cities();
         ct1.setId(4);
         ct1.setName(“hyd”);
         Cities ct2 = new Cities();
         ct2.setId(5);
         ct2.setName(“vij”);
         Cities ct3 = new Cities();
         ct3.setId(6);
         ct3.setName(“saasdf”);
         List c = new ArrayList();
         c.add(ct1);
         c.add(ct2);
         c.add(ct3);
         st.setCity(c);
         s.save(st);
         s.save(ct1);
         s.save(ct2);
         s.save(ct3);
         tx.commit();
         s.close();
    }
}
Selectr.java
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class selectr {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Configuration cfg = new Configuration();
        cfg.configure(“States.cfg.xml”);
        SessionFactory sf = cfg.buildSessionFactory();
        Session s = sf.openSession();
        Transaction tx = s.beginTransaction();
        States st = (States)s.get(States.class,new String(“Texas”));
        System.out.println(“after here 1″);
        List city= st.getCity();
        for(Cities  c:city)
        {
            System.out.println(c.getName());
        }
        s.close();
    }
}
Press Esc to close