Java

Thursday, 18 October 2012

hibernate one to many xml mapping


Hibernate one to many XML - mapping:

EX -7:


     JAVA-SERVLETS     JAVA-JDBC     JAVA-JSP       HIBERNATE-SHCEMABASED 

    HIBERNATE-ANNOTATIONS     SPRING-IOC       SPRING –AOP-SCHEMABASED   

    SPRING-AOP-ANNOTATIONS      SPRING -DAO     SPRIN-MVC     SPRING-SECUTITY

     SPRING-DATA-JPA     REST-WEB-SERVICE     STRUTS2HIBERNATE    GWT.... 





Dept.java
package madhav;

import java.util.HashSet;
import java.util.Set;

public class Dept {

      private int deptno;
      private String dname;
      private String loc;
      private Set<Employee> employees=new HashSet<Employee>(0);
      public int getDeptno() {
            return deptno;
      }
     
      public Set<Employee> getEmployees() {
            return employees;
      }

      public void setEmployees(Set<Employee> employees) {
            this.employees = employees;
      }

      public void setDeptno(int deptno) {
            this.deptno = deptno;
      }
      public String getDname() {
            return dname;
      }
      public void setDname(String dname) {
            this.dname = dname;
      }
      public String getLoc() {
            return loc;
      }
      public void setLoc(String loc) {
            this.loc = loc;
      }
     
}



Employee.java



package madhav;

public class Employee {
       private int empno;
       private String ename;
       private Dept dept;
       public Dept getDept() {
            return dept;
      }
      public void setDept(Dept dept) {
            this.dept = dept;
      }
      public int getEmpno() {
             return empno;
       }
       public void setEmpno(int empno) {
             this.empno = empno;
       }
       public String getEname() {
             return ename;
       }
       public void setEname(String ename) {
             this.ename = ename;
       }
}


dept.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">
<hibernate-mapping package="madhav">
  <class name="Dept" table="dept">
   <id name="deptno" type="int" column="deptno" >
   <generator class="increment"/>
  </id>
  <property name="dname">
   <column name="dname" />
  </property>
  <property name="loc">
  <column name="loc"/>
  </property>
 
  <set name="employees" table="employee" inverse="true" cascade="all">

            <key >
                <column name="deptno" not-null="true"/>
             </key>
            <one-to-many  class="Employee" />
        </set>
 </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">

<hibernate-mapping package="madhav">
  <class name="Employee" table="employee">
   <id name="empno" type="int">
   <column name="empno"></column>
   <generator class="increment"/>
  </id>

  <property name="ename">
   <column name="ename" />
   </property>

  <many-to-one name="dept" class="Dept" fetch="select">
      <column name="deptno" not-null="true"></column>
  </many-to-one>
 </class>
</hibernate-mapping>


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">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/mysql</property>
        <property name="connection.username">madhav</property>
        <property name="connection.password">madhav</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <mapping resource="employee.hbm.xml"/>
    <mapping resource="dept.hbm.xml"/>
    </session-factory>

</hibernate-configuration>


TestSupport.java




import madhav.*;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.hibernate.cfg.*;
import org.hibernate.*;
import org.apache.log4j.*;
public class TestSupport {

                /**
                 * @param args
                 */
                public void insert()
                {
                                 System.out.println("execution started");
                                                TestCase t=new TestCase();
                                                Configuration cfg=new Configuration();
                                                cfg.configure();
                                                //cfg.addResource("employee.hbm.xml");
                                                //cfg.addResource("personaldetails.hbm.xml");
                                                SessionFactory factory=cfg.buildSessionFactory();
                 Session session=null;
                 Transaction tx=null;
                 try
                 {            
                                 session =factory.openSession();             
tx=session.beginTransaction();
         Dept dept = new Dept();
         dept.setDname("hugo");
         dept.setLoc("hyd");
                                    session.save(dept);
                                    Employee emp1 = new Employee();
                                    emp1.setEname("madhav");
                                    emp1.setDept(dept);
                                    Employee emp2 = new Employee();
                                    emp2.setEname("narendar");
                                    emp2.setDept(dept);
                        session.save(emp1);
                        session.save(emp2);
                                 tx.commit();
                                 System.out.println("record inserted");
                 }
                 catch(HibernateException e)
                 {            
                e.printStackTrace();       
                 }
                }
}


TestCase.java



import madhav.*;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.hibernate.cfg.*;
import org.hibernate.*;
import org.apache.log4j.*;
public class TestCase {

                /**
                 * @param args
                 */
                public static void main(String[] args) {
                                // TODO Auto-generated method stub
TestSupport ts=new TestSupport();
                                ts.insert();
                }
}


Output-console:











No comments:

Post a Comment