【问题标题】:How to create 3 domain relationships in Grails 3?如何在 Grails 3 中创建 3 个域关系?
【发布时间】:2015-11-20 15:46:46
【问题描述】:

我有 3 个域类,我需要定义它们的关系,但我发现无法正确定义它们。有人可以纠正我的代码吗?我总是收到指向 id 的错误未知列。请帮忙。以下是我的代码。谢谢。

Todo.groovy 类

package todoScaff

import userScaff.User
import categoryScaff.Category

class Todo {

String name
String note
Date createDate
Date dueDate
Date completedDate
String priority
String status
User owner
Category category

static belongsTo = [User, Category]

static constraints = {
    name(blank:false)
    createDate()
    priority()
    status()
    note(maxsize:1000, nullable:true)
    completedDate(nullable:true)
    dueDate(nullable:true)
}


 String toString() {
    name
 }


}

Category.groovy 类

package categoryScaff

import todoScaff.Todo
import userScaff.User

class Category {

String name
String description
User user
static belongsTo = User
static hasMany = [todos: Todo]


static constraints = {
    name(blank:false)
}

 String toString(){
    name
 }

}

User.groovy 类

package userScaff

import todoScaff.Todo
import categoryScaff.Category

class User {

String userName
String fname
String lname

static hasMany = [todos: Todo, categories: Category]

static constraints = {

    userName(blank:false, unique:true)
    fname(blank:false)
    lname(blank:false)
}

 String toString(){
    "$lname, $fname"
 }

}

错误

【问题讨论】:

  • 包含您的 SQL 表结构(表和列的名称)

标签: grails grails-orm grails-3.0


【解决方案1】:

我相当肯定您的 SQL 表不正确。

我用你的 3 个域类创建了一个小项目,并使用了 grails 命令schema-export;删除与域关系无关的所有内容后,结构应如下所示:

create table category (
    id bigint,
    user_id bigint,
);
create table todo (
    id bigint,
    category_id bigint,
    owner_id bigint,
);
create table user (
    id bigint
);

如果您的表结构与此不同,您应该更改它以匹配或使用域类中的static mapping = {} 闭包来指示正确的列名。

静态映射
Todo:

static mapping = {
    owner column: 'owner_id' // change owner_id to match your owner column within todo
    category column: 'category_id' // change category_id to match your category column within todo
}

Category:

static mapping = {
    owner user: 'user_id' // change user_id to match your user column within category
}

【讨论】:

    猜你喜欢
    • 2023-03-04
    • 2019-10-06
    • 1970-01-01
    • 1970-01-01
    • 2016-09-10
    • 1970-01-01
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多