日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Hiberate--one to many

發布時間:2023/12/18 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

總結

以上是生活随笔為你收集整理的Hiberate--one to many的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。