grails - 在Grails中创建域关系会返回无法解决的类错误

标签 grails groovy

祝大家有美好的一天。我在使用grails方面是一个新手,在遵循创建域关系的方法之前,我已经遵循了一些针对使用grails的初学者的教程。但是,我现在陷入了这个问题。我有3个 Realm 类,即todo,category和user。当我定义他们的关系时,它返回一个错误,提示无法解析类。请在下面查看我的代码。请帮忙。非常感谢。

Todo.groovy类

package todoScaff

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

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

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

}

enter image description here

enter image description here

enter image description here

enter image description here

最佳答案

由于已将域类放置在不同的程序包中,因此必须将这些类导入文件的开头。

package categoryScaff

import todoScaff.Todo
import userScaff.User

class Category {

在引用当前程序包之外的类的其他域类中也需要进行同样的操作。

关于grails - 在Grails中创建域关系会返回无法解决的类错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33757979/

相关文章:

java - 有效的 Java 代码不是有效的 Groovy 代码?

grails - 如何在 IntelliJ IDEA Community Edition 9.0 BETA 中启用 Grails 支持?

grails 向 hasErrors 添加验证错误

grails - 从 Grails 过滤器构造函数访问应用程序上下文

grails - Intellij不再在Grails中运行单个Spock测试

groovy - 引用 Grails GORM 中的引用表的域对象

Java运行自定义的groovy代码,如何保证groovy安全?

mongoDB 作为 Grails 应用程序的文件存储

java - 如何在 grails 独立应用程序中嵌入 mysql

ant - Ant : Use script task or groovy task? 中的 Groovy 脚本