<!--
如何去合理设置条件,帮助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); } }