9.hibernate 매핑(다대다)

12963 단어
다대다도 다음과 같다.
  • 단방향 다대다
  • 양방향 멀티 쌍
  • 사례 설명:
    한 학생은 여러 반의 수업을 들을 수 있고, 한 반에도 여러 학생이 있을 수 있다
    먼저 실체류를 보도록 하겠습니다.
    classes.java 클래스
        package entity;
        
        import java.util.Set;
        
        /**
         *      classes   ,   ,               ,          
         * @author arkulo
         *
         */
        public class Classes {
            private int id;
            private String className;
            private Set student;
            
            
            
            public int getId() {
                return id;
            }
            public void setId(int id) {
                this.id = id;
            }
            public String getClassName() {
                return className;
            }
            public void setClassName(String className) {
                this.className = className;
            }
            public Set getStudent() {
                return student;
            }
            public void setStudent(Set student) {
                this.student = student;
            }
        }
    

    Student.java 학생류
        package entity;
        
        public class Student {
            private int id;
            private String name;
        //  private Classes classes;
            
            
        //  public Classes getClasses() {
        //      return classes;
        //  }
        //  public void setClasses(Classes classes) {
        //      this.classes = classes;
        //  }
            public int getId() {
                return id;
            }
            public void setId(int id) {
                this.id = id;
            }
            public String getName() {
                return name;
            }
            public void setName(String name) {
                this.name = name;
            }
        }
    

    일방 다대다
    Classes.hbm.xml
        
        
        
            
                
                    
                
                
                
                
                    
                    
                
                
        
    

    여기에 중간 표의 작성법을 주의해야 한다. 테이블은 표의 이름을 확정하고 키 탭의column 속성 설정은classes 키를 연결하고 manytomany 탭의column 속성 설정은student 테이블의 키를 연결한다.현재 대상 간의 관련 관계는classes에서 유지보수하고 있습니다. 학생에게 유지보수 관계가 있습니까? 아래의 학생t의 맵 파일을 보십시오
    student.hbm.xml
        
        
        
            
                
                    
                
                
                
        
    

    단원 테스트:package entity;
        import java.util.HashSet;
        import java.util.Iterator;
        import java.util.Set;
        
        import org.hibernate.classic.Session;
        
        import junit.framework.TestCase;
        import util.hibernateUtil;
        
        /**
         *        
         *     :
         * 1.     
         * 2.     
         * 3.     
         * 
         * @author arkulo
         *
         */
        
        public class testManyToMany extends TestCase {
        //    ,  ,  classes      
            public void test1() {
                Session session = null;
        
                try {
                    session = hibernateUtil.getSession();
                    session.beginTransaction();
        //             3     ,        ,          
                    
                    Student st1 = new Student();
                    st1.setName("  ");
                    Student st2 = new Student();
                    st2.setName("  ");          
                    Student st3 = new Student();
                    st3.setName("  ");  
                    session.save(st1);
                    session.save(st2);
                    session.save(st3);
                    
                    Set jihe = new HashSet();
                    jihe.add(st1);
                    jihe.add(st2);
                    jihe.add(st3);
                    
                    Classes cl = new Classes();
                    cl.setClassName("     ");
                    cl.setStudent(jihe);
                                
                    session.save(cl);
        
                    session.getTransaction().commit();
                } catch (Exception e) {
                    e.printStackTrace();
                    session.getTransaction().rollback();
                } finally {
                    hibernateUtil.closeSession(session);
                }
            }
            
            
        //            
            public void test2()
            {
                Session session = null;
        
                try {
                    session = hibernateUtil.getSession();
                    session.beginTransaction(); 
        //                      sql  ,   。                ,
        //                  sql  ,            
                    Classes cl = (Classes)session.get(Classes.class, 1);
                    System.out.println("    :"+cl.getClassName());
                    Iterator it = cl.getStudent().iterator();
                    while(it.hasNext())
                    {
                        Student student = it.next();
                        System.out.println("    :"+student.getName());
                    }
                    
                    session.getTransaction().commit();
                } catch (Exception e) {
                    e.printStackTrace();
                    session.getTransaction().rollback();
                } finally {
                    hibernateUtil.closeSession(session);
                }       
            }
            
            
        //             ?         
            public void test3()
            {
                test1();
                System.out.println("------------------------------------------");
                Session session = null;
        
                try {
                    session = hibernateUtil.getSession();
                    session.beginTransaction(); 
        //                      sql  ,   。                ,
        //                  sql  ,            
                    Classes cl = (Classes)session.get(Classes.class, 1);
                    System.out.println("    :"+cl.getClassName());
                    Iterator it = cl.getStudent().iterator();
                    while(it.hasNext())
                    {
                        Student student = it.next();
                        System.out.println("    :"+student.getName());
                    }
                    
        //                                   ,         ,            
        //             ,     sql    :
        //          Hibernate: delete from _relation_classes_student where classes_id=?
        //          Hibernate: delete from Classes where id=?
        //                           ,              
                    session.delete(cl);
                    
                    session.getTransaction().commit();
                } catch (Exception e) {
                    e.printStackTrace();
                    session.getTransaction().rollback();
                } finally {
                    hibernateUtil.closeSession(session);
                }       
            }   
        }
    

    2. 쌍방향 다대다
    student.hbm.xml
        
        
        
            
                
                    
                
                
                
                    
                    
                
                
        
    

    유닛 테스트:
        package entity;
        
        import java.util.HashSet;
        import java.util.Iterator;
        import java.util.Set;
        
        import org.hibernate.classic.Session;
        
        import junit.framework.TestCase;
        import util.hibernateUtil;
        
        /**
         *        
         *     :
         *             ,              
         * 1.     
         * 2.     
         * 3.     
         * 
         * @author arkulo
         *
         */
        
        public class testManyToMany extends TestCase {
        //    ,  ,  classes      
            public void test1() {
                Session session = null;
        
                try {
                    session = hibernateUtil.getSession();
                    session.beginTransaction();
        //             3     ,        ,          
                    
                    Student st1 = new Student();
                    st1.setName("  ");
                    Student st2 = new Student();
                    st2.setName("  ");          
                    Student st3 = new Student();
                    st3.setName("  ");  
                    session.save(st1);
                    session.save(st2);
                    session.save(st3);
        
                    Classes cl = new Classes();
                    cl.setClassName("   ");
                    Classes cl1 = new Classes();
                    cl1.setClassName("   ");
                    Classes cl2 = new Classes();
                    cl2.setClassName("    ");       
                    
        //                 
                    Set jihe = new HashSet();
                    jihe.add(st1);
                    jihe.add(st2);
                    cl.setStudent(jihe);
        //                 
                    Set jihe1 = new HashSet();
                    jihe1.add(st2);
                    jihe1.add(st3);
                    cl1.setStudent(jihe1);          
        //                  
                    Set jihe2 = new HashSet();
                    jihe2.add(st1);
                    jihe2.add(st3);
                    cl2.setStudent(jihe2);                  
        
                                
                    session.save(cl);
                    session.save(cl1);
                    session.save(cl2);
        
                    session.getTransaction().commit();
                } catch (Exception e) {
                    e.printStackTrace();
                    session.getTransaction().rollback();
                } finally {
                    hibernateUtil.closeSession(session);
                }
            }
            
            
        //            
            public void test2()
            {
                test1();
                System.out.println("------------------------------");
                Session session = null;
        
                try {
                    session = hibernateUtil.getSession();
                    session.beginTransaction(); 
        //                      sql  ,   。                ,
        //                  sql  ,            
                    Classes cl = (Classes)session.get(Classes.class, 1);
                    System.out.println("    :"+cl.getClassName());
                    Iterator it = cl.getStudent().iterator();
                    while(it.hasNext())
                    {
                        Student student = it.next();
                        System.out.println("    :"+student.getName());
                    }
                    
                    session.getTransaction().commit();
                } catch (Exception e) {
                    e.printStackTrace();
                    session.getTransaction().rollback();
                } finally {
                    hibernateUtil.closeSession(session);
                }       
            }
            
            
        //             ?         
            public void test3()
            {
                test1();
                System.out.println("------------------------------------------");
                Session session = null;
        
                try {
                    session = hibernateUtil.getSession();
                    session.beginTransaction(); 
        //                      sql  ,   。                ,
        //                  sql  ,            
                    Classes cl = (Classes)session.get(Classes.class, 1);
                    System.out.println("    :"+cl.getClassName());
                    Iterator it = cl.getStudent().iterator();
                    while(it.hasNext())
                    {
                        Student student = it.next();
                        System.out.println("    :"+student.getName());
                    }
                    
        //                                   ,         ,            
        //             ,     sql    :
        //          Hibernate: delete from _relation_classes_student where classes_id=?
        //          Hibernate: delete from Classes where id=?
        //                           ,              
                    session.delete(cl);
                    
                    session.getTransaction().commit();
                } catch (Exception e) {
                    e.printStackTrace();
                    session.getTransaction().rollback();
                } finally {
                    hibernateUtil.closeSession(session);
                }       
            }   
        }
    

    좋은 웹페이지 즐겨찾기