addheader

Thursday, 18 October 2012

hibernate many to many xml mapping


Ex- 8:


//hibernate many to many  xml mapping 


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







Student.java



package madhav;

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

public class Course {

      private int c_id;
      private String c_name;
      private Set<Student> students=new HashSet<Student>(0);
      public int getC_id() {
            return c_id;
      }
      public void setC_id(int c_id) {
            this.c_id = c_id;
      }
      public String getC_name() {
            return c_name;
      }
      public void setC_name(String c_name) {
            this.c_name = c_name;
      }
      public Set<Student> getStudents() {
            return students;
      }
      public void setStudents(Set<Student> students) {
            this.students = students;
      }
     
}



Course.java



package madhav;

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

public class Student {

      private int s_id;
      private String s_name;
      private Set<Course> courses=new HashSet<Course>(0);
      public int getS_id() {
            return s_id;
      }
      public void setS_id(int s_id) {
            this.s_id = s_id;
      }
      public String getS_name() {
            return s_name;
      }
      public void setS_name(String s_name) {
            this.s_name = s_name;
      }
      public Set<Course> getCourses() {
            return courses;
      }
      public void setCourses(Set<Course> courses) {
            this.courses = courses;
      }
     
     
}



Course.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="Course" table="course">
   <id name="c_id" type="int" column="c_id" >
   <generator class="increment"/>
  </id>
  <property name="c_name">
   <column name="c_name" />
  </property>
 
  <set name="students" table="selectedcourse" inverse="false" cascade="all">

            <key >
                <column name="c_id" />
             </key>
            <many-to-many  column="s_id" class="Student" />
        </set>
 </class>
</hibernate-mapping>


Student.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="Student" table="student">
   <id name="s_id" type="int" column="s_id" >
   <generator class="increment"/>
  </id>
  <property name="s_name">
   <column name="s_name" />
  </property>
 
  <set name="courses" table="selectedcourse" inverse="false" cascade="all">

            <key >
                <column name="s_id" />
             </key>
            <many-to-many column="c_id" class="Course" />
        </set>
 </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>
        <property name="show_sql">true</property>
    <mapping resource="student.hbm.xml"/>
    <mapping resource="course.hbm.xml"/>
    </session-factory>

</hibernate-configuration>


TestSuport.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 {
                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();

Student s1=new Student();
s1.setS_name("madhva");
Student s2=new Student();
s2.setS_name("venkat");
Course c1=new Course();
c1.setC_name("hibernate");
Course c2=new Course();
c2.setC_name("spring");
s1.getCourses().add(c1);
s1.getCourses().add(c2);
s2.getCourses().add(c1);
s1.getCourses().add(c2);
session.save(s1);
session.save(s2);
                                 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