Hibernate学习(八)---【多对多】

Hibernate学习(八)---【多对多】

举一个简单的例子:
Teacher.java
package  zzn.hibernate.model;

import  java.util.Set;

public   class  Teacher {
    
private   int  id;
    
private  String name;
    
private   int  age;
    
private  Set < Student >  student;

    
public  Set < Student >  getStudent() {
        
return  student;
    }

    
public   void  setStudent(Set < Student >  student) {
        
this .student  =  student;
    }

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

    
public   int  getAge() {
        
return  age;
    }

    
public   void  setAge( int  age) {
        
this .age  =  age;
    }

}
Student.java
package  zzn.hibernate.model;

import  java.util.Set;

public   class  Student {
    
private   int  id;
    
private  String name;
    
private   int  age;
    
private  Set < Teacher >  teacher;
    
    
public  Set < Teacher >  getTeacher() {
        
return  teacher;
    }

    
public   void  setTeacher(Set < Teacher >  teacher) {
        
this .teacher  =  teacher;
    }

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

    
public   int  getAge() {
        
return  age;
    }

    
public   void  setAge( int  age) {
        
this .age  =  age;
    }

}
Teacher.hbm.xml
<? xml version="1.0" encoding='gb2312' ?>   
<! DOCTYPE hibernate-mapping PUBLIC  
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>   
  
< hibernate-mapping  package ="zzn.hibernate.model" >   
    
< class  name ="Teacher"  table ="teacher"   >
        
< id  name ="id"  column ="id" >
            
< generator  class ="identity"   />
        
</ id >
        
        
< property  name ="name"   />
        
< property  name ="age"   />
        
< set  name ="Student"  table ="student_teacher" >
            
< key  column ="teacher_id"   />
            
< many-to-many  class ="Student"  column ="student_id"   />
        
</ set >
    
</ class >
</ hibernate-mapping >
Student.hbm.xml
<? xml version="1.0" encoding='gb2312' ?>   
<! DOCTYPE hibernate-mapping PUBLIC  
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>   
  
< hibernate-mapping  package ="zzn.hibernate.model" >   
    
< class  name ="Student"  table ="student"   >
        
< id  name ="id"  column ="id" >
            
< generator  class ="identity"   />
        
</ id >
        
        
< property  name ="name"   />
        
< property  name ="age"   />
        
        
< set  name ="Teacher"  table ="student_teacher" >
            
< key  column ="student_id"   />
            
< many-to-many  class ="Teacher"  column ="teacher_id"   />
        
</ set >
    
</ class >
</ hibernate-mapping >
hibernate-config.xml
<? xml version='1.0' encoding='gb2312' ?>
<! DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
>

< hibernate-configuration >
    
< session-factory >
        
< property  name ="show_sql" > true </ property >
        
< property  name ="hibernate.hbm2ddl.auto" > update </ property >
        
< property  name ="connection.username" > sa </ property >
        
< property  name ="connection.password" ></ property >
        
< property  name ="connection.url" > jdbc:jtds:sqlserver://localhost:1433;databasename=hibernate_test </ property >
        
< property  name ="dialect" > org.hibernate.dialect.SQLServerDialect </ property >
        
< property  name ="myeclipse.connection.profile" > SQL2005 </ property >
        
< property  name ="connection.driver_class" > net.sourceforge.jtds.jdbc.Driver </ property >
    
        
< mapping  resource ="zzn/hibernate/mappings/Teacher.hbm.xml" />
        
< mapping  resource ="zzn/hibernate/mappings/Student.hbm.xml" />
        
    
</ session-factory >

</ hibernate-configuration >
ManyToManyTest.java
package  zzn.hibernate.test;

import  java.util.ArrayList;
import  java.util.HashSet;
import  java.util.List;
import  java.util.Set;

import  org.hibernate.Session;
import  org.hibernate.SessionFactory;
import  org.hibernate.Transaction;
import  org.hibernate.cfg.Configuration;

import  zzn.hibernate.base.HibernateUtil;
import  zzn.hibernate.model.Student;
import  zzn.hibernate.model.Teacher;

public   class  ManyToManyTest {
    
public   static   void  main(String[] args) {
        add();
        get(
1 );
    }

    
public   static   void  add() {
        Configuration configuration 
=   null ;
        SessionFactory sessionFactory 
=   null ;
        Session session 
=   null ;
        Transaction transaction 
=   null ;

        Student student 
=   new  Student();
        Teacher teacher 
=   new  Teacher();

        
try  {
            configuration 
=   new  Configuration();
            sessionFactory 
=  configuration.configure().buildSessionFactory();
            session 
=  sessionFactory.openSession();
            transaction 
=  session.beginTransaction();
            
            Set
< Student >  stu  =   new  HashSet < Student > ();
            student.setName(
" zhaozhaonan " );
            student.setAge(
25 );
            stu.add(student);

            teacher.setName(
" zhangxiaolan " );
            teacher.setAge(
25 );
            
            teacher.setStudent(stu);//建立关联
            session.save(student);
            session.save(teacher);
        } 
finally  {
            
if  (session  !=   null ) {
                transaction.commit();
                session.close();
            }
        }
    }

    @SuppressWarnings(
" unchecked " )
    
public   static   void  get( int  id) {
        Session session 
=   null ;
        Transaction transaction 
=   null ;
        
try  {
            session 
=  HibernateUtil.getSession();
            transaction 
=  session.beginTransaction();
            Student student 
=  (Student)session.get(Student. class , id);
            List list 
=   new  ArrayList (student.getTeacher());
            
for ( int  i = 0 ;i < list.size();i ++ ){
                Teacher teacher 
=  (Teacher)list.get(i);
                System.out.println(teacher.getName());
            }
        } 
finally  {
            
if  (session  !=   null ) {
                transaction.commit();
                session.close();
            }
        }
    }
}

你可能感兴趣的:(Hibernate学习(八)---【多对多】)