hibernate一对一,一对多,多对一,多对多关系映射与级联

总结一下hibernate的多种关系映射与级联
(1)one-to-many (一对多)
          实体类中one方封装一个set集合(set集合装的是many对象)作为属性,并初始化。实体many方封装一个one对象。
         以UserInfo---EmailInfo---LetterInfo 三张表来级联说明主控方的选择与cascade的配置
         1)生成配置文件。
          2)选择主控方,设置级联操作。(one-tomany,many-to-one 一般选择many一方作为主控方)
              inverse = false;表示主控方,关系表的维护由主控方维护。一般在one-to-many或many-to-many中设置
              cascade级联:属性值有:
                    none,all,save-update,delete, lock,refresh,evict,replicate,persist, merge,delete-orphan(one-to-many)
               inverse在双向关系中 inverse 只需要在一方配置即可(其中cascade级联与inverse一起使用)
           3)在级联查询与删除中cascade在set标签中配置。
          4)添加,修改cascade配置以主控方要影响的对象为根据配置。
例如:以Letter做主控方添加email,user。
  //LetterInfo.hbm.xml




    
        
            
            
        
        
            
        
        
         
            
        
    

//EmailInfo.hbm.xml




    
        
            
            
        
        
            
        
     
        
            
        
       
        
            
                
            
            
        
    
              
// UserInfo.hbm.xml




    
        
            
            
        
        
            
        
        
            
                
            
            
        
    

//test.java
/**
* 以letter为主控:(many方为主控,推荐)
* 在要影响的地方添加cascade
*/
public void testAddLetter(){
Session session = HibernateSessionFactory.getSession();
LetterInfo letter = new LetterInfo("华山论剑");

EmailInfo email = new EmailInfo("[email protected]");

UserInfo user = new UserInfo("黄蓉");

letter.setEmail(email);
email.setUser(user);

Transaction tx = session.beginTransaction();
session.save(letter);
tx.commit();
HibernateSessionFactory.closeSession();
}        
/**
* 删除用户同时删除邮箱与邮箱内的邮件
* letter为主控配置inverse,cascade在set中配置(与查询一样)
* 
*/
public void testDeleteUser(){
Session session = HibernateSessionFactory.getSession();
UserInfo user = (UserInfo) session.get(UserInfo.class,25);
//EmailInfo email = (EmailInfo)session.get(EmailInfo.class,2);
Transaction tx = session.beginTransaction();
session.delete(user);
tx.commit();
HibernateSessionFactory.closeSession();
}
 /**
     * 以user为主控:(one 方为主控)
     *  瞬时态数据持久化需要注意:
     *   对象数据需要双向设置
     *   
     *   
     */
public void testAddUser(){
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();

UserInfo user = new UserInfo("萨斯");
EmailInfo e1 = new EmailInfo("[email protected]");

LetterInfo letter = new LetterInfo("欢迎你,萨斯");
letter.setEmail(e1);
e1.getLetters().add(letter);
e1.setUser(user);
user.getEmails().add(e1);
session.save(user);
tx.commit();
HibernateSessionFactory.closeSession();
}
public void testQueryUser(){
Session session = HibernateSessionFactory.getSession();
UserInfo ui = (UserInfo)session.get(UserInfo.class, 1);
System.out.println(ui.getName());
   Setemails = ui.getEmails();
   for (EmailInfo ei : emails) {
System.out.println(ei.getAddress());
Set letters = ei.getLetters();
for (LetterInfo letter : letters) {
System.out.print("\t"+letter.getTitle());
}
System.out.println("");
}
   HibernateSessionFactory.closeSession();
}
     
public void testQueryEmail(){
Session session = HibernateSessionFactory.getSession();
LetterInfo letter = (LetterInfo)session.get(LetterInfo.class, 1);
System.out.println("信件:"+letter.getTitle());
EmailInfo email= letter.getEmail();
System.out.println("\t"+email.getAddress());
UserInfo user = email.getUser();
System.out.println("\t\t"+user.getName());
HibernateSessionFactory.closeSession();
}
        //one-to-one
     以cardInfo-personInfo
    //CardInfo.hbm.xml文件
    



    
        
            
            
        
       
        
            
        
    


//PersonInfo.hbm.xml




    
        
            
            
        
        
            
        
        
    

/**
* one-to-one
*   
*/
public void test5(){
Session session = HibernateSessionFactory.getSession();
PersonInfo person = (PersonInfo)session.get(PersonInfo.class,1);
   System.out.println(person.getName()+"\t"+person.getCard().getCode());
   HibernateSessionFactory.closeSession();
}
many-to-many
   以studentinfo-teacherInfo为例
//TeacherInfo.hbm.xml




    
        
            
            
        
        
            
        
        
            
            
        
    

//StudentInfo.hbm.xml




    
        
            
            
        
        
            
        
        
            
            
        
    


//java部分
/**
* many-to-many:级联操作之添加数据(包括添加瞬时态与持久态)
*/
public void test2(){
Session session = HibernateSessionFactory.getSession();
//添加瞬时态数据
// StudentInfo stu = new StudentInfo("郭靖");
// Set teachers = new HashSet();
// TeacherInfo t1 = new TeacherInfo("洪七公");
// TeacherInfo t2 = new TeacherInfo("江南七怪");

//根据持久态数据添加关系
Transaction transaction = session.beginTransaction();
TeacherInfo t1 = (TeacherInfo) session.get(TeacherInfo.class,1);
TeacherInfo t2 = (TeacherInfo) session.get(TeacherInfo.class,2);
Set teachers = new HashSet();
teachers.add(t1);
teachers.add(t2);
//以学生为主控方,学生维护关系表
StudentInfo stu = (StudentInfo) session.get(StudentInfo.class,1);
stu.setTeachers(teachers);
session.update(stu);
transaction.commit();
HibernateSessionFactory.closeSession();
}
/**
* many-to-many:级联操作之更新数据(删除)
*/
public void test3(){
Session session = HibernateSessionFactory.getSession();
//TeacherInfo ti = (TeacherInfo) session.get(TeacherInfo.class,5);

Transaction tx = session.beginTransaction();
StudentInfo  stu = (StudentInfo)session.get(StudentInfo.class, 1);
TeacherInfo  t  = (TeacherInfo)session.get(TeacherInfo.class, 2);
// session.delete(stu);//删除关系与学生
//仅删除关系,不删除老师
stu.getTeachers().remove(t);
session.update(stu);
tx.commit();
HibernateSessionFactory.closeSession();
}
/**
* 测试many-to-many:级联操作之更新数据(修改);
*/
public void test4(){
Session session = HibernateSessionFactory.getSession();
StudentInfo stu = (StudentInfo)session.get(StudentInfo.class, 1);
TeacherInfo t1 = (TeacherInfo)session.get(TeacherInfo.class,1);
TeacherInfo t2 = (TeacherInfo)session.get(TeacherInfo.class,2);
        stu.getTeachers().remove(t1);
        stu.getTeachers().add(t2);
Transaction tx = session.beginTransaction();
session.update(stu);
tx.commit();
HibernateSessionFactory.closeSession();
}










你可能感兴趣的:(hibernate)