hibernate4--04单向多对一以及单向一对多的关联映射

一丶单向多对一

1. 学生表student 和年级表 grade

image

2. 类Student 和Grade类

Student:

public class Student {
    private int id;
    private String name;
    private int age;
    private Grade grade;
//get…set
}

Grade:

public class Grade {
    private int id;
    private String name;
    //get…set
}

3. 映射文件

Student.hbm.xml


    
        
            
        
        
        
        
              


Grade.hbm.xml


    
        
            
        
        
    

4. 测试代码

@Test
    public void testSave() throws HibernateException, SerialException, SQLException{
        Session session = null;
        Transaction tx = null;
        try{
            session = HibernateUtil.getSession();
            tx = session.beginTransaction();
            Grade grade = new Grade();
            grade.setName("基础");
            session.save(grade);
            Student stu = new Student();
            stu.setName("张三疯");
            stu.setAge(22);
            stu.setGrade(grade);
            session.save(stu);
            
            tx.commit();
            
        }catch (HibernateException e) {
            if(tx!=null)
                tx.rollback();
            e.printStackTrace();
            throw e;
        }finally{
            HibernateUtil.closeSession();
        }
    }

二丶单向一对多

1. 类Student 和Grade类

Student:

public class Student {
    private int id;
    private String name;
    private int age;
//get…set
}

Grade:

public class Grade {
    private int id;
    private String name;
    private Set students = new HashSet(0);    //get…set
}

2. 映射文件

Student.hbm.xml


    
        
            
        
        
        
    


Grade.hbm.xml


    
        
            
        
        
        
        
            
            
            
            
        
    

3. 测试代码

@Test
    public void testSave() throws HibernateException, SerialException, SQLException{
        Session session = null;
        Transaction tx = null;
        try{
            session = HibernateUtil.getSession();
            tx = session.beginTransaction();
            Grade grade = new Grade();
            grade.setName("基础");
            
            Student stu = new Student();
            stu.setName("张三疯");
            stu.setAge(22);
            Student stu1 = new Student();
            stu1.setName("老王");
            stu1.setAge(23);
            //关联
            grade.getStudents().add(stu);
            grade.getStudents().add(stu1);
            //保存数据的顺序 是根据外键的配置来决定的
            //如果外键不能为null,那么先保存一的一端
            //如果外键可以为null,则可以随意保存
            session.save(grade);
            session.save(stu);
            session.save(stu1);
            
            tx.commit();
            
        }catch (HibernateException e) {
            if(tx!=null)
                tx.rollback();
            e.printStackTrace();
            throw e;
        }finally{
            HibernateUtil.closeSession();
        }
    }
    @Test
    public void testGet(){
        Session session = null;
        Transaction tx = null;
        try{
            session = HibernateUtil.getSession();
            tx = session.beginTransaction();
            //取数据
            Grade grade = (Grade)session.get(Grade.class, 1);
            System.out.println("gradeName="+grade.getName());
            System.out.println("grade所对应的多的一端的数据");
            Iterator iter = grade.getStudents().iterator();
            for(;iter.hasNext();){
                Student temp = iter.next();
                System.out.println("name="+temp.getName()+"\tage="+temp.getAge());
            }
            tx.commit();
        }catch (HibernateException e) {
            if(tx!=null)
                tx.rollback();
            e.printStackTrace();
            throw e;
        }finally{
            HibernateUtil.closeSession();
        }
    }

你可能感兴趣的:(hibernate4--04单向多对一以及单向一对多的关联映射)