Many-to-One :
Many-to-One relationship means many no.of employees having one address(same).Here multiple  Employee objects having one address object.

//creating two tables for many-to-one relatioship

create table EMPLOYEE ( id INT(11) NOT NULL auto_increment,  first_name VARCHAR(20),   last_name  VARCHAR(20),   salary     INT(11),   address    INT NOT NULL,   PRIMARY KEY (id));

create table ADDRESS ( id INT NOT NULL auto_increment, street_name VARCHAR(40),  city_name VARCHAR(40), state_name VARCHAR(40) default NULL,  zipcode VARCHAR(10),PRIMARY KEY (id));

Step1: developing pojo classes for Address and Employee Tables

Address.java

package util;


public class Address implements java.io.Serializable {

       // Fields

       private Integer id;
       private String streetName;
       private String cityName;
       private String stateName;
       private String zipcode;

       // Constructors

       /** default constructor */
       public Address() {
       }

       /** full constructor */
       public Address(String streetName, String cityName, String stateName,
                     String zipcode) {
              this.streetName = streetName;
              this.cityName = cityName;
              this.stateName = stateName;
              this.zipcode = zipcode;
       }

       // Property accessors

       // write here setters and getters methods for above property

}

Employee.java

package util;

public class Employee implements java.io.Serializable {

       // Fields
       private Integer id;
       private String firstName;
       private String lastName;
       private Integer salary;
       private Address address;

       // Constructors

       /** default constructor */
       public Employee() {
       }


       /** full constructor */
       public Employee(String firstName, String lastName, Integer salary,
                     Address address) {
              this.firstName = firstName;
              this.lastName = lastName;
              this.salary = salary;
              this.address = address;
       }

       //  provide setters and getters methods  all the property’s


}

Step 2:Develop a hbm files for Pojo classes Employee and Address tables
Address.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="util.Address" table="address" catalog="icepages">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        <property name="streetName" type="java.lang.String">
            <column name="street_name" length="40" />
        </property>
        <property name="cityName" type="java.lang.String">
            <column name="city_name" length="40" />
        </property>
        <property name="stateName" type="java.lang.String">
            <column name="state_name" length="40" />
        </property>
        <property name="zipcode" type="java.lang.String">
            <column name="zipcode" length="10" />
        </property>
    </class>
</hibernate-mapping>

Employee.hbm.xml


<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
write the m<many-to-one > tag for employee...
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="util.Employee" table="employee" catalog="icepages">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        <property name="firstName" type="java.lang.String">
            <column name="first_name" length="20" />
        </property>
        <property name="lastName" type="java.lang.String">
            <column name="last_name" length="20" />
        </property>
        <property name="salary" type="java.lang.Integer">
            <column name="salary" />
        </property>
        <many-to-one name="address" class="util.Address" column="address" not-null="true"></many-to-one>
    </class>
</hibernate-mapping>

             
Step3:
Develop a Dao class for how many employees having same address ….

package test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import util.Address;
import util.Employee;
import util.HibernateSessionFactory;


public class DAOAddress {
      
       public Address addAddress(String cityName,String stateName,String streetName,String zipCode )
       {
       Address address=null;
      
       SessionFactory sf=HibernateSessionFactory.getSessionFactory();
       Session s=sf.openSession();
      
       Transaction tx=s.beginTransaction();
      
       address=new Address();
       address.setCityName(cityName);
       address.setStreetName(streetName);
       address.setStateName(stateName);
       address.setZipcode(zipCode);
       Integer addressID=(Integer)s.save(address);
      
       tx.commit();
       return address;
      
       }
      
      
       public Integer addEmployee(String firstName,String lastName,Integer salary,Address address){
      
       Integer employeeId=null;
      
       SessionFactory sf=HibernateSessionFactory.getSessionFactory();
       Session s=sf.openSession();
      
       Transaction tx=s.beginTransaction();
      
       Employee employee=null;
       employee=new Employee();
       employee.setFirstName(firstName);
       employee.setLastName(lastName);
       employee.setSalary(salary);
       employee.setAddress(address);
      
       employeeId=(Integer)s.save(employee);
       tx.commit();
      
       return employeeId;
       }
}


Step4: develop a main class Test.java


package test;

import util.Address;
import util.Employee;

public class Test {

       /**
        * @param args
        */
       public static void main(String[] args) {
              // TODO Auto-generated method stub
             
              DAOAddress dao=new DAOAddress();
             
Address address=dao.addAddress
("hyd","hyd","ammerpet","500033");
Integer employee=dao.addEmployee("ashok","venna",34643,address);
       Integer employee1=dao.addEmployee("prudhvi","bnala",5525,address);
       }

}


Output:

All the Records are inserted….

0 comments:

Post a Comment