Hiberate--one to many

<!--

如何去合理设置条件,帮助hibernate系统得到属于这个post的所有的reply?
1. table="forumreply"
select * from forumreply

2. <key column="post_id"></key>
这是一个过滤条件
select * from forumreply where post_id=@forumpost.postId

3. <one-to-many class="ForumReply"/>
把前二步的查询所得到的记录,按照ForumReply这个类的映射机制来封装成ForumReply对象。

4. cascade="all"
级联操作,当系统对forumpost做操作的时候,也将一起对forumreply做操作。

5. inverse="true"
代表关系的维护动作转交给对象

6. fetch="join"
代表该属性的获取模式 ,如果没有设置,多方往往是新开一条语句来获取。

7. lazy="true"
懒惰加载技术,多方往往数量众多,加载一方的时候,我们一般可以不加载多方, 但加载某个多方的记录,往往一方要一并取出来。
懒惰加载技术有利于提高性能,只有发现确实需要加载多方的时候采取执行SQL语句,执行对象的加载。
lazy="true"是默认值
-->
<set name="replys" table="forumreply" cascade="all" inverse="true" fetch="join" lazy="true">
  <key column="post_id"></key>      <!-- 外键 : forum_reply这张表的外键字段名-->
  <one-to-many class="ForumReply"/>   <!-- 封装方式 -->
</set>

 

 

public void testAddForumPost() throws Exception{
        
        ForumPost post = new ForumPost();
        post.setPostContent("昨天听说PX装置爆炸了?");
        post.setPostName("有没有污染?");
        post.setPostTime(new Date());
        
        ForumReply reply1= new ForumReply();
        reply1.setReplyContent("漳州的,我这里距离很远!");
        reply1.setReplyTime(new Date());
        
        ForumReply reply2= new ForumReply();
        reply2.setReplyContent("不知道啊,应该还好!");
        reply2.setReplyTime(new Date());        
        
        
        post.getReplys().add(reply1); 
        post.getReplys().add(reply2);
        
        reply1.setPost(post);      //多的一方要维护关系,添加外键
        reply2.setPost(post);
        
        Transaction trans = session.beginTransaction();
        
        try{
          
          session.save(post);
          trans.commit();
            
        }catch(HibernateException e){
          trans.rollback();
          e.printStackTrace();
        }
        
        
    }
    
    
    public void testLoadForumPost() throws Exception{
        
        Transaction trans = session.beginTransaction();
        
        try{
          
          ForumPost post=(ForumPost)session.get(ForumPost.class, 2);
          
          System.out.println("post name:"+post.getPostName()+",reply count:"+post.getReplys().size());
          
          trans.commit();
            
        }catch(HibernateException e){
          trans.rollback();
          e.printStackTrace();
        }
                
    }
    
    
    public void testLoadAllForumPosts() throws Exception{
        
        Transaction trans = session.beginTransaction();
        
        try{
          
          String hql="from ForumPost f  order by f.postId desc";
          List<ForumPost> forumPostList=session.createQuery(hql).list();
          
          System.out.println("post list count:"+forumPostList.size());
          
          if(forumPostList.size()>1)
              forumPostList.get(0).getReplys().size();
          
          trans.commit();
            
        }catch(HibernateException e){
          trans.rollback();
          e.printStackTrace();
        }
                
    }    
    
    
    public void testUpdateForumPost() throws Exception{
        
        Transaction trans = session.beginTransaction();
        
        try{
          
          ForumPost post=(ForumPost)session.get(ForumPost.class, 2);
          post.setPostName(post.getPostName()+"~bacdefg");
          
          Set<ForumReply> replys = post.getReplys();
          for(ForumReply reply:replys)
              reply.setReplyContent(reply.getReplyContent()+"123456");
          
          session.saveOrUpdate(post);
          
          
          trans.commit();
            
        }catch(HibernateException e){
          trans.rollback();
          e.printStackTrace();
        }
                
    }
    
    
     public void testRemoveForumPost() throws Exception{
        
        Transaction trans = session.beginTransaction();
        
        try{
          
          ForumPost post=(ForumPost)session.load(ForumPost.class, 1);
          
          session.delete(post);
          
          
          trans.commit();
            
        }catch(HibernateException e){
          trans.rollback();
          e.printStackTrace();
        }
                
    }
     
     
     public void testRemoveForumReply() throws Exception{
            
            Transaction trans = session.beginTransaction();
            
            try{
              
              ForumPost post=(ForumPost)session.get(ForumPost.class, 2);
              
              ForumReply reply = (ForumReply)post.getReplys().toArray()[0];
              
              post.getReplys().remove(reply);      //多方删除也要在一方删除
              System.out.println(post.getReplys().size());
              reply.setPost(null);
              
              session.delete(reply);
              
              
              trans.commit();
                
            }catch(HibernateException e){
              trans.rollback();
              e.printStackTrace();
            }
                    
    }    
     
     
     public void testLoadSpecialPost() throws Exception{
            
            Transaction trans = session.beginTransaction();
            
            try{
              
              String hql="from ForumPost fp where fp.postId in (select distinct post.postId from ForumReply fr where fr.replyContent like '%不%')";
              List<ForumPost> forumPosts=(List<ForumPost>) session.createQuery(hql).list();
              
              for(ForumPost post:forumPosts){
                 System.out.println("post id:"+post.getPostId()+",post name:"+post.getPostName()+",reply count:"+post.getReplys().size());
                 session.delete(post);
              }
              
              trans.commit();
                
            }catch(HibernateException e){
              trans.rollback();
              e.printStackTrace();
              throw new Exception(e);
            }
                    
    }

 

  


转载于:https://www.cnblogs.com/QinH/p/4411242.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值