Hiberate--one to many
<!--
如何去合理設(shè)置條件,幫助hibernate系統(tǒng)得到屬于這個(gè)post的所有的reply?
1. table="forumreply"
select * from forumreply
2. <key column="post_id"></key>
這是一個(gè)過(guò)濾條件
select * from forumreply where post_id=@forumpost.postId
3. <one-to-many class="ForumReply"/>
把前二步的查詢所得到的記錄,按照ForumReply這個(gè)類的映射機(jī)制來(lái)封裝成ForumReply對(duì)象。
4. cascade="all"
級(jí)聯(lián)操作,當(dāng)系統(tǒng)對(duì)forumpost做操作的時(shí)候,也將一起對(duì)forumreply做操作。
5. inverse="true"
代表關(guān)系的維護(hù)動(dòng)作轉(zhuǎn)交給對(duì)象
6. fetch="join"
代表該屬性的獲取模式 ,如果沒(méi)有設(shè)置,多方往往是新開(kāi)一條語(yǔ)句來(lái)獲取。
7. lazy="true"
懶惰加載技術(shù),多方往往數(shù)量眾多,加載一方的時(shí)候,我們一般可以不加載多方, 但加載某個(gè)多方的記錄,往往一方要一并取出來(lái)。
懶惰加載技術(shù)有利于提高性能,只有發(fā)現(xiàn)確實(shí)需要加載多方的時(shí)候采取執(zhí)行SQL語(yǔ)句,執(zhí)行對(duì)象的加載。
lazy="true"是默認(rèn)值
-->
<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("昨天聽(tīng)說(shuō)PX裝置爆炸了?");post.setPostName("有沒(méi)有污染?");post.setPostTime(new Date());ForumReply reply1= new ForumReply();reply1.setReplyContent("漳州的,我這里距離很遠(yuǎn)!");reply1.setReplyTime(new Date());ForumReply reply2= new ForumReply();reply2.setReplyContent("不知道啊,應(yīng)該還好!");reply2.setReplyTime(new Date()); post.getReplys().add(reply1); post.getReplys().add(reply2);reply1.setPost(post); //多的一方要維護(hù)關(guān)系,添加外鍵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);}}?
轉(zhuǎn)載于:https://www.cnblogs.com/QinH/p/4411242.html
總結(jié)
以上是生活随笔為你收集整理的Hiberate--one to many的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: IOS-C语言小练习02
- 下一篇: 【bzoj1565】[NOI2009]植