`
小于Brick
  • 浏览: 88984 次
  • 性别: Icon_minigender_1
  • 来自: 新疆
社区版块
存档分类
最新评论

grails hasMany 使用

阅读更多

 

grails3 hasMany,一对多关系

参考:http://docs.grails.org/latest/ref/Domain%20Classes/hasMany.html

 

对象说明:

作者:Author

书:Book

Author拥有多个Book

数据库会建立3张表:

author、book、 author_book(一_ 多)

 

domain:

class Author {
    String name
    static hasMany = [books: Book]
    static constraints = {
    }
}

 

class Book {
    String title
    static constraints = {
    }
}

 

数据库:

 

页面视图:

 

create.gsp页面添加:

<g:form action="save">
   <fieldset class="form">
      <f:all bean="author"/>
<!-- 这里 ↓-->
      <g:select name="books" from="${com.o2o.Book.list()}" size="5" multiple="yes" optionKey="id"
value="${author?.books}" />
<!-- 这里 ↑-->
   </fieldset>
   <fieldset class="buttons">
        <g:submitButton name="create" class="save" value="${message(code: 'default.button.create.
label', default: 'Create')}" />
   </fieldset>
</g:form>

 

 

 

============================================================================

 

 

也可以通过代码自己控制关系:
@Transactional
def saveDemo() {
    def author = new Author();
    author.name = "张三";
    def book2 = new Book(title: "书2");
    def book3 = new Book(title: "书3");
    Set<Book> books=new HashSet<Book>();
    books.add(book2);
    books.add(book3);
    //主要是下面这行代码
    author.addToBooks(books);
    author.errors.each{
        println "it:"+it
    }
    author.save(flush:true);

    render "ok";
    return ;
}
 

 

 

 

分享到:
评论

相关推荐

    Grails入门教程(二)

    在 Grails 中,我们可以使用 belongsTo 和 hasMany 两个关键字来定义域关系。belongsTo 用于定义一个域对象所属的其他域对象,而 hasMany 用于定义一个域对象拥有的多个其他域对象的引用。 二、域类设计 在设计域...

    Grails权威指南.pdf

    - **Relationships**:支持一对一、一对多、多对多等关系映射,例如belongsTo、hasMany、hasOne等。 - **Criteria API**:用于构建灵活的数据库查询,避免硬编码SQL。 5. **Ajax集成**: - **AJAX in Grails**:...

    grails项目配置

    在Grails中,一对多关系是通过在模型类中定义`hasMany`属性来实现的。例如,如果有一个`User`类和一个`Post`类,用户可以有多个帖子,那么在`User`类中会有一行代码: ```groovy static hasMany = [posts: Post] ...

    精通 Grails 使用 Ajax 实现多对多关系

    在Book类中,我们可以声明`static hasMany = [authors: Author]`,表明一本书可以有多个作者。同样,在Author类中,声明`static hasMany = [books: Book]`,表示一个作者可以写多本书。 2. 添加`belongsTo`属性:...

    一步一步学grails(4)

    同时,`Planning`类拥有两个1:M关系,即`materials`和`craftList`,分别关联到`Material`(物料表项)和`Crafts`(工艺表项)领域类,这通过`static hasMany=[materials:Material, craftList:Crafts]`定义。...

    Grails中集成GWT中文资料

    static hasMany = [books: Book] static constraints = { books(nullable: true) } } ``` 3. **BootStrap配置**:在`BootStrap.groovy`中,可以定义初始化应用时执行的代码,例如创建初始数据。 ```groovy ...

    Grails 开发手册1

    - 在hasMany关系中,Grails会自动创建一个Set类型的属性,如`Author.get(1).books.each { println it.title }` 6. **配置说明** Grails的配置文件通常位于`conf/Config.groovy`和`conf/DataSource.groovy`,它们...

    Grails完全入门.pdf

    通过静态属性`hasMany`和`belongsTo`来定义一对多和多对一的关系。 八、领域对象验证 使用域约束来验证实体类字段的合法性,如非空、唯一性、范围限制等。 九、Grails的运行环境 支持`prod`、`dev`和`test`等多种...

    GWTGrailsTutorial 实面gwt和grails groovy集成

    这里的`belongsTo`关系表示每个`Book`都属于一个`Author`,而`hasMany`关系表示一个`Author`可以有多个`Book`。`constraints`块允许我们设置验证规则,例如,这里允许`Author`没有关联的`Book`。 ### 初始化数据 在...

    Grails Quick Reference 1.1

    - **`Author` 类**:表示作者实体,拥有一个 `name` 属性和两个 `hasMany` 关联:`books` 和 `coBooks`,分别对应主作者和合著者书籍。 - **`Publisher` 类**:代表出版社,使用了 `static mapping` 块来定义...

    Grails_DC_Cheat_Sheet_1.09.pdf

    2. **Author类**:Author类拥有`name`属性,以及两个`hasMany`关系`books`和`coBooks`,分别表示由该作者写的所有书籍和作为合著者的书籍列表。 3. **Publisher类**:Publisher类展示了更复杂的数据库映射配置,如...

    The Grails Framework - Reference Documentation

    - **一对多关联**:使用`hasMany`属性。 - **多对多关联**:通常通过中间表实现。 - **集合类型**:支持多种集合类型,如列表、集等。 - **组合关系**:将实体细分为更小的组成部分。 - **继承关系**:支持单一表...

    Spring Recipes: A Problem-Solution Approach, Second Edition

    The release of Spring Framework 3 has ushered in many improvements and new features. Spring Recipes: A Problem-Solution Approach, Second Edition continues upon the bestselling success of the previous ...

    Spring.Boot.in.Action.2015.12.pdf

    Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and manning Publications was aware of a...

Global site tag (gtag.js) - Google Analytics