ONE-TO-MANY RELATIONSHIP IN HIBERNATE:





step1: create a tables like ...


table1:

create table employees(eid int(11) primary key,ename varchar(20),salary float(11));



table2:


create table address(aid int(11) primary key,addressno int(11),eid int(11),street varchar(20),state varchar(20),country varchar(20));

For alter table : 

alter table address add   foreign key(eid) references  employees(eid); 



step2: develop a correspondent pojo classess for tables like…

Employees.java


package com.dustbin.mapping;
// Generated Dec 19, 2013 1:10:05 PM by Hibernate Tools 3.2.1.GA
import java.util.HashSet;
import java.util.Set;
public class Employees  implements java.io.Serializable {
private int eid;
 private String ename;
 private Float salary;
 private Set addresses = new HashSet(0);
 public Employees() {
    }
public Employees(int eid) {
        this.eid = eid;
    }
    public Employees(int eid, String ename, Float salary, Set addresses) {
       this.eid = eid;
       this.ename = ename;
       this.salary = salary;
       this.addresses = addresses;
    }
   public int getEid() {
        return this.eid;
}
public void setEid(int eid) {
        this.eid = eid;
    }
public String getEname() {
        return this.ename;
    }
    public void setEname(String ename) {
        this.ename = ename;
    }
    public Float getSalary() {
        return this.salary;}
   
    public void setSalary(Float salary) {
        this.salary = salary;
    }
    public Set getAddresses() {
        return this.addresses;
    }
    public void setAddresses(Set addresses) {
        this.addresses = addresses;
    }
}


Address.java
package com.dustbin.mapping;
// Generated Dec 19, 2013 1:10:05 PM by Hibernate Tools 3.2.1.GA
public class Address  implements java.io.Serializable {
private int aid;
     private Employees employees;
     private Integer addressno;
     private String street;
     private String state;
     private String country;
public Address() {
    }
public Address(int aid) {
        this.aid = aid;
    }
    public Address(int aid, Employees employees, Integer addressno, String street, String state, String country) {
       this.aid = aid;
       this.employees = employees;
       this.addressno = addressno;
       this.street = street;
       this.state = state;
       this.country = country;
    }
  
    public int getAid() {
        return this.aid;
    }
   
    public void setAid(int aid) {
        this.aid = aid;
    }
    public Employees getEmployees() {
        return this.employees;
    }
   
public void setEmployees(Employees employees) {
        this.employees = employees;
    }
    public Integer getAddressno() {
        return this.addressno;
    }
    public void setAddressno(Integer addressno) {
        this.addressno = addressno;
    }
    public String getStreet() {
        return this.street;
    }
    public void setStreet(String street) {
        this.street = street;
    }
    public String getState() {
        return this.state;
    }
    public void setState(String state) {
        this.state = state;
    }
    public String getCountry() {
        return this.country;
    }
     public void setCountry(String country) {
        this.country = country;
    }
}


Employees.hbm.xml


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Dec 19, 2013 1:10:18 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
    <class name="com.dustbin.mapping.Employees" table="employees" catalog="ashok">
        <id name="eid" type="int">
            <column name="eid" />
            <generator class="assigned" />
        </id>
        <property name="ename" type="string">
            <column name="ename" length="20" />
        </property>
        <property name="salary" type="java.lang.Float">
            <column name="salary" precision="12" scale="0" />
        </property>
        <set name="addresses" inverse="true" cascade="all" lazy="false">
            <key>
                <column name="eid" />
            </key>
            <one-to-many class="com.dustbin.mapping.Address" />
        </set>
    </class>
</hibernate-mapping>


Address.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Dec 19, 2013 1:10:18 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
    <class name="com.dustbin.mapping.Address" table="address" catalog="ashok">
        <id name="aid" type="int">
            <column name="aid" />
            <generator class="assigned" />
        </id>
        <many-to-one name="employees" class="com.dustbin.mapping.Employees" fetch="select">
            <column name="eid" />
        </many-to-one>
        <property name="addressno" type="java.lang.Integer">
            <column name="addressno" />
        </property>
        <property name="street" type="string">
            <column name="street" length="20" />
        </property>
        <property name="state" type="string">
            <column name="state" length="20" />
        </property>
        <property name="country" type="string">
            <column name="country" length="20" />
        </property></class></hibernate-mapping>


HibernateUtil.java

package com.dustbin.util;
import org.hibernate.cfg.Configuration;
import org.hibernate.SessionFactory;

public class HibernateUtil {
private static final SessionFactory sessionFactory;
    static {
        try {
            // Create the SessionFactory from standard (hibernate.cfg.xml)
            // config file.
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Log the exception.
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
   public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

InsertRecordsOneToMany.java

package com.dustbin.main;

import com.dustbin.mapping.Address;
import com.dustbin.mapping.Employees;
import com.dustbin.util.HibernateUtil;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
public class InsertRecordsOneToMany {
    public static void main(String dg[]){
        SessionFactory sf=HibernateUtil.getSessionFactory();
        Session s=sf.openSession();
        Transaction tx=s.beginTransaction();
        Employees e=new Employees();
        e.setEid(2);
        e.setEname("mazaa");
        e.setSalary(2400);
//for ONE-TO-MANY RELATIONSHIP(ONE EMPLOYEE CONTAINS    MULTIPLE addresses
        Address a=new Address();  //first address
            //here aid not give like 1,2 every time,at that time address table was overridden.So every time give 1,2,....for every employees
       
        a.setAid(3);
        a.setAddressno(1);
        a.setCountry("India");
        a.setState("ap");
        a.setStreet("ammerpet");
        a.setEmployees(e);
        Set set=e.getAddresses();
       
        Address a1=new Address(); //for second address
        a1.setAid(4);
        a1.setAddressno(2);
        a1.setCountry("India");
        a1.setState("ap");
        a1.setStreet("vijayawada");
        a1.setEmployees(e);
        Set set1=e.getAddresses();
        set.add(a);
        set1.add(a1);
       s.save(e);
        tx.commit();
       }
   }








hibernate.cfg.xml




<?xml version="1.0" encoding="UTF-8"?>
<!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.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ashok</property>
    <property name="hibernate.connection.username">root</property>
    <mapping resource="com/dustbin/mapping/Employees.hbm.xml"/>
    <mapping resource="com/dustbin/mapping/Address.hbm.xml"/>
  </session-factory>
</hibernate-configuration>




output:





0 comments:

Post a Comment