Hibernate学习(六)---【一对多双项】

Hibernate学习(六)---【一对多双项】

package  com.zzn.hibernate.oneToMany;

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;
    }
}
package  com.zzn.hibernate.oneToMany;

import  java.util.Set;

public   class  Classes {
    
private   int  id;
    
private  String name;
    
private  Set students;

    
public  Set getStudents() {
        
return  students;
    }
    
public   void  setStudents(Set students) {
        
this .students  =  students;
    }
    
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;
    }
    
    
}
<? xml version = " 1.0 " ?>
<! DOCTYPE hibernate - mapping PUBLIC 
    
" -//Hibernate/Hibernate Mapping DTD 3.0//EN "
    
" http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd " >
< hibernate - mapping >

    
< class  name = " com.zzn.hibernate.oneToMany.Student "  table = " STUDENT " >
        
< id name = " id "  column = " id " >
            
< generator  class = " native "   />
        
</ id >
        
< property name = " name "   />
        
<!--  一对多双向关联,在多的这一段必须有column,并且和一的一段中 < key /> 中的column = " classesid " 一样  -->
        
< many - to - one name = " classes "  column = " classesid "  cascade = " all " />
    
</ class >

</ hibernate - mapping >
<? xml version="1.0" ?>
<! DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
< hibernate-mapping  package ="com.zzn.hibernate.oneToMany" >
    
< class  name ="Classes"  table ="CLASSES" >
        
< id  name ="id"  column ="id" >
            
< generator  class ="native"   />
        
</ id >
        
< property  name ="name"   />
        
<!-- cascade="all"才能操作classes时级联操作student   -->
        
< set  name ="students"  cascade ="all"  inverse ="true" >
            
<!--  key 这里不能是not-null="true"  -->
            
< key  column ="classesid" />
            
< one-to-many  class ="Student" />
        
</ set >
    
</ class >
</ hibernate-mapping >

TEST
     public   void  OneToManySaveN() {
        Session session 
=   null ;
        Transaction tx 
=   null ;
        
try  {
            session 
=  HibernateUtils.getSession();
            tx 
=  session.beginTransaction();
            
            Classes classes 
=   new  Classes();
            classes.setName(
" 俄罗斯 " );
            
            Student student 
=   new  Student();
            student.setName(
" 列宁 " );
            student.setClasses(classes);
            session.save(student);
            
        } 
catch  (Exception e) {
            e.printStackTrace();
        } 
finally  {
            
if  (session  !=   null ) {
                
if  (session.isOpen()) {
                    tx.commit();
                    session.close();
                }
            }
        }
    }
    
    
public   void  OneToManyLoadN() {
        Session session 
=   null ;
        Transaction tx 
=   null ;
        
try  {
            session 
=  HibernateUtils.getSession();
            tx 
=  session.beginTransaction();
            
            Student    student 
=  (Student)session.load(Student. class 6 );
            System.out.println(student.getName());
            System.out.println(student.getClasses().getName());
            
        } 
catch  (Exception e) {
            e.printStackTrace();
        } 
finally  {
            
if  (session  !=   null ) {
                
if  (session.isOpen()) {
                    tx.commit();
                    session.close();
                }
            }
        }
    }

你可能感兴趣的:(Hibernate学习(六)---【一对多双项】)