Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Grails域OOP_Oop_Grails - Fatal编程技术网

Grails域OOP

Grails域OOP,oop,grails,Oop,Grails,这是我正在学习的grails的一个例子, 我有两个域,每当我调用控制器时,它会给我一个错误,我给了他一个int,但需要一个package.domain。 用户和另一个是测试。其中包括: 测试域: Long id Date created bool enabled User user 用户域: Long id String firstname String lastname 和我的控制器: user.id = 1 def test = new Test( created: new Da

这是我正在学习的grails的一个例子, 我有两个域,每当我调用控制器时,它会给我一个错误,我给了他一个int,但需要一个package.domain。 用户和另一个是测试。其中包括:

测试域:

Long id
Date created
bool enabled
User user
用户域:

Long id
String firstname
String lastname
和我的控制器:

user.id = 1
def test = new Test(
    created: new Date(),
    enabled: true,
    user: id));
... test.save(flush:true)

我不知道如何使用测试域中的用户属性获取用户域。有人能解释一下吗。

测试
构造函数需要一个域对象作为
user
属性的值。因此,可以尝试类似的方法(根据需要替换
1
):


对于这种情况,应该使用
User.load(1)
来避免不必要的数据库查找。
User
实例只提供其id作为外键,因此
load()
是一种轻量级方法,因为除非您访问非id属性,否则它不会访问数据库。如果我想获取id之外的任何atributte,该怎么办?我应该使用
getByFirstName(“john”)?
def test = new Test(created: new Date(), enabled: true, user: User.get(1));