Grails GORM 嵌套 has-many 关系查询
Grails GORM nested has-many relationship query
我尝试通过以下方式获取针对包含一个特定用户的受众的所有 post:
StreamPost.findAllByAudience(Friendzone.findAllByFriends(User.findAllById(2)))
或
def posts = StreamPost.where {
audience.friends.id ==~ userId
}.list()
第一个结果是
ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - No value specified for parameter 1
第二个也不行,returns:
[]
我有以下领域模型:
class StreamPost {
static belongsTo = User
Friendzone audience
Date postTimestamp
String postComment
String postType
static constraints = {
postType inList: ['event','activitylevel','checkin','rating']
}
}
class Friendzone {
static belongsTo = User
static hasMany = [friends:User,
streamposts:StreamPost]
User owner
String zoneName
static constraints = {
owner nullable: false
friends nullable: false
}
}
class User {
static hasMany = [friends:User,
friendzones:Friendzone,
streamposts:StreamPost]
static mappedBy = [ friends: 'friends' ]
String username
String password
boolean enabled = true
boolean accountExpired
boolean accountLocked
boolean passwordExpired
static constraints = {
username nullable: false
password nullable: true
}
}
所以 user1 可能会为他的 friendzone1 做一个 post 可见,其中包含 user2 和 user3。
现在我想获取 user2 可见的所有 posts...
哪种方法最好?动态查找器,查询、条件或 hql 在哪里?我怎样才能避免前面提到的错误?
数据库方案:
table: user
id | username
1 | user1
2 | user2
table: user_friendzone
user_id | friendzone_id
2 | 1
table: friendzone
id | owner_id | zone_name
1 | 1 | user2only
table: stream_post
id | audience_id | post_comment
1 | 1 | comment
编辑 19.08.2015
我认为 friendzone class 导致了问题。根据 Emmanuel 的评论,我发现在尝试查询朋友区时会抛出错误(与上述错误相同)。例如:
def audience = Friendzone.get(1)
可能是跟"user class"
的关系
static belongsTo = User
static hasMany = [friends:User,
streamposts:StreamPost]
这个怎么样?
def user = User.get(2)
def audiences = Friendzone.findAllByFriends(user)
def posts = StreamPost.findAllByAudienceInList(audiences)
我这样写是为了更容易阅读,并找出失败的查询。
通过更改 StreamPost 完成 class:
class StreamPost {
static belongsTo = [owner:User, audience:Friendzone]
User owner
Friendzone audience
Date postTimestamp
String postComment
String postType
static constraints = {
postType inList: ['event','activitylevel','checkin','rating']
}
}
使用以下查询:
def user = User.get(userID)
def posts = StreamPost.findAllByAudienceInList(user.friendzones.asList())
我尝试通过以下方式获取针对包含一个特定用户的受众的所有 post:
StreamPost.findAllByAudience(Friendzone.findAllByFriends(User.findAllById(2)))
或
def posts = StreamPost.where {
audience.friends.id ==~ userId
}.list()
第一个结果是
ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - No value specified for parameter 1
第二个也不行,returns:
[]
我有以下领域模型:
class StreamPost {
static belongsTo = User
Friendzone audience
Date postTimestamp
String postComment
String postType
static constraints = {
postType inList: ['event','activitylevel','checkin','rating']
}
}
class Friendzone {
static belongsTo = User
static hasMany = [friends:User,
streamposts:StreamPost]
User owner
String zoneName
static constraints = {
owner nullable: false
friends nullable: false
}
}
class User {
static hasMany = [friends:User,
friendzones:Friendzone,
streamposts:StreamPost]
static mappedBy = [ friends: 'friends' ]
String username
String password
boolean enabled = true
boolean accountExpired
boolean accountLocked
boolean passwordExpired
static constraints = {
username nullable: false
password nullable: true
}
}
所以 user1 可能会为他的 friendzone1 做一个 post 可见,其中包含 user2 和 user3。 现在我想获取 user2 可见的所有 posts...
哪种方法最好?动态查找器,查询、条件或 hql 在哪里?我怎样才能避免前面提到的错误?
数据库方案:
table: user
id | username
1 | user1
2 | user2
table: user_friendzone
user_id | friendzone_id
2 | 1
table: friendzone
id | owner_id | zone_name
1 | 1 | user2only
table: stream_post
id | audience_id | post_comment
1 | 1 | comment
编辑 19.08.2015
我认为 friendzone class 导致了问题。根据 Emmanuel 的评论,我发现在尝试查询朋友区时会抛出错误(与上述错误相同)。例如:
def audience = Friendzone.get(1)
可能是跟"user class"
的关系static belongsTo = User
static hasMany = [friends:User,
streamposts:StreamPost]
这个怎么样?
def user = User.get(2)
def audiences = Friendzone.findAllByFriends(user)
def posts = StreamPost.findAllByAudienceInList(audiences)
我这样写是为了更容易阅读,并找出失败的查询。
通过更改 StreamPost 完成 class:
class StreamPost {
static belongsTo = [owner:User, audience:Friendzone]
User owner
Friendzone audience
Date postTimestamp
String postComment
String postType
static constraints = {
postType inList: ['event','activitylevel','checkin','rating']
}
}
使用以下查询:
def user = User.get(userID)
def posts = StreamPost.findAllByAudienceInList(user.friendzones.asList())