人在江湖

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

          hibernate一對多雙向映射通常通過“多”的一端負責維護關系。但是對于list, 因為list保存關于“順序”的信息,而多的一端沒有這樣的信息,所以只能由“一”的一端維護關系。

          用在線圖書館舉個例子。書和評論之間是一對多的關系。

          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: }

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

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

          posted on 2011-03-19 20:10 人在江湖 閱讀(3477) 評論(0)  編輯  收藏 所屬分類: hibernate
          主站蜘蛛池模板: 高密市| 江门市| 扎鲁特旗| 开原市| 德阳市| 垫江县| 鄯善县| 福清市| 团风县| 恩平市| 威海市| 文安县| 绵阳市| 治县。| 汝城县| 贵定县| 鱼台县| 淳安县| 丰台区| 镇原县| 海宁市| 扬中市| 虞城县| 沙雅县| 太仆寺旗| 姜堰市| 白玉县| 南和县| 如东县| 罗山县| 凯里市| 青河县| 呼和浩特市| 曲周县| 静乐县| 安泽县| 东丽区| 凤台县| 灵寿县| 翼城县| 白沙|