0
我試圖在Grails中拉回一個複合對象。Grails - 返回一個包含其他對象的對象?
我有一個用戶類,看起來像這樣。
public class User {
String email
String firstName
String lastName
String password
SortedSet tasks
static hasMany = [tasks: Task]
static constraints = {
email (blank: false, email:true)
firstName (blank: false)
lastName (blank: false)
password (blank: false)
tasks (blank: true)
}
static mapping = {
table 'User'
firstName column:'FirstName'
lastName column:'LastName'
password column:'Password'
email column:'Email'
tasks column:'UserId'
}
我也有一個Task類,看起來像這樣。
public class Task {
String imageUrl
String name
String shortDescription
String longDescription
Date dueDate
Boolean complete
static belongsTo = [User]
static constraints = {
imageUrl (blank: true)
name (blank: false, nullable: false)
shortDescription (nullable: false)
longDescription (nullable: false)
dueDate (blank: false)
complete (blank: false)
}
static mapping = {
table 'Task'
imageUrl column:'ImageUrl'
name column:'Name'
shortDescription column:'ShortDescription'
longDescription column:'LongDescription'
dueDate column:'DueDate'
complete column:'Complete'
}
每個類都有單獨的控制器。如您所見,用戶擁有(包含)許多任務。這就是爲什麼我使用SortedSet來包含所有任務。
我的問題:在下面的UserController代碼中,我需要收集SortedList中userToDisplay的所有任務。你能告訴我我做錯了什麼嗎? SortedSet中沒有任何東西被拉回。
def Login()
{
if(request.method == 'POST')
{
def userToDisplay = User.findByEmailAndPassword(params.email, params.password)
userToDisplay.Tasks = Task.findAllByUserId(userToDisplay.id)
if(userToDisplay)
{
//We have our user. Display his info.
}
else
{
//We don't have our user. Display an error message.
}
}
}
請告訴我如何解決此問題。這看起來非常簡單,我不太明白。謝謝。