【发布时间】:2015-08-18 13:37:18
【问题描述】:
我尝试通过以下方式获取所有针对包含一个特定用户的受众的帖子:
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
第二个也不行,返回:
[]
我有以下领域模型:
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 发布一个可见的帖子,其中包含 user2 和 user3。 现在我想获取对 user2 可见的所有帖子...
执行此操作的最佳方法是什么?动态查找器,在哪里查询、标准或 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类会导致问题。根据 Emmanuel 的评论,我发现错误(与上述相同的错误)是在尝试查询朋友区时引发的。例如:
def audience = Friendzone.get(1)
可能是和“用户类”的关系
static belongsTo = User
static hasMany = [friends:User,
streamposts:StreamPost]
【问题讨论】:
标签: hibernate grails nested grails-orm has-many