<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    人在江湖

      BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
      82 Posts :: 10 Stories :: 169 Comments :: 0 Trackbacks

    hibernate一對(duì)多雙向映射通常通過“多”的一端負(fù)責(zé)維護(hù)關(guān)系。但是對(duì)于list, 因?yàn)閘ist保存關(guān)于“順序”的信息,而多的一端沒有這樣的信息,所以只能由“一”的一端維護(hù)關(guān)系。

    用在線圖書館舉個(gè)例子。書和評(píng)論之間是一對(duì)多的關(guān)系。

    book代碼:

       1: package org.emoticon.library.model;
       2:  
       3: import java.util.ArrayList;
       4: import java.util.Date;
       5: import java.util.List;
       6:  
       7: import javax.persistence.CascadeType;
       8: import javax.persistence.Entity;
       9: import javax.persistence.FetchType;
      10: import javax.persistence.GeneratedValue;
      11: import javax.persistence.Id;
      12: import javax.persistence.JoinColumn;
      13: import javax.persistence.OneToMany;
      14:  
      15: @Entity
      16: public class Book {
      17:     
      18: private Integer id;
      19: private String title;
      20: private String author;
      21: private String description;
      22: private String publisher;
      23: private int pageNumber;
      24: private Date publishTime;
      25: private String isbn;
      26: private List<Comment> comments = new ArrayList<Comment>();
      27:  
      28:  
      29: public void addComment(Comment comment){
      30:     comment.setBook(this);
      31:     comments.add(comment);
      32: }
      33:  
      34: @Id
      35: @GeneratedValue
      36: public Integer getId() {
      37:     return id;
      38: }
      39: public void setId(Integer id) {
      40:     this.id = id;
      41: }
      42: public String getTitle() {
      43:     return title;
      44: }
      45: public void setTitle(String title) {
      46:     this.title = title;
      47: }
      48: public String getAuthor() {
      49:     return author;
      50: }
      51: public void setAuthor(String author) {
      52:     this.author = author;
      53: }
      54: public String getDescription() {
      55:     return description;
      56: }
      57: public void setDescription(String description) {
      58:     this.description = description;
      59: }
      60: public String getPublisher() {
      61:     return publisher;
      62: }
      63: public void setPublisher(String publisher) {
      64:     this.publisher = publisher;
      65: }
      66: public int getPageNumber() {
      67:     return pageNumber;
      68: }
      69: public void setPageNumber(int pageNumber) {
      70:     this.pageNumber = pageNumber;
      71: }
      72: public Date getPublishTime() {
      73:     return publishTime;
      74: }
      75: public void setPublishTime(Date publishTime) {
      76:     this.publishTime = publishTime;
      77: }
      78: public String getIsbn() {
      79:     return isbn;
      80: }
      81: public void setIsbn(String isbn) {
      82:     this.isbn = isbn;
      83: }
      84:  
      85: @OneToMany(fetch = FetchType.EAGER,
      86:         cascade = {CascadeType.ALL})
      87: @JoinColumn (name = "book_id",
      88:         nullable = false)
      89: @org.hibernate.annotations.IndexColumn(name = "comment_position",
      90:         nullable = false,
      91:         base = 1)
      92: public List<Comment> getComments() {
      93:     return comments;
      94: }
      95:  
      96: public void setComments(List<Comment> comments) {
      97:     this.comments = comments;
      98: }
      99:  
     100:  
     101: }

    comment代碼:

       1: package org.emoticon.library.model;
       2:  
       3: import javax.persistence.CascadeType;
       4: import javax.persistence.Entity;
       5: import javax.persistence.GeneratedValue;
       6: import javax.persistence.Id;
       7: import javax.persistence.JoinColumn;
       8: import javax.persistence.ManyToOne;
       9:  
      10: import junit.framework.Assert;
      11:  
      12: @Entity
      13: public class Comment {
      14: private Integer id;
      15: private int rate;
      16: private String title;
      17: private String content;
      18: private Book book;
      19:  
      20: @Id
      21: @GeneratedValue
      22: public Integer getId() {
      23:     return id;
      24: }
      25: public void setId(Integer id) {
      26:     this.id = id;
      27: }
      28: public int getRate() {
      29:     return rate;
      30: }
      31:  
      32: public void setRate(int rate) {
      33:     Assert.assertTrue(rate > 0 && rate < 6);
      34:     this.rate = rate;
      35: }
      36:  
      37: public String getTitle() {
      38:     return title;
      39: }
      40:  
      41: public void setTitle(String title) {
      42:     this.title = title;
      43: }
      44:  
      45: public String getContent() {
      46:     return content;
      47: }
      48: public void setContent(String content) {
      49:     this.content = content;
      50: }
      51:  
      52: @ManyToOne(cascade = {CascadeType.ALL})
      53: @JoinColumn(name="book_id",
      54:         nullable = false,
      55:         updatable = false,
      56:         insertable = false)
      57: public Book getBook() {
      58:     return book;
      59: }
      60:  
      61: public void setBook(Book book) {
      62:     this.book = book;
      63: }
      64:  
      65: }

    測試代碼:

       1: package org.emoticon.library.manager;
       2:  
       3: import org.emoticon.core.test.DaoTestCase;
       4: import org.emoticon.library.model.Book;
       5: import org.emoticon.library.model.Comment;
       6:  
       7: public class BookManagerTest extends DaoTestCase {
       8:     private BookManager manager;
       9:     
      10:     private static final String commentTitle = "a good book";
      11:     
      12:     public void setManager(BookManager manager) {
      13:         this.manager = manager;
      14:     }
      15:     
      16:     public void testSave(){
      17:         Book entity = new Book();
      18:         entity.setTitle("thinking in java");
      19:         entity.setDescription("for newbie");
      20:         
      21:         Comment comment = new Comment();
      22:         comment.setTitle(commentTitle);
      23:         comment.setContent("I like it.");
      24:         comment.setRate(5);
      25:         
      26:         Comment comment2 = new Comment();
      27:         comment2.setTitle(commentTitle + "2");
      28:         comment2.setContent("I like it2.");
      29:         comment2.setRate(4);
      30:  
      31:         entity.addComment(comment);
      32:         entity.addComment(comment2);
      33:         
      34:         manager.save(entity);
      35:         assertNotNull(entity.getId());
      36:         entity = manager.get(entity.getId());
      37:         //assertEquals(entity.getComments().get(0).getTitle(), commentTitle);
      38:         assertNotNull(entity);
      39:         this.setComplete();
      40:     }
      41: }

    這里需要注意:很多時(shí)候你在一對(duì)多l(xiāng)ist關(guān)聯(lián)的時(shí)候,不真的需要indexcolumn來維護(hù)順序。比如在線圖書館,如果沒有indexcolumn也沒問題。因?yàn)閏omment總是一條一條加的,那么indexcolumn那一列其實(shí)總不變。只有一下添加多個(gè)comment的時(shí)候,如上面代碼,indexcolumn才會(huì)增加。comment的id本身就可以用來排序。

    在線圖書館其實(shí)用不上indexcolumn, 實(shí)現(xiàn)的時(shí)候想錯(cuò)了,既然已經(jīng)調(diào)通了,就記下來以后也許用得上。

    posted on 2011-03-19 20:10 人在江湖 閱讀(3475) 評(píng)論(0)  編輯  收藏 所屬分類: hibernate
    主站蜘蛛池模板: 亚洲精品欧美综合四区 | 免费看污成人午夜网站| 亚洲精品无码久久久久去q| 日本永久免费a∨在线视频| 国产三级免费电影| 无遮挡a级毛片免费看| 日产国产精品亚洲系列| 一级人做人爰a全过程免费视频| 亚洲人成无码www久久久| a级毛片免费观看网站| 中文字幕在线亚洲精品| 国产精成人品日日拍夜夜免费| 亚洲精品综合一二三区在线| 88xx成人永久免费观看| 亚洲欧洲另类春色校园网站| 色www永久免费视频| 日韩精品无码免费视频| 亚洲av无码专区在线播放| 99精品视频在线观看免费专区| 亚洲女人初试黑人巨高清| 成年人免费观看视频网站| 国产亚洲高清在线精品不卡| AV在线播放日韩亚洲欧| 免费91最新地址永久入口| 亚洲理论在线观看| 日韩精品视频免费网址| 又硬又粗又长又爽免费看| 久久精品国产精品亚洲毛片| 国产精品视频永久免费播放| 高潮内射免费看片| 婷婷亚洲综合五月天小说| 免费人成视频在线| 男女拍拍拍免费视频网站 | 亚洲另类古典武侠| 免费h成人黄漫画嘿咻破解版| 久久性生大片免费观看性| 亚洲一区二区三区91| 亚洲AV日韩精品一区二区三区| 99久热只有精品视频免费看| 亚洲国产精品精华液| 亚洲AV无码专区国产乱码4SE|