`

hibernate annotation 双向一对多 List映射

    博客分类:
  • Java
阅读更多

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

 

comment代码:

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

测试代码:

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

这里需要注意:很多时候你在一对多list关联的时候,不真的需要indexcolumn来维护顺序。比如在线图书馆,如果没有indexcolumn也没问题。因为comment总是一条一条加的,那么indexcolumn那一列其实总不变。只有一下添加多个comment的时候,如上面代码,indexcolumn才会增加。comment的id本身就可以用来排序。

在线图书馆其实用不上indexcolumn, 实现的时候想错了,既然已经调通了,就记下来以后也许用得上。

分享到:
评论

相关推荐

    Hibernate+中文文档

    7.4.1. 一对多(one to many) / 多对一(many to one) 7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many ...

    HibernateAPI中文版.chm

    7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) ...

    hibernate3.2中文文档(chm格式)

    7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) ...

    Hibernate中文详细学习文档

    7.4.1. 一对多(one to many) / 多对一(many to one) 7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many ...

    最全Hibernate 参考文档

    7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在集合中出现的依赖对象 ...

    Hibernate 中文 html 帮助文档

    7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在...

    hibernate 体系结构与配置 参考文档(html)

    一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在...

    Hibernate教程

    8.5.1. 一对多(one to many) /多对一( many to one) 8.5.2. 一对一(one to one) 8.5.3. 多对多(many to many) 9. 组件(Component)映射 9.1. 依赖对象(Dependent objects) 9.2. 在集合中出现的依赖...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) ...

    Hibernate3+中文参考文档

    7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在集合中出现的依赖对象 ...

    hibernate3.04中文文档.chm

    8.5.1. 一对多(one to many) /多对一( many to one) 8.5.2. 一对一(one to one) 8.5.3. 多对多(many to many) 9. 组件(Component)映射 9.1. 依赖对象(Dependent objects) 9.2. 在集合中出现的依赖...

    hibernate 框架详解

    一对多(one to many) /多对一( many to one) 8.5.2. 一对一(one to one) 8.5.3. 多对多(many to many) 9. 组件(Component)映射 9.1. 依赖对象(Dependent objects) 9.2. 在集合中出现的依赖对象 ...

    Hibernate参考文档

    7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在...

    Hibernate注释大全收藏

    这种策略支持双向的一对多关联,但不支持 IDENTIFY 生成器策略,因为ID必须在多个表间共享。一旦使用就不能使用AUTO和IDENTIFY生成器。 每个类层次结构一张表 @Entity @Inheritance(strategy=InheritanceType....

    SpringMVC+Hibernate全注解整合

    &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt; &lt;list&gt; &lt;value&gt;com.org.entity&lt;/value&gt; &lt;/list&gt; ...

    低清版 大型门户网站是这样炼成的.pdf

    4.3.7 映射一对多双向自身关联关系 244 4.3.8 映射多对多单向关联关系 247 4.3.9 映射多对多双向关联关系 252 4.3.10 映射组成关系 256 4.3.11 映射继承关系 260 4.3.12 hibernate的集合映射 266 4.4 小结 270...

    spring3.2+strut2+hibernate4

    --此处hibernate 的映射采用的是.xml 配置则应设置为:class=”org.springframework.orm.hibernate3.LocalSessionFactoryBean”--&gt; &lt;property name="dataSource" ref="dataSource" /&gt; &lt;prop key="...

    spring_MVC源码

    18. class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt; 19. &lt;property name="dataSource" ref="dataSource" /&gt; 20. &lt;property name="hibernateProperties"&gt; 21. &lt;props&gt; 22...

    java开源包1

    JCarder 是一个用来查找多线程应用程序中一些潜在的死锁,通过对 Java 字节码的动态分析来完成死锁分析。 Java的Flash解析、生成器 jActionScript jActionScript 是一个使用了 JavaSWF2 的 Flash 解析器和生成器。...

    java开源包11

    JCarder 是一个用来查找多线程应用程序中一些潜在的死锁,通过对 Java 字节码的动态分析来完成死锁分析。 Java的Flash解析、生成器 jActionScript jActionScript 是一个使用了 JavaSWF2 的 Flash 解析器和生成器。...

Global site tag (gtag.js) - Google Analytics