addheader

Sunday, 27 January 2013

hibernate manytomany annotations




hibernate manytomany annotations:
----------------------------------


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






Course .java
----------------



package madhav;

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

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

@Entity
@Table(name="course")
public class Course {
@Id
@GeneratedValue
private int c_id;
private String c_name;

@ManyToMany( cascade = CascadeType.ALL)
@JoinTable(
name = "selectedstudent", joinColumns ={
@JoinColumn(name = "c_id", nullable = false, updatable = false)},
inverseJoinColumns =  { @JoinColumn(name = "s_id")
}
)

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;
}

}





Student .java
---------------



package madhav;

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

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

@Entity
@Table(name="student")
public class Student {
@Id
@GeneratedValue
private int s_id;
private String s_name;
@ManyToMany( cascade = CascadeType.ALL)
@JoinTable(
name = "selectedstudent", joinColumns ={
@JoinColumn(name = "s_id", nullable = false, updatable = false)},
inverseJoinColumns =  { @JoinColumn(name = "c_id")
}
)

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;
}


}






hibernate.hbm.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">root</property>
        <property name="connection.password">mysql</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
 
    <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">create</property>
        <mapping class="madhav.Student" />
        <mapping class="madhav.Course"/>
    </session-factory>

</hibernate-configuration>






TestCase.java
----------------





package madhav;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.classic.Session;

public class TestCase {

           /**
            * @param args
            */
           public static void main(String[] args) {
                       // TODO Auto-generated method stub
                        try {
                                     // Create the SessionFactory from hibernate.cfg.xml
                                     SessionFactory  sf= new
                                     AnnotationConfiguration().configure().buildSessionFactory();
                                     Session s=sf.openSession();
                                     Transaction tx=s.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);


                                     s.save(s1);
                                     s.save(s2);
                                     tx.commit();
                                 
                                     } catch (Throwable ex) {
                                     // Make sure you log the exception, as it might be swallowed
                                     System.err.println("Initial SessionFactory creation failed." + ex);
                               
                                     }
                                     }

           }






output from console:
---------------------





Related Topics:

  1. hibernate-basic-example(xml-based)
  2. hibernate-inheritance-tableforclass-hierarchy(xml-based)
  3. hibernate-inheritance-joinsubclass-(xml-based)
  4. hibernate-inheritance-tableforconcreteclass(xml-based)
  5. hibernate-onetoone-relation(xml-based)
  6. hibernate-onetomany-relation(xml-based)
  7. hibernate-manytomany-relation(xml-based)
  8. hibernate-basic-example(Annotation-based)
  9. hibernate-inheritance-tableforclass-hierarchy(Annotation-based)
  10. hibernate-inberitance-joinsubclass-(Annotation-based)
  11. hibernate-inheritance-tableforconcreteclass(Annotation-based)
  12. hibernate-onetoone-relation(Annotation-based)
  13. hibernate-onetomany-relation(Annotation-based)
  14. hibernate-manytomany-relation(Annotation-based)



















































No comments:

Post a Comment